forked from urnetwork/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil_test.go
More file actions
300 lines (251 loc) · 6.54 KB
/
util_test.go
File metadata and controls
300 lines (251 loc) · 6.54 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package connect
import (
"fmt"
"slices"
"sync"
"testing"
"time"
// "math"
mathrand "math/rand"
"github.com/go-playground/assert/v2"
)
func TestMonitor(t *testing.T) {
timeout := 1 * time.Second
monitor := NewMonitor()
for i := 0; i < 32; i += 1 {
update := monitor.NotifyChannel()
go monitor.NotifyAll()
select {
case <-update:
case <-time.After(timeout):
t.Fail()
}
}
}
func TestCallbackList(t *testing.T) {
callbacks := NewCallbackList[func(int)]()
n := 10
m := 100
testingCallbacks := []*testingCallback{}
for i := 0; i < n; i += 1 {
testingCallbacks = append(testingCallbacks, &testingCallback{})
}
testingCallbackIds := []int{}
assert.Equal(t, 0, len(callbacks.Get()))
for i := 0; i < n; i += 1 {
callbackId := callbacks.Add(testingCallbacks[i].Callback)
testingCallbackIds = append(testingCallbackIds, callbackId)
assert.Equal(t, i+1, len(callbacks.Get()))
}
assert.Equal(t, n, len(callbacks.Get()))
// note callbacks can be added multiple times
mathrand.Shuffle(len(testingCallbackIds), func(i int, j int) {
testingCallbackIds[i], testingCallbackIds[j] = testingCallbackIds[j], testingCallbackIds[i]
})
for i := 0; i < n; i += 1 {
callbacks.Remove(testingCallbackIds[i])
assert.Equal(t, n-1-i, len(callbacks.Get()))
}
assert.Equal(t, 0, len(callbacks.Get()))
for i := 0; i < n; i += 1 {
callbacks.Add(testingCallbacks[i].Callback)
assert.Equal(t, i+1, len(callbacks.Get()))
}
for i := 0; i < m; i += 1 {
for _, callback := range callbacks.Get() {
callback(i)
}
}
for i := 0; i < n; i += 1 {
values := testingCallbacks[i].Values()
for j := 0; j < m; j += 1 {
assert.Equal(t, j, values[j])
}
}
}
type testingCallback struct {
stateLock sync.Mutex
values []int
}
func (self *testingCallback) Callback(value int) {
self.stateLock.Lock()
defer self.stateLock.Unlock()
self.values = append(self.values, value)
}
func (self *testingCallback) Values() []int {
self.stateLock.Lock()
defer self.stateLock.Unlock()
valuesCopy := slices.Clone(self.values)
return valuesCopy
}
func TestIdleCondition(t *testing.T) {
idleCondition := NewIdleCondition()
a := idleCondition.Checkpoint()
success := idleCondition.UpdateOpen()
assert.Equal(t, true, success)
success = idleCondition.Close(a)
assert.Equal(t, false, success)
success = idleCondition.UpdateOpen()
assert.Equal(t, true, success)
success = idleCondition.Close(a)
assert.Equal(t, false, success)
idleCondition.UpdateClose()
idleCondition.UpdateClose()
b := idleCondition.Checkpoint()
go func() {
success := idleCondition.Close(b)
assert.Equal(t, true, success)
}()
success = idleCondition.WaitForClose()
assert.Equal(t, true, success)
}
func TestEvent(t *testing.T) {
event := NewEvent()
go func() {
event.Set()
}()
success := event.WaitForSet(30 * time.Second)
assert.Equal(t, true, success)
}
func TestMinTime(t *testing.T) {
a := time.Now()
n := 10
bs := make([]time.Time, n)
bs[0] = a
for i := 1; i < n; i += 1 {
bs[i] = bs[i-1].Add(time.Second)
}
mathrand.Shuffle(len(bs), func(i int, j int) {
bs[i], bs[j] = bs[j], bs[i]
})
foundA := MinTime(bs[0], bs[1:]...)
assert.Equal(t, a, foundA)
}
func TestWeightedShuffle(t *testing.T) {
// weighted shuffle many times and look at the average position
// the average position order should trend with the weight order
if testing.Short() {
t.Skip("skipping testing in short mode")
}
k := 64
n := 512
netIndexes1 := map[int]int64{}
netIndexes2 := map[int]int64{}
for i := 0; i < n*k; i += 1 {
if i%100 == 0 {
fmt.Printf("[w]%d/%d\n", i+1, n*k)
}
values := []int{}
weights := map[int]float32{}
for j := 0; j < n; j += 1 {
values = append(values, j)
weights[j] = float32(n - j)
}
WeightedShuffle(values, weights)
for index, value := range values {
netIndexes1[value] += int64(index)
}
WeightedShuffleFunc(values, func(i int) float32 {
return weights[i]
})
for index, value := range values {
netIndexes2[value] += int64(index)
}
}
test := func(netIndexes map[int]int64) {
orderedValues := []int{}
for i := 0; i < n; i += 1 {
orderedValues = append(orderedValues, i)
}
slices.SortFunc(orderedValues, func(a int, b int) int {
if netIndexes[a] < netIndexes[b] {
return -1
} else if netIndexes[b] < netIndexes[a] {
return 1
} else {
return 0
}
})
errorThreshold := 2 * n / k
for i := 0; i < n; i += 1 {
e := i - orderedValues[i]
if -errorThreshold <= e && e <= errorThreshold {
e = 0
}
assert.Equal(t, 0, e)
}
}
test(netIndexes1)
test(netIndexes2)
}
func TestWeightedShuffleWithEntropy(t *testing.T) {
// as entropy approaches 1, the weighted shuffle should become uniform
if testing.Short() {
t.Skip("skipping testing in short mode")
}
k := 64
n := 256
orderedEntropies := []float32{
0.0,
0.5,
1.0,
}
for entropyIndex, entropy := range orderedEntropies {
netIndexes1 := map[int]int64{}
netIndexes2 := map[int]int64{}
for i := 0; i < n*k; i += 1 {
if i%100 == 0 {
fmt.Printf("[we]%d/%d\n", i+1, n*k)
}
values := []int{}
weights := map[int]float32{}
for j := 0; j < n; j += 1 {
values = append(values, j)
weights[j] = float32(n - j)
}
WeightedShuffleWithEntropy(values, weights, entropy)
for index, value := range values {
netIndexes1[value] += int64(index)
}
WeightedShuffleFuncWithEntropy(values, func(i int) float32 {
return weights[i]
}, entropy)
for index, value := range values {
netIndexes2[value] += int64(index)
}
}
testError := func(testEntropy float32, expected bool, netIndexes map[int]int64) {
// n * k * ((1-e)*n + e*n/k)
// == n^2 * ((1-e)*k+e)
errorThreshold := int64(float32(n) * float32(n) * ((1-testEntropy)*float32(k) + testEntropy))
failed := false
for i := 1; i < n; i += 1 {
a := i / 2
b := n - (i+1)/2
e := netIndexes[a] - netIndexes[b]
if -errorThreshold <= e && e <= errorThreshold {
e = 0
}
if expected {
// all must pass
assert.Equal(t, int64(0), e)
} else if int64(0) != e {
failed = true
}
}
if !expected {
// at least one of the comparisons must have failed
// not all must fail
assert.Equal(t, true, failed)
}
}
fmt.Printf("[entropy]%d\n", entropyIndex)
testError(entropy, true, netIndexes1)
testError(entropy, true, netIndexes2)
// the test should fail at the next entropy index (tigher error bound)
if entropyIndex+1 < len(orderedEntropies) {
testError(orderedEntropies[entropyIndex+1], false, netIndexes1)
testError(orderedEntropies[entropyIndex+1], false, netIndexes2)
}
}
}