-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator_test.go
More file actions
143 lines (124 loc) · 2.96 KB
/
generator_test.go
File metadata and controls
143 lines (124 loc) · 2.96 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
package guid
import (
"sync"
"testing"
"time"
)
type testReader struct {
buff []byte
}
func newTestReader(b [8]byte) *testReader {
return &testReader{
buff: b[:],
}
}
func (t *testReader) Read(b []byte) (int, error) {
copy(b, t.buff)
return len(b), nil
}
func TestGenerator(t *testing.T) {
ts1 := int64(1600000000000000000) // 2020-09-13 07:26:40 -0500 EST
gen := stdGenerator{
Fingerprint: 123456,
Random: newTestReader([8]byte{1, 2, 3, 4, 5, 6, 7, 8}),
Now: func() time.Time {
return time.Unix(0, ts1)
},
Counter: 0,
}
g, err := gen.Generate()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// verify fields round-trip correctly
if g.Counter() != 0 {
t.Fatalf("expected counter 0, got %d", g.Counter())
}
if g.Fingerprint() != 123456%maxInt {
t.Fatalf("expected fingerprint %d, got %d", 123456%maxInt, g.Fingerprint())
}
nano := g.Time().UnixNano()
if nano != ts1 {
t.Fatalf("expected time %d, got %d", ts1, nano)
}
// verify counter incremented
if gen.Counter != 1 {
t.Fatalf("expected generator counter to be 1, got %d", gen.Counter)
}
}
func TestConcurrentGeneration(t *testing.T) {
const goroutines = 100
const perGoroutine = 100
results := make(chan GUID, goroutines*perGoroutine)
var wg sync.WaitGroup
for i := 0; i < goroutines; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < perGoroutine; j++ {
g, err := New()
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
results <- g
}
}()
}
wg.Wait()
close(results)
seen := make(map[string]struct{}, goroutines*perGoroutine)
for g := range results {
s := g.String()
if len(s) != byteSize {
t.Fatalf("expected string length %d, got %d: %s", byteSize, len(s), s)
}
if _, exists := seen[s]; exists {
t.Fatalf("duplicate GUID detected: %s", s)
}
seen[s] = struct{}{}
}
if len(seen) != goroutines*perGoroutine {
t.Fatalf("expected %d unique GUIDs, got %d", goroutines*perGoroutine, len(seen))
}
}
func TestCounterWraparound(t *testing.T) {
gen := &stdGenerator{
Fingerprint: 42,
Random: newTestReader([8]byte{1, 2, 3, 4, 5, 6, 7, 8}),
Now: func() time.Time {
return time.Now().UTC()
},
Counter: int32(maxInt - 1),
}
// generate with counter at maxInt-1
g, err := gen.Generate()
if err != nil {
t.Fatal(err)
}
if g.Counter() != int32(maxInt-1) {
t.Fatalf("expected counter %d, got %d", maxInt-1, g.Counter())
}
// counter should have wrapped to 0 (maxInt is out of range for filter)
if gen.Counter != 0 {
t.Fatalf("expected counter to wrap to 0, got %d", gen.Counter)
}
// next generate should use counter 0
g2, err := gen.Generate()
if err != nil {
t.Fatal(err)
}
if g2.Counter() != 0 {
t.Fatalf("expected counter 0 after wrap, got %d", g2.Counter())
}
}
func BenchmarkNew(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = New()
}
}
func BenchmarkNewWithOptions(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = New(WithPrefixBytes('f', 'u'))
}
}