-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathspaceflake_test.go
205 lines (179 loc) · 5.06 KB
/
spaceflake_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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package spaceflake
import (
"testing"
"time"
)
// Probably not the best tests, but they test what I want to test
func TestSpaceflakeBulkGeneration(t *testing.T) {
spaceflakes := map[uint64]*Spaceflake{}
settings := NewBulkGeneratorSettings(1000000) // Default epoch
bulk, err := BulkGenerate(settings)
if err != nil {
t.Error(err)
return
}
for _, sf := range bulk {
if spaceflakes[sf.ID()] != nil {
t.Errorf("Failed! Spaceflake ID %d already exists", sf.ID())
return
}
spaceflakes[sf.ID()] = sf
}
t.Log("Success! All Spaceflakes are unique")
}
func TestSpaceflakeBulkGenerationNode(t *testing.T) {
spaceflakes := map[uint64]*Spaceflake{}
node := NewNode(1)
bulk, err := node.BulkGenerateSpaceflakes(1000000)
if err != nil {
t.Error(err)
return
}
for _, sf := range bulk {
if sf.NodeID() != 1 {
t.Error("Failed! Generated Spaceflake with wrong Node ID")
return
}
if spaceflakes[sf.ID()] != nil {
t.Errorf("Failed! Spaceflake ID %d already exists", sf.ID())
return
}
spaceflakes[sf.ID()] = sf
}
t.Log("Success! All Spaceflakes are unique")
}
func TestSpaceflakeBulkGenerationWorker(t *testing.T) {
spaceflakes := map[uint64]*Spaceflake{}
node := NewNode(1)
worker := node.NewWorker()
bulk, err := worker.BulkGenerateSpaceflakes(1000000)
if err != nil {
t.Error(err)
return
}
for _, sf := range bulk {
if sf.WorkerID() != 1 {
t.Error("Failed! Generated Spaceflake with wrong Worker ID")
return
}
if spaceflakes[sf.ID()] != nil {
t.Errorf("Failed! Spaceflake ID %d already exists", sf.ID())
return
}
spaceflakes[sf.ID()] = sf
}
t.Log("Success! All Spaceflakes are unique")
}
func TestSpaceflakeAt(t *testing.T) {
node := NewNode(1)
worker := node.NewWorker()
sf, err := worker.GenerateSpaceflakeAt(time.Date(2018, 7, 21, 13, 43, 32, 64*int(time.Millisecond), time.UTC))
if err != nil {
t.Error(err)
return
}
if sf.Time() != 1532180612064 {
t.Error("Failed! Time of Spaceflake is not set to Saturday, July 21, 2018 1:43:32.064 PM")
return
}
t.Log("Success! Time of Spaceflake is correct")
}
func TestSpaceflakeInFuture(t *testing.T) {
node := NewNode(1)
worker := node.NewWorker()
worker.BaseEpoch = 2662196938000 // Tuesday, May 12, 2054 11:08:58 AM GMT
_, err := worker.GenerateSpaceflake()
if err != nil {
t.Log("Success! The generator did not allowed the Spaceflake generation")
return
}
t.Error("Failed! A Spaceflake has been generated with a future base epoch")
}
func TestSpaceflakeWorkerUnique(t *testing.T) {
spaceflakes := map[uint64]*Spaceflake{}
node := NewNode(1)
worker := node.NewWorker()
for i := 0; i < 1000; i++ {
sf, err := worker.GenerateSpaceflake()
if err != nil {
t.Error(err)
return
}
if spaceflakes[sf.ID()] != nil {
t.Error("Failed! A Spaceflake has been generated twice")
return
}
// Here we don't need to sleep because the worker is using an incrementing sequence, and we don't generate more than 4095 spaceflakes per millisecond
spaceflakes[sf.ID()] = sf
}
t.Log("Success! All Spaceflakes are unique")
}
type result struct {
spaceflake *Spaceflake
err error
}
func generate(w *Worker, channel chan *result) {
sf, err := w.GenerateSpaceflake()
res := &result{sf, err}
channel <- res
}
func TestSpaceflakeWorkerGoroutineUnique(t *testing.T) {
spaceflakes := map[uint64]*Spaceflake{}
node := NewNode(1)
worker := node.NewWorker()
for i := 0; i < 1000; i++ {
// That's not a use case for a Goroutine, but I thought let's see if it works with Goroutines
channel := make(chan *result)
go generate(worker, channel)
result := <-channel
if result.err != nil {
t.Error(result.err)
return
}
if spaceflakes[result.spaceflake.ID()] != nil {
t.Errorf("Failed! Spaceflake ID %d already exists", result.spaceflake.ID())
return
}
spaceflakes[result.spaceflake.ID()] = result.spaceflake
}
t.Log("Success! All Spaceflakes are unique")
}
func TestSameTimeStampDifferentBaseEpoch(t *testing.T) {
node := NewNode(1)
worker := node.NewWorker()
sf1, err := worker.GenerateSpaceflake() // Default epoch
if err != nil {
t.Error(err)
return
}
worker.BaseEpoch = 1640995200000 // Saturday, January 1, 2022 12:00:00 AM GMT
sf2, err := worker.GenerateSpaceflake()
if err != nil {
t.Error(err)
return
}
if sf1.Time() == sf2.Time() {
t.Log("Success! Generated same timestamp for different base epoch")
return
}
t.Error("Failed! Generated different timestamps for different base epoch")
}
func TestSpaceflakeGenerateUnique(t *testing.T) {
spaceflakes := map[uint64]*Spaceflake{}
settings := NewGeneratorSettings()
for i := 0; i < 1000; i++ {
sf, err := Generate(settings)
if err != nil {
t.Error(err)
return
}
if spaceflakes[sf.ID()] != nil {
t.Errorf("Failed! Spaceflake ID %d already exists", sf.ID())
return
}
spaceflakes[sf.ID()] = sf
// When using random there is a chance that the sequence will be twice the same due to Go's speed, hence using a worker is better. We wait a millisecond to make sure it's different.
time.Sleep(1 * time.Millisecond)
}
t.Log("Success! All Spaceflakes are unique")
}