-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfactory_test.go
63 lines (55 loc) · 1.15 KB
/
factory_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
package snowflake
import (
"testing"
)
func TestMask(t *testing.T) {
tests := map[uint]int64{
1: 0x1,
2: 0x3,
3: 0x7,
4: 0xf,
5: 0x1f,
6: 0x3f,
7: 0x7f,
8: 0xff,
}
for b, m := range tests {
if v := mask(b); v != m {
t.Errorf("expected %v; got %v\n", m, v)
}
}
}
func TestIdNReturnsUniqueValues(t *testing.T) {
for serverBits := 2; serverBits <= 4; serverBits++ {
for sequenceBits := 2; sequenceBits <= 4; sequenceBits++ {
verifyUnique(t, uint(serverBits), uint(sequenceBits))
}
}
}
func verifyUnique(t *testing.T, serverBits, sequenceBits uint) {
generator := NewFactory(FactoryOptions{
ServerBits: serverBits,
SequenceBits: sequenceBits,
})
n := 4096
rounds := 1024
allIds := make([][]int64, 0, rounds)
for i := 0; i < rounds; i++ {
ids := generator.IdN(n)
allIds = append(allIds, ids)
}
unique := map[int64]struct{}{}
for _, ids := range allIds {
if v := len(ids); v != n {
t.Errorf("expected %v; got %v\n", n, v)
}
for _, id := range ids {
unique[id] = struct{}{}
}
}
expected := n * rounds
if v := len(unique); v != expected {
t.Errorf("expected %v; got %v\n", expected, v)
return
}
}