-
Notifications
You must be signed in to change notification settings - Fork 20
/
gosnow_test.go
71 lines (53 loc) · 1.33 KB
/
gosnow_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
/*
github.com/twitter/snowflake in golang
*/
package gosnow
import (
"testing"
"github.com/stretchrcom/testify/assert"
)
func TestDefaultWorkId(t *testing.T) {
id := DefaultWorkId()
id2 := DefaultWorkId()
t.Logf("id %v, next id %v", id, id2)
if id != id2 {
t.Errorf("different workd id, %v and %v", id, id2)
}
}
func TestNext(t *testing.T) {
sf, err := Default()
assert.Nil(t, err)
id, err := sf.Next()
assert.Nil(t, err)
id2, err := sf.Next()
assert.Nil(t, err)
t.Logf("id %v, next id %v", id, id2)
if id > id2 {
t.Errorf("id %v is smaller then previous one %v", id2, id)
}
}
func TestDuplicate(t *testing.T) {
total := 1000 * 1000
data := make(map[uint64]int)
sf, err := Default()
assert.Nil(t, err)
var id, pre uint64
for i := 0; i < total; i++ {
id, err = sf.Next()
assert.Nil(t, err)
if id < pre {
t.Errorf("id %v is samller 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("legth does not match want %v actual %d", total, length)
}
}