-
Notifications
You must be signed in to change notification settings - Fork 0
/
snowflake_test.go
77 lines (60 loc) · 1.12 KB
/
snowflake_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
package snowflake
import (
"testing"
)
func TestNext(t *testing.T) {
sf, err := NewSnowFlake(1)
if err != nil {
t.Error(err)
}
id, err := sf.Next()
if err != nil {
t.Error(err)
}
println(id)
id2, err := sf.Next()
if err != nil {
t.Error(err)
}
if id >= id2 {
t.Errorf("id %v is smaller or equal to previous one %v", id2, id)
}
}
func TestDuplicate(t *testing.T) {
total := 1000 * 1000
data := make(map[uint64]int)
sf, err := NewSnowFlake(1)
if err != nil {
t.Error(err)
}
var id, pre uint64
for i := 0; i < total; i++ {
id, err = sf.Next()
if err != nil {
t.Error(err)
}
if id < pre {
t.Errorf("id %v is smaller than previous one %v", id, pre)
}
pre = id
count := data[id]
if count > 0 {
t.Errorf("duplicate id %v %d", id, count)
}
data[id] = count + 1
}
length := len(data)
t.Logf("map length %v", length)
if length != total {
t.Errorf("length does not match expected value; expected %v, actual %d", total, length)
}
}
func BenchmarkNext(b *testing.B) {
sf, err := NewSnowFlake(1)
if err != nil {
b.Error(err)
}
for i := 0; i < b.N; i++ {
sf.Next()
}
}