-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidgen_test.go
More file actions
196 lines (158 loc) · 4.35 KB
/
Copy pathidgen_test.go
File metadata and controls
196 lines (158 loc) · 4.35 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
package debugmonitor
import (
"sync"
"testing"
"time"
)
func TestIDGenerator_Generate(t *testing.T) {
gen := NewIDGenerator()
// Generate a single ID
id := gen.Generate()
if id <= 0 {
t.Errorf("Expected positive ID, got %d", id)
}
}
func TestIDGenerator_UniqueIDs(t *testing.T) {
gen := NewIDGenerator()
count := 10000
ids := make(map[int64]bool, count)
// Generate multiple IDs and check uniqueness
for i := 0; i < count; i++ {
id := gen.Generate()
if ids[id] {
t.Errorf("Duplicate ID generated: %d", id)
}
ids[id] = true
}
if len(ids) != count {
t.Errorf("Expected %d unique IDs, got %d", count, len(ids))
}
}
func TestIDGenerator_Ordering(t *testing.T) {
gen := NewIDGenerator()
prevID := int64(0)
// Generate IDs and verify they are increasing
for i := 0; i < 1000; i++ {
id := gen.Generate()
if id <= prevID {
t.Errorf("IDs not in ascending order: prev=%d, current=%d", prevID, id)
}
prevID = id
}
}
func TestIDGenerator_Concurrent(t *testing.T) {
gen := NewIDGenerator()
count := 10000
goroutines := 10
idsPerGoroutine := count / goroutines
mu := sync.Mutex{}
ids := make(map[int64]bool, count)
var wg sync.WaitGroup
wg.Add(goroutines)
// Generate IDs concurrently
for g := 0; g < goroutines; g++ {
go func() {
defer wg.Done()
for i := 0; i < idsPerGoroutine; i++ {
id := gen.Generate()
mu.Lock()
if ids[id] {
t.Errorf("Duplicate ID generated in concurrent test: %d", id)
}
ids[id] = true
mu.Unlock()
}
}()
}
wg.Wait()
if len(ids) != count {
t.Errorf("Expected %d unique IDs, got %d", count, len(ids))
}
}
func TestExtractTimestamp(t *testing.T) {
gen := NewIDGenerator()
beforeGen := time.Now()
id := gen.Generate()
afterGen := time.Now()
extractedTime := ExtractTimestamp(id)
// The extracted time should be close to the generation time (within 2ms margin)
// We use a margin because the timestamp is stored at millisecond precision
margin := 2 * time.Millisecond
if extractedTime.Before(beforeGen.Add(-margin)) || extractedTime.After(afterGen.Add(margin)) {
t.Errorf("Extracted timestamp %v is not within expected range [%v, %v]",
extractedTime, beforeGen.Add(-margin), afterGen.Add(margin))
}
}
func TestExtractSequence(t *testing.T) {
gen := NewIDGenerator()
// Generate multiple IDs in quick succession (likely same millisecond)
ids := make([]int64, 100)
for i := 0; i < 100; i++ {
id := gen.Generate()
ids[i] = id
}
// Check that sequence numbers are valid
for i, id := range ids {
seq := ExtractSequence(id)
if seq < 0 || seq > maxSequence {
t.Errorf("ID %d has invalid sequence number: %d (should be 0-%d)",
id, seq, maxSequence)
}
// If we have at least 2 IDs with the same timestamp,
// their sequences should be different
if i > 0 {
prevTimestamp := ExtractTimestamp(ids[i-1])
currTimestamp := ExtractTimestamp(id)
// If timestamps are the same (within 1ms), sequences should differ
if currTimestamp.Sub(prevTimestamp).Abs() < time.Millisecond {
prevSeq := ExtractSequence(ids[i-1])
currSeq := ExtractSequence(id)
if prevSeq >= currSeq {
t.Errorf("Sequences not incrementing properly within same ms: prev=%d, curr=%d",
prevSeq, currSeq)
}
}
}
}
}
func TestIDGenerator_BitStructure(t *testing.T) {
gen := NewIDGenerator()
id := gen.Generate()
// Verify that the ID is positive (sign bit is 0)
if id < 0 {
t.Errorf("Generated ID is negative: %d", id)
}
// Extract and verify components
timestamp := id >> timestampShift
sequence := id & maxSequence
// Timestamp should be positive and reasonable
if timestamp < 0 {
t.Errorf("Extracted timestamp is negative: %d", timestamp)
}
// Sequence should be within valid range
if sequence < 0 || sequence > maxSequence {
t.Errorf("Sequence out of range: %d (max: %d)", sequence, maxSequence)
}
// Reconstruct the ID from components
reconstructed := (timestamp << timestampShift) | sequence
if reconstructed != id {
t.Errorf("Failed to reconstruct ID: original=%d, reconstructed=%d",
id, reconstructed)
}
}
func BenchmarkIDGenerator_Generate(b *testing.B) {
gen := NewIDGenerator()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = gen.Generate()
}
}
func BenchmarkIDGenerator_GenerateParallel(b *testing.B) {
gen := NewIDGenerator()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_ = gen.Generate()
}
})
}