-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhyperflake_test.go
More file actions
214 lines (190 loc) · 6.05 KB
/
hyperflake_test.go
File metadata and controls
214 lines (190 loc) · 6.05 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
package hyperflake
import (
"sync"
"testing"
)
// TestDecodeID verifies that known IDs decode to the expected components.
func TestDecodeID(t *testing.T) {
hf := NewHyperflakeConfig(0, 0)
testCases := []struct {
name string
id int64
expectedSignbit int
expectedTimestamp int64
expectedDatacenterID int
expectedMachineID int
expectedSequenceNumber int
}{
{
name: "zero datacenter and machine ID",
id: 3282575599297626112,
expectedSignbit: 0,
expectedTimestamp: 1729311810178,
expectedDatacenterID: 0,
expectedMachineID: 0,
expectedSequenceNumber: 0,
},
{
name: "non-zero datacenter, machine ID and sequence number",
id: 3273974649684016911,
expectedSignbit: 0,
expectedTimestamp: 1727261183992,
expectedDatacenterID: 6,
expectedMachineID: 11,
expectedSequenceNumber: 3855,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
decoded, err := hf.DecodeID(tc.id)
if err != nil {
t.Fatalf("DecodeID(%d) returned error: %v", tc.id, err)
}
if decoded.Signbit != tc.expectedSignbit {
t.Errorf("Signbit = %d; want %d", decoded.Signbit, tc.expectedSignbit)
}
if decoded.Timestamp != tc.expectedTimestamp {
t.Errorf("Timestamp = %d; want %d", decoded.Timestamp, tc.expectedTimestamp)
}
if decoded.DatacenterID != tc.expectedDatacenterID {
t.Errorf("DatacenterID = %d; want %d", decoded.DatacenterID, tc.expectedDatacenterID)
}
if decoded.MachineID != tc.expectedMachineID {
t.Errorf("MachineID = %d; want %d", decoded.MachineID, tc.expectedMachineID)
}
if decoded.SequenceNumber != tc.expectedSequenceNumber {
t.Errorf("SequenceNumber = %d; want %d", decoded.SequenceNumber, tc.expectedSequenceNumber)
}
})
}
}
// TestGenerateAndDecode verifies that a generated ID round-trips correctly through DecodeID.
func TestGenerateAndDecode(t *testing.T) {
const datacenterID = 12
const machineID = 7
config := NewHyperflakeConfig(datacenterID, machineID)
id, err := config.GenerateHyperflakeID()
if err != nil {
t.Fatalf("GenerateHyperflakeID() error: %v", err)
}
if id <= 0 {
t.Fatalf("GenerateHyperflakeID() returned non-positive ID: %d", id)
}
decoded, err := config.DecodeID(id)
if err != nil {
t.Fatalf("DecodeID(%d) error: %v", id, err)
}
if decoded.DatacenterID != datacenterID {
t.Errorf("DatacenterID = %d; want %d", decoded.DatacenterID, datacenterID)
}
if decoded.MachineID != machineID {
t.Errorf("MachineID = %d; want %d", decoded.MachineID, machineID)
}
if decoded.ID != id {
t.Errorf("ID = %d; want %d", decoded.ID, id)
}
}
// TestConcurrency verifies that GenerateHyperflakeID is safe for concurrent use
// and produces no duplicate IDs across goroutines.
func TestConcurrency(t *testing.T) {
const goroutines = 100
const idsPerGoroutine = 100
config := NewHyperflakeConfig(1, 1)
ids := make(chan int64, goroutines*idsPerGoroutine)
var wg sync.WaitGroup
for i := 0; i < goroutines; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < idsPerGoroutine; j++ {
id, err := config.GenerateHyperflakeID()
if err != nil {
t.Errorf("GenerateHyperflakeID() error: %v", err)
return
}
ids <- id
}
}()
}
wg.Wait()
close(ids)
seen := make(map[int64]struct{}, goroutines*idsPerGoroutine)
for id := range ids {
if _, exists := seen[id]; exists {
t.Errorf("duplicate ID generated: %d", id)
}
seen[id] = struct{}{}
}
}
// TestClockBackwards verifies that GenerateHyperflakeID returns an error
// if the clock appears to move backwards.
func TestClockBackwards(t *testing.T) {
config := NewHyperflakeConfig(0, 0)
// Simulate a future lastTimestamp to trigger the clock-backwards guard.
config.lastTimestamp = 1<<41 - 1 // max possible timestamp
_, err := config.GenerateHyperflakeID()
if err == nil {
t.Fatal("expected clock-backwards error, got nil")
}
}
// TestCustomEpoch verifies that NewHyperflakeConfigWithEpoch uses the provided epoch
// and that the decoded timestamp reflects it correctly.
func TestCustomEpoch(t *testing.T) {
customEpoch := int64(1_000_000_000_000) // arbitrary epoch in ms
config := NewHyperflakeConfigWithEpoch(1, 1, customEpoch)
id, err := config.GenerateHyperflakeID()
if err != nil {
t.Fatalf("GenerateHyperflakeID() error: %v", err)
}
decoded, err := config.DecodeID(id)
if err != nil {
t.Fatalf("DecodeID(%d) error: %v", id, err)
}
// Timestamp field should equal TimestampSinceEpoch + customEpoch.
if decoded.Timestamp != decoded.TimestampSinceEpoch+customEpoch {
t.Errorf("Timestamp = %d; want TimestampSinceEpoch(%d) + epoch(%d) = %d",
decoded.Timestamp, decoded.TimestampSinceEpoch, customEpoch, decoded.TimestampSinceEpoch+customEpoch)
}
}
// TestSettersGetters verifies Set/Get methods for datacenter and machine IDs.
func TestSettersGetters(t *testing.T) {
config := NewHyperflakeConfig(0, 0)
config.SetDatacenterID(15)
if got := config.GetDatacenterID(); got != 15 {
t.Errorf("GetDatacenterID() = %d; want 15", got)
}
config.SetMachineID(31)
if got := config.GetMachineID(); got != 31 {
t.Errorf("GetMachineID() = %d; want 31", got)
}
// Verify the updated IDs are encoded in generated IDs.
id, err := config.GenerateHyperflakeID()
if err != nil {
t.Fatalf("GenerateHyperflakeID() error: %v", err)
}
decoded, err := config.DecodeID(id)
if err != nil {
t.Fatalf("DecodeID error: %v", err)
}
if decoded.DatacenterID != 15 {
t.Errorf("encoded DatacenterID = %d; want 15", decoded.DatacenterID)
}
if decoded.MachineID != 31 {
t.Errorf("encoded MachineID = %d; want 31", decoded.MachineID)
}
}
func BenchmarkGenerateHyperflakeID(b *testing.B) {
config := NewHyperflakeConfig(3, 7)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = config.GenerateHyperflakeID()
}
}
func BenchmarkDecodeID(b *testing.B) {
config := NewHyperflakeConfig(3, 7)
id, _ := config.GenerateHyperflakeID()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = config.DecodeID(id)
}
}