-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgpq_base_test.go
299 lines (243 loc) · 6.3 KB
/
gpq_base_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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package gpq_test
import (
"log"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/JustinTimperio/gpq"
"github.com/JustinTimperio/gpq/schema"
)
func TestOrder(t *testing.T) {
var (
total uint = 1_000_000
syncToDisk bool = false
lazySync bool = false
maxBuckets uint = 10
)
defaultMessageOptions := schema.EnqueueOptions{
ShouldEscalate: false,
EscalationRate: time.Duration(time.Second),
CanTimeout: false,
Timeout: time.Duration(time.Second * 1),
}
opts := schema.GPQOptions{
MaxPriority: maxBuckets,
DiskCacheEnabled: syncToDisk,
DiskCachePath: "/tmp/gpq/test-order",
DiskCacheCompression: false,
DiskEncryptionEnabled: false,
DiskEncryptionKey: []byte("1234567890"),
LazyDiskCacheChannelSize: 1_000_000,
DiskWriteDelay: time.Duration(time.Second),
LazyDiskCacheEnabled: lazySync,
LazyDiskBatchSize: 10_000,
}
_, queue, err := gpq.NewGPQ[string](opts)
if err != nil {
log.Fatalln(err)
}
var order = make(map[uint][]schema.Item[string])
// Add the messages to the queue in order
for i := uint(0); i < total; i++ {
p := i % maxBuckets
item := schema.NewItem(p, randomString(30), defaultMessageOptions)
err := queue.Enqueue(item)
if err != nil {
log.Fatalln(err)
}
_, ok := order[p]
if !ok {
order[p] = make([]schema.Item[string], 0)
}
order[p] = append(order[p], item)
}
// Pull off the queue and verify order
for i := uint(0); i < total; i++ {
item, err := queue.Dequeue()
if err != nil {
log.Fatalln(err)
}
orderMap := order[item.Priority]
if orderMap[0].Data != item.Data {
log.Fatalln("Order mismatch", orderMap[0], item)
}
order[item.Priority] = orderMap[1:]
}
queue.Close()
}
func TestPrioritize(t *testing.T) {
defaultMessageOptions := schema.EnqueueOptions{
ShouldEscalate: true,
EscalationRate: time.Duration(time.Second),
CanTimeout: true,
Timeout: time.Duration(time.Second * 10),
}
ptest := func(tm uint, sd bool, ls bool, mb uint) {
opts := schema.GPQOptions{
MaxPriority: mb,
DiskCacheEnabled: sd,
DiskCachePath: "/tmp/gpq/test-prioritize",
DiskCacheCompression: false,
DiskEncryptionEnabled: false,
DiskEncryptionKey: []byte("12345678901234567890123456789012"),
LazyDiskCacheChannelSize: tm / 2,
DiskWriteDelay: time.Duration(time.Second),
LazyDiskCacheEnabled: ls,
LazyDiskBatchSize: 10_000,
}
_, queue, err := gpq.NewGPQ[uint](opts)
if err != nil {
log.Fatalln(err)
}
var (
escalated uint64
removed uint64
received uint64
)
var wg sync.WaitGroup
shutdown := make(chan struct{})
wg.Add(1)
go func() {
defer wg.Done()
ticker := time.NewTicker(time.Second * 1)
forloop:
for {
select {
case <-ticker.C:
r, e, err := queue.Prioritize()
if err != nil {
log.Fatalln(err)
}
atomic.AddUint64(&removed, uint64(r))
atomic.AddUint64(&escalated, uint64(e))
t.Log("Received:", atomic.LoadUint64(&received), "Removed:", atomic.LoadUint64(&removed), "Escalated:", atomic.LoadUint64(&escalated))
case <-shutdown:
break forloop
}
}
}()
wg.Add(1)
go func() {
defer wg.Done()
for j := uint(0); j < tm; j++ {
p := j % mb
item := schema.NewItem(p, j, defaultMessageOptions)
err := queue.Enqueue(item)
if err != nil {
log.Fatalln(err)
}
}
t.Log("Enqueued all items")
}()
wg.Add(1)
go func() {
defer wg.Done()
for {
if atomic.LoadUint64(&received)+atomic.LoadUint64(&removed) >= uint64(tm) {
break
}
time.Sleep(time.Millisecond * 10)
_, err := queue.Dequeue()
if err != nil {
continue
}
atomic.AddUint64(&received, 1)
}
t.Log("Dequeued all items")
shutdown <- struct{}{}
}()
wg.Wait()
if received == 0 || removed == 0 || escalated == 0 {
t.Fatal("Prioritize failed, no value should be zero: received -", received, "removed -", removed, "escalated -", escalated)
}
if queue.ItemsInQueue() != 0 {
t.Fatal("Items in queue:", queue.ItemsInQueue())
}
queue.Close()
t.Log("Received:", received, "Removed:", removed, "Escalated:", escalated)
}
// Test Without Disk Features
ptest(1_000_000, false, false, 10)
// Test With Disk Features
ptest(1_000_000, true, true, 10)
}
func TestRestoreOrder(t *testing.T) {
var (
total uint = 250_000
syncToDisk bool = true
lazySync bool = true
maxBuckets uint = 10
)
defaultMessageOptions := schema.EnqueueOptions{
ShouldEscalate: false,
EscalationRate: time.Duration(time.Second),
CanTimeout: false,
Timeout: time.Duration(time.Second * 1),
}
opts := schema.GPQOptions{
MaxPriority: maxBuckets,
DiskCacheEnabled: syncToDisk,
DiskCachePath: "/tmp/gpq/test-restore",
DiskCacheCompression: false,
DiskEncryptionEnabled: false,
DiskEncryptionKey: []byte("1234567890"),
LazyDiskCacheChannelSize: 1_000_000,
DiskWriteDelay: time.Duration(time.Second),
LazyDiskCacheEnabled: lazySync,
LazyDiskBatchSize: 10_000,
}
_, queue, err := gpq.NewGPQ[string](opts)
if err != nil {
log.Fatalln(err)
}
var order = make(map[uint][]schema.Item[string])
// Add the messages to the queue in order
for i := uint(0); i < total; i++ {
p := i % maxBuckets
item := schema.NewItem(p, randomString(30), defaultMessageOptions)
err := queue.Enqueue(item)
if err != nil {
log.Fatalln(err)
}
_, ok := order[p]
if !ok {
order[p] = make([]schema.Item[string], 0)
}
order[p] = append(order[p], item)
}
// Close the queue
queue.Close()
// Rebuild the queue
restored, queue, err := gpq.NewGPQ[string](opts)
if err != nil {
log.Fatalln(err)
}
if restored == 0 {
log.Fatalln("No items were restored")
}
var lastHigh = uint(0)
// Pull off the queue and verify order
for i := uint(0); i < total; i++ {
item, err := queue.Dequeue()
if err != nil {
log.Fatalln(err)
}
if item.Priority < lastHigh {
log.Fatalln("Priority order mismatch", lastHigh, maxBuckets)
}
lastHigh = item.Priority
orderMap := order[item.Priority]
var found bool
for _, m := range orderMap {
if m.Data == item.Data {
found = true
break
}
}
if !found {
log.Fatalln("Message not found in correct bucket")
}
}
log.Println("Restore Order test passed")
}