-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnowfake_test.go
152 lines (125 loc) · 2.74 KB
/
snowfake_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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package snowfake_test
import (
"bytes"
"errors"
"fmt"
"reflect"
"sync"
"testing"
"time"
"github.com/deryrahman/snowfake"
)
func TestNew(t *testing.T) {
resetConfig()
name := func(nodeID uint64) string {
return fmt.Sprintf("nodeID=%d", nodeID)
}
tests := []struct {
nodeID uint64
nodeBits uint8
expectedErr error
}{
{
nodeID: uint64(1),
nodeBits: 16,
expectedErr: nil,
},
{
nodeID: uint64(4),
nodeBits: 2,
expectedErr: errors.New("nodeID should less than 4"),
},
}
for _, tt := range tests {
t.Run(name(tt.nodeID), func(t *testing.T) {
snowfake.SetNodeBits(tt.nodeBits)
_ = snowfake.Init()
sf, err := snowfake.New(tt.nodeID)
assertEqual(t, tt.expectedErr, err)
if err == nil {
assertNotNil(t, sf)
}
})
}
}
func TestSnowfake_GenerateID(t *testing.T) {
resetConfig()
nodeID := uint64(29)
sf, _ := snowfake.New(nodeID)
estimateTimeFromID := uint64(time.Now().Unix()) - uint64(1577836800)
expectedNodeFromID := uint64(29)
expectedSeqFromID := uint64(0)
assertNotNil(t, sf)
if sf != nil {
id := sf.GenerateID()
assertTrue(t, estimateTimeFromID <= (id&0xFFFFFFFF00000000)>>32)
assertEqual(t, expectedNodeFromID, (id&0b11111000000000000000000000000000)>>27)
assertEqual(t, expectedSeqFromID, id&0b111111111111111111111111111)
}
}
func TestSnowfake_GenerateID_Collision(t *testing.T) {
resetConfig()
nodeID := uint64(1)
concurrent := 10000
sf, _ := snowfake.New(nodeID)
assertNotNil(t, sf)
if sf != nil {
var wg sync.WaitGroup
c := make(chan uint64, concurrent)
wg.Add(concurrent)
for i := 0; i < concurrent; i++ {
go func(c chan uint64, wg *sync.WaitGroup) {
id := sf.GenerateID()
c <- id
wg.Done()
}(c, &wg)
}
wg.Wait()
close(c)
mp := make(map[uint64]bool)
for ch := range c {
mp[ch] = true
}
assertEqual(t, concurrent, len(mp))
}
}
func resetConfig() {
snowfake.SetEpoch(1577836800)
snowfake.SetNodeBits(5)
snowfake.SetSeqBits(27)
_ = snowfake.Init()
}
func assertTrue(t *testing.T, cond bool) {
if !cond {
t.Errorf("got false, expected true")
}
}
func assertNotNil(t *testing.T, actual interface{}) {
t.Helper()
if isEqual(nil, actual) {
t.Errorf("got %v, expected nil", actual)
}
}
func assertEqual(t *testing.T, expected, actual interface{}) {
t.Helper()
if !isEqual(expected, actual) {
t.Errorf("got %v, expected %v", actual, expected)
}
}
func isEqual(expected, actual interface{}) bool {
if expected == nil || actual == nil {
return expected == actual
}
exp, ok := expected.([]byte)
if !ok {
return reflect.DeepEqual(expected, actual)
}
act, ok := actual.([]byte)
if !ok {
return false
}
if exp == nil || act == nil {
return exp == nil && act == nil
}
return bytes.Equal(exp, act)
}