-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathid_test.go
124 lines (110 loc) · 2.84 KB
/
id_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
package k4id
import (
"log"
"testing"
)
func benchmarkIDGeneration(idGen Generator, b *testing.B) {
// run the function b.N times
for n := 0; n < b.N; n++ {
idGen.New()
}
}
func BenchmarkIDGeneration(b *testing.B) {
benchmarkIDGeneration(globalIDHost, b)
}
func BenchmarkIDGenerationNano(b *testing.B) {
idGen := DefaultGenerator()
idGen.idLength = 16
idGen.SetTimeSize(TimeGeneratorNano)
benchmarkIDGeneration(idGen, b)
}
func BenchmarkIDGenerationMicro(b *testing.B) {
idGen := DefaultGenerator()
idGen.SetTimeSize(TimeGeneratorMicro)
benchmarkIDGeneration(idGen, b)
}
func BenchmarkIDGenerationMilli(b *testing.B) {
idGen := DefaultGenerator()
idGen.SetTimeSize(TimeGeneratorMilli)
benchmarkIDGeneration(idGen, b)
}
func BenchmarkIDGenerationSmall(b *testing.B) {
idGen := DefaultGenerator()
idGen.SetBaseLength(9)
idGen.SetTimeSize(TimeGeneratorSecond)
log.Println(idGen.New().String())
benchmarkIDGeneration(idGen, b)
}
func TestID(t *testing.T) {
routines := 10
iter := 10000
test := routines * iter
idStream := make(chan ID, test)
for i := 0; i < routines; i++ {
go func(generator Generator) {
for i := 0; i < iter; i++ {
gen := generator.New()
idStream <- gen
if i%500 == 0 {
log.Println(gen.String())
// log.Println(gen.UUID())
}
}
}(DefaultGenerator())
}
generated := map[string]bool{}
lastProcess := 0
for processed := 0; processed < test; processed++ {
gen := <-idStream
if _, found := generated[gen.String()]; found {
t.Fatal("Duplicate ID generated ", gen)
} else {
generated[gen.String()] = true
}
lastProcess = processed
if !FromString(gen.String()).IsValid() {
t.Fatal("Invalid verification")
}
if FromUUID(gen.UUID()).UUID() != gen.UUID() {
t.Fatal("Invalid uuid: ", gen.UUID(), " - ", gen.String())
}
}
log.Println("Processed ", lastProcess+1, " IDs")
}
func TestIDUUID(t *testing.T) {
h := DefaultGenerator()
h.SetBaseLength(19)
for i := 0; i < 10000; i++ {
id := h.New()
originalUUID := id.UUID()
id2 := FromUUID(originalUUID)
log.Println(id.UUID(), " : ", id2.UUID())
log.Println(id.String(), " : ", id2.String())
if id.String() != id2.String() {
log.Fatal("UUID conversion failed")
}
if id.IsValid() && !id2.IsValid() {
log.Fatal("UUID conversion failed checksum")
}
}
}
func TestUUIDImport(t *testing.T) {
uuid := "690482d9-1dda-42b8-b6e1-8df9d16baf05"
log.Println(FromUUID(uuid).String())
}
func TestGenerateWorkspaceID(t *testing.T) {
idGen := DefaultGenerator()
idGen.SetBaseLength(9)
idGen.SetHostID("crs")
idGen.SetTimeSize(TimeGeneratorSecond)
log.Println(idGen.New().String())
}
func TestExtractedTime(t *testing.T) {
id := "0QKSstH3dyZ1711Hx7M"
gen := DefaultGenerator()
gen.SetTimeSize(TimeGeneratorNano)
ti := gen.ExtractTime(id)
if ti.Format("2006-01-02 15:04:05") != "2023-01-01 00:00:00" {
t.Fatal("Invalid time")
}
}