-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathid_generator_sync_test.go
171 lines (156 loc) · 4.13 KB
/
id_generator_sync_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
package id_generator
import (
"github.com/zale144/id-generator/provider"
"sync"
"testing"
)
const OperationIdCategory = "example_uid"
func Test_idGenerator_TakeIDMock(t *testing.T) {
const take = 1000
wantSize := defaultTotalSize - take
// create and initialize id-generator
mockIDProvider := provider.NewMockIDProvider()
g := NewIDGenerator(mockIDProvider)
if err := g.Initialize(OperationIdCategory, 1); err != nil {
t.Errorf("IDGenerator.Initialize() error = %v", err)
return
}
wg := sync.WaitGroup{}
for i := 0; i < take; i++ {
wg.Add(1)
go func(gen *IDGenerator) {
defer wg.Done()
_, err := gen.TakeID(OperationIdCategory)
if err != nil {
t.Errorf("IDGenerator.TakeID() error = %v", err)
return
}
//t.Logf("Got ID: %d", got)
}(g)
}
wg.Wait()
g.Stop()
ids, err := g.PeekIDs(OperationIdCategory)
if err != nil {
t.Errorf("IDGenerator.PeekIDs() error = %v", err)
return
}
size := ids.GetSize()
if wantSize != size {
t.Errorf("IDGenerator.sizeAfter = %v, want %v", size, wantSize)
}
t.Logf("STATE AFTER: %s", ids.String())
}
func Test_idGenerator_TakeIDZooKeeper(t *testing.T) {
// create and initialize id-generator
zookeeperIDProvider, err := provider.NewZooKeeperIDProvider("rnd-r-kafka-0-node-ec2.rnd-env.com:2181")
if err != nil {
t.Errorf("provider.NewZooKeeperIDProvider error = %v", err)
return
}
var lastVersion int32
defer func() {
if err := zookeeperIDProvider.Delete(OperationIdCategory, lastVersion); err != nil {
t.Errorf("zookeeperIDProvider.Delete error = %v", err)
return
}
}()
g := NewIDGenerator(zookeeperIDProvider)
if err := g.Initialize(OperationIdCategory, 1); err != nil {
t.Errorf("IDGenerator.Initialize() error = %v", err)
return
}
set, err := g.PeekIDs(OperationIdCategory)
if err != nil {
t.Errorf("IDGenerator.PeekIDs() error = %v", err)
return
}
initialSize := set.GetSize()
const noGoroutines = 10
const take = 50
wantSize := initialSize - noGoroutines*take
wg := sync.WaitGroup{}
for i := 0; i < noGoroutines; i++ {
wg.Add(1)
go func(rp IDProvider, in int, ver *int32) {
ig := NewIDGenerator(rp)
defer func() {
*ver = ig.Stop()
wg.Done()
}()
for j := 0; j < take; j++ {
_, err := ig.TakeID(OperationIdCategory)
if err != nil {
t.Errorf("IDGenerator.TakeID() error = %v", err)
return
}
//t.Logf("g-%d: Got ID: %d", in, got)
}
}(zookeeperIDProvider, i, &lastVersion)
}
wg.Wait()
ids, err := g.PeekIDs(OperationIdCategory)
if err != nil {
t.Errorf("IDGenerator.PeekIDs() error = %v", err)
return
}
size := ids.GetSize()
if wantSize != size {
t.Errorf("IDGenerator.sizeAfter = %v, want %v", size, wantSize)
}
t.Logf("STATE AFTER: %s", ids.String())
}
func Test_idGenerator_TakeIDRedis(t *testing.T) {
if testing.Short() {
t.Skip("skipping this since Redis is not being used yet")
}
// create and initialize id-generator
redisIDProvider := provider.NewRedisIDProvider(":6379", "", 0)
defer func() {
if err := redisIDProvider.Delete(OperationIdCategory, 0); err != nil {
panic(err)
}
}()
g := NewIDGenerator(redisIDProvider)
if err := g.Initialize(OperationIdCategory, 1); err != nil {
t.Errorf("IDGenerator.Initialize() error = %v", err)
return
}
set, err := g.PeekIDs(OperationIdCategory)
if err != nil {
t.Errorf("IDGenerator.PeekIDs() error = %v", err)
return
}
initialSize := set.GetSize()
const noGoroutines = 10
const take = 50
wantSize := initialSize - noGoroutines*take
wg := sync.WaitGroup{}
for i := 0; i < noGoroutines; i++ {
wg.Add(1)
go func(rp IDProvider, in int) {
defer wg.Done()
ig := NewIDGenerator(rp)
for j := 0; j < take; j++ {
_, err := ig.TakeID(OperationIdCategory)
if err != nil {
t.Errorf("IDGenerator.TakeID() error = %v", err)
return
}
//t.Logf("g-%d: Got ID: %d", in, got)
}
ig.Stop()
}(redisIDProvider, i)
}
wg.Wait()
ids, err := g.PeekIDs(OperationIdCategory)
if err != nil {
t.Errorf("IDGenerator.PeekIDs() error = %v", err)
return
}
size := ids.GetSize()
if wantSize != size {
t.Errorf("IDGenerator.sizeAfter = %v, want %v", size, wantSize)
}
t.Logf("STATE AFTER: %s", ids.String())
}