forked from sdming/gosnow
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgosnow_test.go
64 lines (48 loc) · 1.11 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
/*
github.com/twitter/snowflake in golang
*/
package gosnow
import (
//"github.com/stretchrcom/testify/assert"
"testing"
)
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) {
id := Default.Next()
id2 := Default.Next()
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 := Default
var id, pre uint64
for i := 0; i < total; i++ {
id = sf.Next()
if id < pre {
t.Errorf("id %v is samller than previous one %v (%v)", id, pre, (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("MaxSequence %d, workerId %d", MaxSequence, sf.workerId)
t.Logf("map length %v", length)
if length != total {
t.Errorf("legth does not match want %v actual %d", total, length)
}
}