-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflakeid_test.go
71 lines (53 loc) · 1.16 KB
/
flakeid_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
package flakeid
import (
"sort"
"testing"
)
const size = 100000
type uint64Sort []uint64
func (p uint64Sort) Len() int { return len(p) }
func (p uint64Sort) Less(i, j int) bool { return p[i] < p[j] }
func (p uint64Sort) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func TestSingleRoutine(t *testing.T) {
gen := new(FlakeID)
ids := make([]uint64, size)
for i := 0; i < size; i++ {
ids[i] = gen.NextID()
}
verify(t, ids)
}
func TestMultipleRoutines(t *testing.T) {
gen := new(FlakeID)
ids := make([]uint64, size)
done := make(chan bool)
routine := func(ids []uint64) {
for i := range ids {
ids[i] = gen.NextID()
}
verify(t, ids)
done <- true
}
go routine(ids[0:25000])
go routine(ids[25000:50000])
go routine(ids[50000:75000])
go routine(ids[75000:])
for i := 0; i < 4; i++ {
<-done
}
sort.Sort(uint64Sort(ids))
verify(t, ids)
}
func verify(t *testing.T, ids []uint64) {
size := len(ids)
for i := 1; i < size; i++ {
if ids[i] <= ids[i-1] {
t.Errorf("Invalid order of identifiers in iteration %d", i)
}
}
}
func BenchmarkNextID(b *testing.B) {
gen := new(FlakeID)
for n := 0; n < b.N; n++ {
gen.NextID()
}
}