-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesnowflake_test.go
172 lines (153 loc) · 3.95 KB
/
esnowflake_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
package esnowflake
import (
"fmt"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestConfigWithValidIP_ReturnsConfig(t *testing.T) {
config := New("192.168.1.1", 1, 2, 3)
if config == nil {
t.Error("Expected non-nil config")
}
}
func TestConfigWithInvalidIP_Panics(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("Expected panic for invalid IP")
}
}()
New("invalid_ip", 1, 2, 3)
}
func TestConfigWithValidIP_SetsMaskedIP(t *testing.T) {
config := New("192.168.1.2", 1, 2, 3)
expectedIP := []byte{168 ^ 1, 1 ^ 2, 2 ^ 3}
for i, b := range expectedIP {
if config.ip[i] != b {
t.Errorf("Expected masked IP byte %d to be %d, got %d", i, b, config.ip[i])
}
}
}
func TestConfigWithValidIP_GetIp(t *testing.T) {
config := New("192.168.1.2", 1, 2, 3)
encode := config.GenerateByRandom()
ip := config.GetIP(encode)
assert.Equal(t, "xxx.168.1.2", ip)
}
func TestGenerateByRandom_ReturnsUniqueIDs(t *testing.T) {
config := New("192.168.1.1", 1, 2, 3)
id1 := config.GenerateByRandom()
id2 := config.GenerateByRandom()
if id1 == id2 {
t.Errorf("Expected unique IDs, but got %s and %s", id1, id2)
}
}
func TestGenerateByRandom_HandlesPoolRefill(t *testing.T) {
config := New("192.168.1.1", 1, 2, 3)
for i := 0; i < 100; i++ {
config.GenerateByRandom()
}
}
func TestGenerateByRandom_ConcurrencyNotEqual(t *testing.T) {
config := New("192.168.1.1", 1, 2, 3)
testLength := 1000000
output := sync.Map{}
// 测试并发生成随机数,是否重复
wg := sync.WaitGroup{}
wg.Add(testLength)
for i := 0; i < testLength; i++ {
go func() {
id := config.GenerateByRandom()
_, flag := output.Load(id)
if flag {
t.Errorf("Expected unique IDs, but got %s and %s", id, id)
}
output.Store(id, struct{}{})
wg.Done()
}()
}
wg.Wait()
}
func TestGenerateBySequence_ConcurrencyNotEqual(t *testing.T) {
config := New("192.168.1.1", 1, 2, 3)
testLength := 1000000
output := sync.Map{}
// 测试并发生成随机数,是否重复
wg := sync.WaitGroup{}
wg.Add(testLength)
for i := 0; i < testLength; i++ {
go func() {
id := config.GenerateBySequence()
_, flag := output.Load(id)
if flag {
t.Errorf("Expected unique IDs, but got %s and %s", id, id)
}
output.Store(id, struct{}{})
wg.Done()
}()
}
wg.Wait()
}
func TestGenerateByRandom_GetTime(t *testing.T) {
config := New("192.168.1.1", 1, 2, 3)
encode := config.GenerateByRandom()
encodeTime := config.GetTime(encode)
fmt.Printf("time--------------->"+"%+v\n", encodeTime)
}
func TestGenerateByRandom_HandlesLocking(t *testing.T) {
config := New("192.168.1.1", 1, 2, 3)
done := make(chan bool)
go func() {
config.GenerateByRandom()
done <- true
}()
select {
case <-done:
case <-time.After(1 * time.Second):
t.Error("GenerateByRandom did not return within 1 second")
}
}
func TestGenerateBySequence_ReturnsUniqueIDs(t *testing.T) {
config := New("192.168.1.1", 1, 2, 3)
id1 := config.GenerateBySequence()
id2 := config.GenerateBySequence()
if id1 == id2 {
t.Errorf("Expected unique IDs, but got %s and %s", id1, id2)
}
}
func TestGenerateBySequence_HandlesSequenceOverflow(t *testing.T) {
config := New("192.168.1.1", 1, 2, 3)
config.timestamp = time.Now().UnixNano() / 1000000
config.sequence = sequenceMask
id := config.GenerateBySequence()
if id == "" {
t.Error("Expected non-empty ID on sequence overflow")
}
}
func TestGenerateBySequence_HandlesLocking(t *testing.T) {
config := New("192.168.1.1", 1, 2, 3)
done := make(chan bool)
go func() {
config.GenerateBySequence()
done <- true
}()
select {
case <-done:
case <-time.After(1 * time.Second):
t.Error("GenerateBySequence did not return within 1 second")
}
}
func BenchmarkRandomAndSequence(b *testing.B) {
obj := New("192.168.1.1", 1, 2, 3)
b.Run("Random", func(b *testing.B) {
for i := 0; i < b.N; i++ {
obj.GenerateByRandom()
}
})
b.Run("Sequence", func(b *testing.B) {
for i := 0; i < b.N; i++ {
obj.GenerateBySequence()
}
})
}