-
Notifications
You must be signed in to change notification settings - Fork 0
/
snowflake_test.go
executable file
·123 lines (108 loc) · 2.51 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
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
package snowflake
import (
"fmt"
"testing"
"time"
)
func TestSnowflake(t *testing.T) {
sf0 := NewSnowflake(0)
sf1 := NewSnowflake(1)
map0 := make(map[int64]bool)
map1 := make(map[int64]bool)
for i := 0; i < 2000; i++ {
v0 := sf0.Next()
v1 := sf0.Next()
v2 := sf1.Next()
v3 := sf1.Next()
if map0[v0] {
t.Errorf("v0(%x) already exist in map0", v0)
}
if map0[v1] {
t.Errorf("v1(%x) already exist in map0", v1)
}
map0[v0] = true
map0[v1] = true
if map1[v2] {
t.Errorf("v2(%x) already exist in map1", v2)
}
if map1[v3] {
t.Errorf("v3(%x) already exist in map1", v3)
}
map1[v2] = true
map1[v3] = true
if map0[v2] {
t.Errorf("v2(%x) should not exist in map0", v2)
}
if map0[v3] {
t.Errorf("v3(%x) should not exist in map0", v3)
}
time.Sleep(time.Millisecond)
}
}
func TestInvalidNode(t *testing.T) {
defer func() {
if err := recover(); err != nil {
fmt.Printf("TestInvalidNode panic: %v\n", err)
}
}()
NewSnowflake(4096)
}
func TestEpoch(t *testing.T) {
sf0 := NewSnowflakeEpoch(0, 100)
v0 := sf0.Next()
v1 := sf0.Next()
if v0 == v1 {
t.Fatalf("v0 equals v1, should different")
}
}
func TestEpochInvalidNode(t *testing.T) {
defer func() {
if err := recover(); err != nil {
fmt.Printf("TestEpochInvalidNode panic: %v\n", err)
}
}()
NewSnowflakeEpoch(4096, 100)
}
func TestVeryLargeEpoch(t *testing.T) {
defer func() {
if err := recover(); err != nil {
fmt.Printf("TestVeryLargeEpoch panic: %v\n", err)
}
}()
sf0 := NewSnowflakeEpoch(0, 9223372036854770000)
sf0.Next()
}
func TestTimeMovedBackwards(t *testing.T) {
defer func() {
if err := recover(); err != nil {
fmt.Printf("TestTimeMovedBackwards panic: %v\n", err)
}
}()
sf0 := NewSnowflake(0)
sf0.lastTime, sf0.lastTimestamp = sf0.ts()
sf0.lastTime = sf0.lastTime.Add(time.Second)
sf0.lastTimestamp += 1000
sf0.Next()
}
func TestReachedMaxSequence(t *testing.T) {
sf0 := NewSnowflake(0)
for i := 0; i < 10000; i++ {
v0 := sf0.Next()
v1 := sf0.Next()
if v0 == v1 {
t.Fatalf("v0 equals v1, should different")
}
}
}
var result int64
func BenchmarkSnowflake(b *testing.B) {
sf := NewSnowflake(99)
var v int64
for n := 0; n < b.N; n++ {
// record the result prevent the compiler eliminating the function call.
v = sf.Next()
}
// store the result to a package level variable
// so the compiler cannot eliminate the Benchmark itself.
result = v
}