Repository navigation
Expand file tree
/
Copy pathsnowflake_test.go
More file actions
159 lines (136 loc) · 3.11 KB
/
Copy pathsnowflake_test.go
File metadata and controls
159 lines (136 loc) · 3.11 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
package snowflake
import (
"errors"
"fmt"
"sync"
"testing"
"time"
)
func TestGenerate(t *testing.T) {
SetMachineId("arn", 35)
tests := []ID{
Generate(),
Generate(),
Generate(),
ID(9223372036854775807),
}
for _, id := range tests {
fmt.Println("ID: ", id)
fmt.Println("ID: ", int64(id))
encoded, err := id.base54()
if err != nil {
t.Errorf("encoding failed: %v", err)
}
fmt.Println("Encoded: ", encoded)
fmt.Println("Time: ", time.UnixMilli(id.Time()).UTC().String())
fmt.Println("MachineId: ", id.MachineId())
fmt.Println("MachineSequence: ", id.MachineSequence())
fmt.Println("---")
}
}
func TestMachineId(t *testing.T) {
tests := []struct {
region string
num int64
}{
{"fra", 35},
{"lax", 4},
// {"unk", 0},
// {"phx", -1},
}
for _, test := range tests {
t.Run(fmt.Sprintf("Test_MachineID_%s_%d", test.region, test.num), func(t *testing.T) {
SetMachineId(test.region, test.num)
fmt.Println("Machine ID: ", machineId)
fmt.Printf("Binary: %09b\n", machineId)
})
}
}
func TestGenerateExceedSequence(t *testing.T) {
var wg sync.WaitGroup
for j := 0; j < 4; j++ {
wg.Add(1)
go func() {
for i := 0; i < 5000; i++ {
id := Generate()
seq := id.MachineSequence()
// TODO: Define a more useful test.
if seq > 4092 || seq < 3 {
fmt.Printf("[%d]: Sequence = %d\n", j, seq)
}
}
wg.Done()
}()
}
wg.Wait()
}
func TestMachineIdRace(t *testing.T) {
SetMachineId("eu-west-1", 1)
done := make(chan struct{})
go func() {
for i := 0; i < 1000; i++ {
Generate()
}
close(done)
}()
for i := 0; i < 1000; i++ {
SetMachineId("eu-west-1", 2)
}
<-done
}
// 244.0 ns/op
func BenchmarkGenerate(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Generate()
}
}
//
// Marshaler interface implementation
//
func TestMarshalJSON(t *testing.T) {
tests := []struct {
id ID
verify string
}{
{ID(123123), `"6vF"`},
{ID(123123123), `"nHW1a"`},
{ID(1820096636282474496), `"efUzLtM5yvu"`},
{ID(9223372036854775807), `"EZNmktHEz5H"`},
{ID(305023354946072576), `"8uyZY2sj3re"`},
}
for _, test := range tests {
t.Run(fmt.Sprintf("Test_Marshal_%d", int64(test.id)), func(t *testing.T) {
bytes, err := test.id.MarshalJSON()
if err != nil {
t.Errorf("marshal failed: %v", err)
} else if string(bytes) != test.verify {
t.Errorf("got '%v', want '%v'", string(bytes), test.verify)
}
})
}
}
func TestUnmarshalJSON(t *testing.T) {
tests := []struct {
json string
verify ID
err error
}{
{`"6vF"`, ID(123123), nil},
{`"nHW1a"`, ID(123123123), nil},
{`"efUzLtM5yvu"`, ID(1820096636282474496), nil},
{`"EZNmktHEz5H"`, ID(9223372036854775807), nil},
{`"8HH7MXkTRtr"`, ID(310311215280041986), nil},
{`6vF`, Invalid, &ErrorInvalidJson},
}
for _, test := range tests {
t.Run(fmt.Sprintf("Test_Unmarshal_%s", test.json), func(t *testing.T) {
var id ID
err := id.UnmarshalJSON([]byte(test.json))
if err != nil && !errors.Is(err, test.err) {
t.Errorf("unexpected unmarshal error: %v", err)
} else if id != test.verify {
t.Errorf("got '%v', want '%v'", id, test.verify)
}
})
}
}