-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathid_generator_test.go
More file actions
239 lines (193 loc) · 5.6 KB
/
Copy pathid_generator_test.go
File metadata and controls
239 lines (193 loc) · 5.6 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package wildcat
import (
"math"
"sync"
"sync/atomic"
"testing"
"time"
)
func TestNewIDGenerator(t *testing.T) {
g := newIDGenerator()
if g == nil {
t.Fatal("NewIDGenerator returned nil")
}
if g.lastID != 0 {
t.Fatal("lastID was not initialized")
}
}
func TestNextID_Unique(t *testing.T) {
g := newIDGenerator()
id1 := g.nextID()
id2 := g.nextID()
if id1 == id2 {
t.Fatal("NextID did not generate unique IDs")
}
}
func TestNextID_Monotonic(t *testing.T) {
g := newIDGenerator()
id1 := g.nextID()
id2 := g.nextID()
if id2 <= id1 {
t.Fatalf("NextID did not ensure monotonicity: id1=%d, id2=%d", id1, id2)
}
}
func TestNextID_ThreadSafety(t *testing.T) {
g := newIDGenerator()
const numGoroutines = 100
const idsPerGoroutine = 100
var wg sync.WaitGroup
ids := make(chan int64, numGoroutines*idsPerGoroutine)
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < idsPerGoroutine; j++ {
ids <- g.nextID()
}
}()
}
wg.Wait()
close(ids)
// Check for uniqueness
idSet := make(map[int64]struct{})
for id := range ids {
if _, exists := idSet[id]; exists {
t.Fatalf("Duplicate ID detected: %d", id)
}
idSet[id] = struct{}{}
}
}
func TestTimestampIDGenerator_ConcurrentGap(t *testing.T) {
g := newIDGeneratorWithTimestamp()
// Simulate a large time gap by setting lastID to a future timestamp
futureTime := time.Now().Add(1 * time.Hour).UnixNano()
atomic.StoreInt64(&g.lastID, futureTime)
const numGoroutines = 50
const idsPerGoroutine = 20
var wg sync.WaitGroup
ids := make(chan int64, numGoroutines*idsPerGoroutine)
// Launch concurrent goroutines immediately after setting future time
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < idsPerGoroutine; j++ {
ids <- g.nextID()
}
}()
}
wg.Wait()
close(ids)
// Collect and sort all IDs
var allIDs []int64
idSet := make(map[int64]struct{})
for id := range ids {
// Check for uniqueness
if _, exists := idSet[id]; exists {
t.Fatalf("Duplicate ID detected: %d", id)
}
idSet[id] = struct{}{}
allIDs = append(allIDs, id)
}
// Verify we got the expected number of IDs
expectedCount := numGoroutines * idsPerGoroutine
if len(allIDs) != expectedCount {
t.Fatalf("Expected %d IDs, got %d", expectedCount, len(allIDs))
}
// Verify monotonicity by checking each ID is greater than the previous
// Since concurrent generation might not be in perfect order, we'll check
// that all IDs are >= the original future timestamp
for i, id := range allIDs {
if id < futureTime {
t.Fatalf("ID %d at position %d is less than the initial future time %d",
id, i, futureTime)
}
}
t.Logf("Generated %d unique, monotonic timestamp IDs despite large time gap", len(allIDs))
t.Logf("Time gap was: %v", time.Duration(futureTime-time.Now().UnixNano()))
t.Logf("First ID: %d, Last ID: %d", allIDs[0], allIDs[len(allIDs)-1])
}
func TestTimestampIDGenerator_RapidConcurrentGeneration(t *testing.T) {
g := newIDGeneratorWithTimestamp()
const numGoroutines = 100
const idsPerGoroutine = 50
var wg sync.WaitGroup
ids := make(chan int64, numGoroutines*idsPerGoroutine)
// Use a barrier to make all goroutines start as close to simultaneously as possible
startBarrier := make(chan struct{})
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func() {
defer wg.Done()
<-startBarrier // Wait for the signal to start
for j := 0; j < idsPerGoroutine; j++ {
ids <- g.nextID()
}
}()
}
// Release all goroutines at once
close(startBarrier)
wg.Wait()
close(ids)
// Collect all IDs and verify uniqueness
idSet := make(map[int64]struct{})
var lastID int64 = 0
count := 0
for id := range ids {
count++
// Check for uniqueness
if _, exists := idSet[id]; exists {
t.Fatalf("Duplicate timestamp ID detected: %d", id)
}
idSet[id] = struct{}{}
// Track the highest ID we've seen
if id > lastID {
lastID = id
}
}
expectedCount := numGoroutines * idsPerGoroutine
if count != expectedCount {
t.Fatalf("Expected %d IDs, got %d", expectedCount, count)
}
t.Logf("Successfully generated %d unique timestamp IDs under high concurrency", count)
t.Logf("Highest timestamp ID: %d", lastID)
}
func TestIDGenerator_OverflowBehavior(t *testing.T) {
// Test int64 generator overflow
g := newIDGenerator()
atomic.StoreInt64(&g.lastID, math.MaxInt64)
nextID := g.nextID()
if nextID != 1 {
t.Fatalf("Int64 generator should reset to 1 on overflow, got %d", nextID)
}
// Test timestamp generator overflow
gTimestamp := newIDGeneratorWithTimestamp()
atomic.StoreInt64(&gTimestamp.lastID, math.MaxInt64)
nextTimestampID := gTimestamp.nextID()
now := time.Now().UnixNano()
// Should reset to current time (with some tolerance for execution time)
if nextTimestampID < now-1000000 || nextTimestampID > now+1000000 {
t.Fatalf("Timestamp generator should reset to current time on overflow, got %d, expected around %d",
nextTimestampID, now)
}
}
func TestIDGenerator_ClockBackwards(t *testing.T) {
g := newIDGeneratorWithTimestamp()
// Get a timestamp ID
g.nextID()
// Manually set lastID to a higher value to simulate clock going backwards
futureTime := time.Now().Add(1 * time.Second).UnixNano()
atomic.StoreInt64(&g.lastID, futureTime)
// Next ID should be futureTime + 1 (not current time)
id2 := g.nextID()
if id2 != futureTime+1 {
t.Fatalf("Expected ID to be %d when clock goes backwards, got %d",
futureTime+1, id2)
}
// Subsequent calls should continue incrementing
id3 := g.nextID()
if id3 != id2+1 {
t.Fatalf("Expected monotonic increment after clock backwards, got %d after %d",
id3, id2)
}
}