-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsleekid_test.go
193 lines (167 loc) · 5.42 KB
/
sleekid_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
package sleekid
import (
"regexp"
"sync"
"testing"
"time"
"github.com/google/uuid"
"github.com/rs/xid"
"gotest.tools/assert"
)
func BenchmarkNew(b *testing.B) {
gen := NewGenerator(GeneratorInit{ChecksumToken: 30, RandomDigitsLength: 15})
prefix := "usr"
b.ResetTimer()
for i := 0; i < b.N; i++ {
gen.New(prefix)
}
}
func BenchmarkPrefix(b *testing.B) {
gen := NewGenerator(GeneratorInit{ChecksumToken: 30, RandomDigitsLength: 15, ChecksumLength: 4, Delimiter: '_'})
b.ResetTimer()
for i := 0; i < b.N; i++ {
gen.Prefix([]byte("usr_ajijoi"))
}
}
func BenchmarkTimestamp(b *testing.B) {
gen := NewGenerator(GeneratorInit{ChecksumToken: 30, RandomDigitsLength: 2, TimestampLength: 5})
id, _ := gen.New("usr")
b.ResetTimer()
for i := 0; i < b.N; i++ {
gen.Timestamp(id)
}
}
func BenchmarkValidate(b *testing.B) {
gen := NewGenerator(GeneratorInit{ChecksumToken: 30, RandomDigitsLength: 15})
prefix := "usr"
id, _ := gen.New(prefix)
b.ResetTimer()
for i := 0; i < b.N; i++ {
gen.Validate(id)
}
}
func BenchmarkValidateWithPrefix(b *testing.B) {
gen := NewGenerator(GeneratorInit{ChecksumToken: 30, RandomDigitsLength: 15})
prefix := "usr"
id, _ := gen.New(prefix)
b.ResetTimer()
for i := 0; i < b.N; i++ {
gen.ValidateWithPrefix(prefix, id)
}
}
func BenchmarkNewUUID(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
uuid.NewRandom()
}
}
func BenchmarkNewXid(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
xid.New()
}
}
func TestNew(t *testing.T) {
gen := NewGenerator(GeneratorInit{ChecksumToken: 30, RandomDigitsLength: 10})
id, _ := gen.New("usr")
assert.Equal(t, 3+1+5+10+2, len(id))
assert.Assert(t, regexp.MustCompile(`^[a-z]+_[A-Za-z0-9]+$`).MatchString(string(id)))
}
func TestPrefix(t *testing.T) {
gen := NewGenerator(GeneratorInit{ChecksumToken: 30, RandomDigitsLength: 10})
id, _ := gen.New("usr")
assert.Equal(t, "usr", gen.Prefix(id))
}
func TestPrefix_shouldReturnEmpty(t *testing.T) {
gen := NewGenerator(GeneratorInit{ChecksumToken: 30, RandomDigitsLength: 10})
assert.Equal(t, "", gen.Prefix([]byte("ajijoi")))
}
func TestTimestamp(t *testing.T) {
gen := NewGenerator(GeneratorInit{ChecksumToken: 30, RandomDigitsLength: 2, TimestampLength: 5})
id, _ := gen.New("usr")
now := time.Unix(time.Now().Unix(), 0)
timestmap := gen.Timestamp(id)
assert.Equal(t, now, timestmap)
}
func TestTimestampWithCustomTimestampLength(t *testing.T) {
gen := NewGenerator(GeneratorInit{ChecksumToken: 30, RandomDigitsLength: 2, TimestampLength: 6})
id, _ := gen.New("usr")
now := time.Unix(time.Now().Unix(), 0)
timestmap := gen.Timestamp(id)
assert.Equal(t, now, timestmap)
}
func TestValidate(t *testing.T) {
gen := NewGenerator(GeneratorInit{ChecksumToken: 30, RandomDigitsLength: 15})
id, _ := gen.New("usr")
assert.Equal(t, true, gen.Validate(id))
assert.Equal(t, false, gen.Validate([]byte("aoj_aoisjdfoi")))
assert.Equal(t, false, gen.Validate([]byte("asf")))
}
func TestValidateWithPrefix_shouldFailWhenPrefixIsDifferent(t *testing.T) {
gen := NewGenerator(GeneratorInit{ChecksumToken: 30, RandomDigitsLength: 15})
id, _ := gen.New("usr")
assert.Equal(t, false, gen.ValidateWithPrefix("usr2", id))
}
func TestValidateWithPrefix_shouldFailWhenIdIsTooShort(t *testing.T) {
gen := NewGenerator(GeneratorInit{ChecksumToken: 30, RandomDigitsLength: 15})
assert.Equal(t, false, gen.ValidateWithPrefix("usr", []byte("a")))
assert.Equal(t, false, gen.ValidateWithPrefix("usr", []byte("usr_")))
assert.Equal(t, false, gen.ValidateWithPrefix("usr", []byte("usr_aos")))
}
func TestValidateWithPrefix_shouldFailWhenTokenIsDifferent(t *testing.T) {
gen := NewGenerator(GeneratorInit{ChecksumToken: 30, RandomDigitsLength: 15})
id, _ := gen.New("usr")
gen2 := NewGenerator(GeneratorInit{ChecksumToken: 100, RandomDigitsLength: 15})
assert.Equal(t, false, gen2.ValidateWithPrefix("usr", id))
}
func TestValidateWithPrefix_shouldWorkWhenAllIsGood(t *testing.T) {
gen := NewGenerator(GeneratorInit{ChecksumToken: 30, RandomDigitsLength: 15})
id, _ := gen.New("usr")
gen2 := NewGenerator(GeneratorInit{ChecksumToken: 30, RandomDigitsLength: 15})
assert.Equal(t, true, gen2.ValidateWithPrefix("usr", id))
}
func TestNewConcurrentGeneration(t *testing.T) {
const numGoroutines = 150
const numIDs = 2000
gen := NewGenerator(GeneratorInit{ChecksumToken: 30, RandomDigitsLength: 15})
seen := sync.Map{}
var wg sync.WaitGroup
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < numIDs; j++ {
id, _ := gen.New("test")
_, loaded := seen.LoadOrStore(string(id), true)
assert.Equal(t, false, loaded, "Duplicate ID generated")
}
}()
}
wg.Wait()
}
func TestCustomChecksumLength(t *testing.T) {
gen := NewGenerator(GeneratorInit{
ChecksumToken: 100,
RandomDigitsLength: 15,
ChecksumLength: 4,
TimestampLength: 5,
Delimiter: '_',
})
id, _ := gen.New("usr")
assert.Equal(t, true, gen.Validate(id))
assert.Equal(t, 3+1+5+15+4, len(id))
assert.Assert(t, regexp.MustCompile(`^[a-z]+_[A-Za-z0-9]+$`).MatchString(string(id)))
}
func TestCustomTimestampLength(t *testing.T) {
gen := NewGenerator(GeneratorInit{
ChecksumToken: 100,
RandomDigitsLength: 15,
ChecksumLength: 2,
TimestampLength: 6,
Delimiter: '_',
})
id, _ := gen.New("usr")
assert.Equal(t, true, gen.Validate(id))
assert.Equal(t, 3+1+6+15+2, len(id))
assert.Assert(t, regexp.MustCompile(`^[a-z]+_[A-Za-z0-9]+$`).MatchString(string(id)))
}