This repository has been archived by the owner on Sep 20, 2022. It is now read-only.
forked from dedis/onet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
treestorage_test.go
167 lines (125 loc) · 3.56 KB
/
treestorage_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package onet
import (
"runtime"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
"go.dedis.ch/onet/v3/network"
)
const treeStoreTimeout = 200 * time.Millisecond
func checkLeakingGoroutines(t *testing.T) {
buf := make([]byte, 2<<20)
buf = buf[:runtime.Stack(buf, true)]
require.False(t, strings.Contains(string(buf), "(*treeStorage).Remove"))
}
// Tests the main use cases
func TestTreeStorage_SimpleCase(t *testing.T) {
store := newTreeStorage(treeStoreTimeout)
tree := &Tree{ID: TreeID{1}}
require.False(t, store.IsRegistered(tree.ID))
store.Register(tree.ID)
require.True(t, store.IsRegistered(tree.ID))
require.Nil(t, store.Get(tree.ID))
store.Set(tree)
require.NotNil(t, store.Get(tree.ID))
store.Remove(tree.ID)
require.NotNil(t, store.Get(tree.ID))
time.Sleep(treeStoreTimeout + 50*time.Millisecond)
require.Nil(t, store.Get(tree.ID))
require.False(t, store.IsRegistered(tree.ID))
}
// Tests the behaviour of Unregister
func TestTreeStorage_Registration(t *testing.T) {
store := newTreeStorage(treeStoreTimeout)
tree := &Tree{ID: TreeID{1}}
store.Register(tree.ID)
require.True(t, store.IsRegistered(tree.ID))
store.Unregister(tree.ID)
require.False(t, store.IsRegistered(tree.ID))
store.Set(tree)
store.Unregister(tree.ID)
require.True(t, store.IsRegistered(tree.ID))
}
func TestTreeStorage_GetRoster(t *testing.T) {
store := newTreeStorage(treeStoreTimeout)
ro1 := genRoster(testSuite, []network.Address{""})
ro2 := genRoster(testSuite, []network.Address{""})
ro3 := genRoster(testSuite, []network.Address{""})
store.Set(&Tree{ID: TreeID(ro1.GetID()), Roster: ro1})
store.Set(&Tree{ID: TreeID(ro2.GetID()), Roster: ro2})
require.NotNil(t, store.GetRoster(ro1.GetID()))
require.Nil(t, store.GetRoster(ro3.GetID()))
}
// Tests that the tree won't be removed if it is set again after a remove
func TestTreeStorage_CancelDeletion(t *testing.T) {
store := newTreeStorage(treeStoreTimeout)
tree := &Tree{ID: TreeID{1}}
store.Set(tree)
store.Remove(tree.ID)
time.Sleep(50 * time.Millisecond)
store.Set(tree)
time.Sleep(treeStoreTimeout + 50*time.Millisecond)
require.NotNil(t, store.Get(tree.ID))
store.Remove(tree.ID)
time.Sleep(treeStoreTimeout / 2)
require.NotNil(t, store.getAndRefresh(tree.ID))
time.Sleep(treeStoreTimeout)
require.NotNil(t, store.Get(tree.ID))
}
// Tests if planned removals are correctly stopped
func TestTreeStorage_Close(t *testing.T) {
store := newTreeStorage(treeStoreTimeout)
trees := []*Tree{
&Tree{ID: TreeID{1}},
&Tree{ID: TreeID{2}},
&Tree{ID: TreeID{3}},
}
for _, tree := range trees {
store.Set(tree)
store.Remove(tree.ID)
store.Remove(tree.ID)
}
store.Close()
// make sure several calls are ok
store.Close()
// this should not create a new goroutine
store.Remove(trees[0].ID)
checkLeakingGoroutines(t)
}
// This test is intented to be run with -race to detect race conditions
func TestTreeStorage_Race(t *testing.T) {
store := newTreeStorage(treeStoreTimeout)
trees := []*Tree{
&Tree{ID: TreeID{1}},
&Tree{ID: TreeID{2}},
&Tree{ID: TreeID{3}},
}
n := 1000
k := 20
wg := sync.WaitGroup{}
lock := sync.Mutex{}
for i := 0; i < k; i++ {
wg.Add(1)
go func() {
for j := 0; j < n; j++ {
store.Set(trees[0])
store.Remove(trees[2].ID)
store.Get(trees[1].ID)
store.Set(trees[2])
store.Set(trees[1])
store.Get(trees[2].ID)
store.Get(trees[1].ID)
store.Remove(trees[0].ID)
}
lock.Lock()
wg.Done()
lock.Unlock()
}()
}
wg.Wait()
store.wg.Wait()
require.Equal(t, 2, len(store.trees))
checkLeakingGoroutines(t)
}