-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_test.go
508 lines (457 loc) · 13.3 KB
/
json_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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
package taskqueue_test
import (
"encoding/json"
"fmt"
"testing"
"time"
taskqueue "github.com/stellaraf/go-task-queue"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type RetryValue struct {
Value string `json:"value"`
}
func (v *RetryValue) UnmarshalJSON([]byte) error {
return fmt.Errorf("wrong")
}
func Test_NewJSONTaskQueue(t *testing.T) {
name := fmt.Sprintf("%s--%s", t.Name(), time.Now().Format(time.RFC3339Nano))
mr := RunT(t)
var ctx, addr, uri taskqueue.Option
if UseMini {
ctx = taskqueue.WithContext(mr.Ctx)
addr = taskqueue.WithHost(mr.Addr())
uri = taskqueue.WithURI(fmt.Sprintf("redis://%s", mr.Addr()))
} else {
ctx = taskqueue.WithContext(Ctx)
addr = taskqueue.WithHost(Addr)
uri = taskqueue.WithURI(fmt.Sprintf("redis://%s", Addr))
}
t.Run("base", func(t *testing.T) {
t.Parallel()
_, err := taskqueue.NewJSON(name, ctx, addr)
require.NoError(t, err)
})
t.Run("with uri", func(t *testing.T) {
t.Parallel()
_, err := taskqueue.NewJSON(name, ctx, uri)
require.NoError(t, err)
})
}
func TestJSONTaskQueue_Clear(t *testing.T) {
mr := RunT(t)
var ctx, addr taskqueue.Option
if UseMini {
ctx = taskqueue.WithContext(mr.Ctx)
addr = taskqueue.WithHost(mr.Addr())
} else {
ctx = taskqueue.WithContext(Ctx)
addr = taskqueue.WithHost(Addr)
}
name := fmt.Sprintf("%s--%s", t.Name(), time.Now().Format(time.RFC3339Nano))
queue, err := taskqueue.NewJSON(name, ctx, addr)
require.NoError(t, err)
t.Run("add", func(t *testing.T) {
err := queue.Add("value")
require.NoError(t, err)
})
t.Run("clear", func(t *testing.T) {
err := queue.Clear()
require.NoError(t, err)
})
t.Run("ensure cleared", func(t *testing.T) {
size := queue.Size()
assert.Equal(t, zero, size)
})
}
func TestJSONTaskQueue_Size(t *testing.T) {
mr := RunT(t)
var ctx, addr taskqueue.Option
if UseMini {
ctx = taskqueue.WithContext(mr.Ctx)
addr = taskqueue.WithHost(mr.Addr())
} else {
ctx = taskqueue.WithContext(Ctx)
addr = taskqueue.WithHost(Addr)
}
name := fmt.Sprintf("%s--%s", t.Name(), time.Now().Format(time.RFC3339Nano))
queue, err := taskqueue.NewJSON(name, ctx, addr)
defer queue.Clear()
require.NoError(t, err)
t.Run("add", func(t *testing.T) {
err := queue.Add("value")
require.NoError(t, err)
})
t.Run("ensure size", func(t *testing.T) {
size := queue.Size()
assert.Equal(t, uint64(1), size)
})
}
func TestJSONTaskQueue_Add(t *testing.T) {
mr := RunT(t)
var ctx, addr taskqueue.Option
if UseMini {
ctx = taskqueue.WithContext(mr.Ctx)
addr = taskqueue.WithHost(mr.Addr())
} else {
ctx = taskqueue.WithContext(Ctx)
addr = taskqueue.WithHost(Addr)
}
name := fmt.Sprintf("%s--%s", t.Name(), time.Now().Format(time.RFC3339Nano))
queue, err := taskqueue.NewJSON(name, ctx, addr)
defer queue.Clear()
require.NoError(t, err)
type Value struct {
String string `json:"string"`
Number int `json:"number"`
Bool bool `json:"bool"`
}
value := Value{String: "string", Number: 1, Bool: true}
t.Run("add", func(t *testing.T) {
err := queue.Add(value)
require.NoError(t, err)
})
t.Run("ensure size", func(t *testing.T) {
size := queue.Size()
assert.Equal(t, uint64(1), size)
})
t.Run("pop", func(t *testing.T) {
var popped *Value
err := queue.Pop(&popped)
require.NoError(t, err)
assert.Equal(t, value.String, popped.String)
assert.Equal(t, value.Number, popped.Number)
assert.Equal(t, value.Bool, popped.Bool)
})
}
func TestJSONTaskQueue_Pop(t *testing.T) {
mr := RunT(t)
var ctx, addr taskqueue.Option
if UseMini {
ctx = taskqueue.WithContext(mr.Ctx)
addr = taskqueue.WithHost(mr.Addr())
} else {
ctx = taskqueue.WithContext(Ctx)
addr = taskqueue.WithHost(Addr)
}
name := fmt.Sprintf("%s--%s", t.Name(), time.Now().Format(time.RFC3339Nano))
queue, err := taskqueue.NewJSON(name, ctx, addr)
defer queue.Clear()
require.NoError(t, err)
type Value struct {
String string `json:"string"`
Number int `json:"number"`
Bool bool `json:"bool"`
}
value1 := Value{String: "string1", Number: 1, Bool: true}
value2 := Value{String: "string2", Number: 2, Bool: false}
value3 := Value{String: "string3", Number: 3, Bool: true}
t.Run("add", func(t *testing.T) {
err := queue.Add(value1)
require.NoError(t, err)
err = queue.Add(value2)
require.NoError(t, err)
err = queue.Add(value3)
require.NoError(t, err)
})
t.Run("pop", func(t *testing.T) {
var popped *Value
err := queue.Pop(&popped)
require.NoError(t, err)
})
t.Run("ensure size", func(t *testing.T) {
size := queue.Size()
assert.Equal(t, uint64(2), size)
})
t.Run("ensure correct item was popped", func(t *testing.T) {
var popped1 *Value
var popped2 *Value
err := queue.Pop(&popped1)
require.NoError(t, err)
err = queue.Pop(&popped2)
require.NoError(t, err)
assert.Equal(t, value2.Number, popped1.Number)
assert.Equal(t, value3.Number, popped2.Number)
size := queue.Size()
assert.Equal(t, zero, size)
})
t.Run("with retry", func(t *testing.T) {
name := fmt.Sprintf("%s--%s", t.Name(), time.Now().Format(time.RFC3339Nano))
queue, err := taskqueue.NewJSON(name, ctx, addr)
require.NoError(t, err)
v := RetryValue{"value"}
err = queue.Add(v)
require.NoError(t, err)
var popped *RetryValue
err = queue.Pop(&popped)
assert.NoError(t, err)
assert.Equal(t, uint64(1), queue.Size())
queue.Clear()
})
t.Run("with no retry", func(t *testing.T) {
name := fmt.Sprintf("%s--%s", t.Name(), time.Now().Format(time.RFC3339Nano))
queue, err := taskqueue.NewJSON(name, ctx, addr, taskqueue.WithNoRetry())
require.NoError(t, err)
v := RetryValue{"value"}
err = queue.Add(v)
require.NoError(t, err)
var popped *RetryValue
err = queue.Pop(&popped)
assert.Error(t, err)
assert.Equal(t, zero, queue.Size())
})
t.Run("retry value is equal to original", func(t *testing.T) {
name := fmt.Sprintf("%s--%s", t.Name(), time.Now().Format(time.RFC3339Nano))
queue, err := taskqueue.NewJSON(name, ctx, addr)
require.NoError(t, err)
in := RetryValue{"value"}
inB, err := json.Marshal(in)
require.NoError(t, err)
err = queue.Add(in)
require.NoError(t, err)
assert.Equal(t, uint64(1), queue.Size())
var out *RetryValue
err = queue.Pop(&out)
assert.NoError(t, err)
outB, err := queue.PopBytes()
require.NoError(t, err)
assert.Equal(t, inB, outB)
t.Cleanup(func() {
queue.Clear()
})
})
}
func TestJSONTaskQueue_Remove(t *testing.T) {
mr := RunT(t)
var ctx, addr taskqueue.Option
if UseMini {
ctx = taskqueue.WithContext(mr.Ctx)
addr = taskqueue.WithHost(mr.Addr())
} else {
ctx = taskqueue.WithContext(Ctx)
addr = taskqueue.WithHost(Addr)
}
name := fmt.Sprintf("%s--%s", t.Name(), time.Now().Format(time.RFC3339Nano))
queue, err := taskqueue.NewJSON(name, ctx, addr)
defer queue.Clear()
require.NoError(t, err)
type Value struct {
String string `json:"string"`
Number int `json:"number"`
Bool bool `json:"bool"`
}
value1 := Value{String: "string1", Number: 1, Bool: true}
value2 := Value{String: "string2", Number: 2, Bool: false}
t.Run("add", func(t *testing.T) {
err := queue.Add(value1)
require.NoError(t, err)
err = queue.Add(value2)
require.NoError(t, err)
})
t.Run("remove", func(t *testing.T) {
err := queue.Remove(value1)
require.NoError(t, err)
})
t.Run("ensure size", func(t *testing.T) {
size := queue.Size()
assert.Equal(t, uint64(1), size)
})
t.Run("ensure correct item was removed", func(t *testing.T) {
var popped *Value
err := queue.Pop(&popped)
require.NoError(t, err)
assert.Equal(t, value2.String, popped.String)
assert.Equal(t, value2.Number, popped.Number)
assert.Equal(t, value2.Bool, popped.Bool)
})
}
func TestJSONTaskQueue_Complex(t *testing.T) {
mr := RunT(t)
var ctx, addr taskqueue.Option
if UseMini {
ctx = taskqueue.WithContext(mr.Ctx)
addr = taskqueue.WithHost(mr.Addr())
} else {
ctx = taskqueue.WithContext(Ctx)
addr = taskqueue.WithHost(Addr)
}
name := fmt.Sprintf("%s--%s", t.Name(), time.Now().Format(time.RFC3339Nano))
queue, err := taskqueue.NewJSON(name, ctx, addr)
defer queue.Clear()
require.NoError(t, err)
type Nested struct {
String string `json:"string"`
}
array := []string{"one", "two"}
type Value struct {
String string `json:"string"`
Number int `json:"number"`
Time time.Time `json:"time"`
Nested Nested `json:"nested"`
Array []string `json:"array"`
NestedPointer *Nested `json:"nested_pointer"`
}
now := time.Now()
value := Value{
String: "string",
Number: 1,
Time: now,
Nested: Nested{String: "nested"},
Array: array,
NestedPointer: &Nested{String: "pointer"},
}
t.Run("add", func(t *testing.T) {
err := queue.Add(value)
require.NoError(t, err)
})
t.Run("pop", func(t *testing.T) {
var popped *Value
err := queue.Pop(&popped)
require.NoError(t, err)
assert.Equal(t, value.String, popped.String)
assert.Equal(t, value.Number, popped.Number)
assert.Equal(t, value.Time.Format(time.RFC3339Nano), popped.Time.Format(time.RFC3339Nano))
assert.EqualValues(t, value.Array, popped.Array)
assert.Equal(t, value.Nested.String, popped.Nested.String)
assert.NotNil(t, popped.NestedPointer)
assert.Equal(t, value.NestedPointer.String, popped.NestedPointer.String)
})
t.Run("add pointer", func(t *testing.T) {
err := queue.Clear()
require.NoError(t, err)
value := &Value{
String: "string",
Number: 1,
Time: now,
Nested: Nested{String: "nested"},
Array: array,
NestedPointer: &Nested{String: "pointer"},
}
err = queue.Add(value)
require.NoError(t, err)
})
t.Run("pop pointer", func(t *testing.T) {
var popped *Value
err := queue.Pop(&popped)
require.NoError(t, err)
assert.Equal(t, value.String, popped.String)
assert.Equal(t, value.Number, popped.Number)
assert.Equal(t, value.Time.Format(time.RFC3339Nano), popped.Time.Format(time.RFC3339Nano))
assert.EqualValues(t, value.Array, popped.Array)
assert.Equal(t, value.Nested.String, popped.Nested.String)
assert.NotNil(t, popped.NestedPointer)
assert.Equal(t, value.NestedPointer.String, popped.NestedPointer.String)
})
}
func TestJSONTaskQueue_Get(t *testing.T) {
mr := RunT(t)
var ctx, addr taskqueue.Option
if UseMini {
ctx = taskqueue.WithContext(mr.Ctx)
addr = taskqueue.WithHost(mr.Addr())
} else {
ctx = taskqueue.WithContext(Ctx)
addr = taskqueue.WithHost(Addr)
}
name := fmt.Sprintf("%s--%s", t.Name(), time.Now().Format(time.RFC3339Nano))
queue, err := taskqueue.NewJSON(name, ctx, addr)
defer queue.Clear()
require.NoError(t, err)
type Value struct {
String string `json:"string"`
Number int `json:"number"`
Bool bool `json:"bool"`
}
value := Value{String: "string", Number: 1, Bool: true}
t.Run("add value", func(t *testing.T) {
err := queue.Add(value)
require.NoError(t, err)
})
t.Run("get value", func(t *testing.T) {
var got *Value
err := queue.Get(0, &got)
require.NoError(t, err)
assert.Equal(t, value.String, got.String)
assert.Equal(t, value.Number, got.Number)
assert.Equal(t, value.Bool, got.Bool)
})
}
func TestJSONTaskQueue_RemoveIndex(t *testing.T) {
mr := RunT(t)
var ctx, addr taskqueue.Option
if UseMini {
ctx = taskqueue.WithContext(mr.Ctx)
addr = taskqueue.WithHost(mr.Addr())
} else {
ctx = taskqueue.WithContext(Ctx)
addr = taskqueue.WithHost(Addr)
}
name := fmt.Sprintf("%s--%s", t.Name(), time.Now().Format(time.RFC3339Nano))
queue, err := taskqueue.NewJSON(name, ctx, addr)
defer queue.Clear()
require.NoError(t, err)
type Value struct {
String string `json:"string"`
Number int `json:"number"`
Bool bool `json:"bool"`
}
value := Value{String: "string", Number: 1, Bool: true}
t.Run("add value", func(t *testing.T) {
err := queue.Add(value)
require.NoError(t, err)
})
t.Run("remove value by index", func(t *testing.T) {
err := queue.RemoveIndex(0)
require.NoError(t, err)
})
t.Run("ensure value is removed via get", func(t *testing.T) {
var popped *Value
err := queue.Get(0, &popped)
require.Error(t, err)
assert.Nil(t, popped)
})
t.Run("ensure value is removed via pop", func(t *testing.T) {
var popped *Value
err := queue.Pop(&popped)
require.Error(t, err)
assert.Nil(t, popped)
})
t.Run("ensure no removed items errors", func(t *testing.T) {
err := queue.RemoveIndex(5)
require.Error(t, err)
})
}
func TestJSONTaskQueue_Has(t *testing.T) {
mr := RunT(t)
var ctx, addr taskqueue.Option
if UseMini {
ctx = taskqueue.WithContext(mr.Ctx)
addr = taskqueue.WithHost(mr.Addr())
} else {
ctx = taskqueue.WithContext(Ctx)
addr = taskqueue.WithHost(Addr)
}
name := fmt.Sprintf("%s--%s", t.Name(), time.Now().Format(time.RFC3339Nano))
queue, err := taskqueue.NewJSON(name, ctx, addr)
defer queue.Clear()
require.NoError(t, err)
type Value struct {
String string `json:"string"`
Number int `json:"number"`
Bool bool `json:"bool"`
}
value := Value{String: "string", Number: 1, Bool: true}
t.Run("add value", func(t *testing.T) {
err := queue.Add(value)
require.NoError(t, err)
})
t.Run("ensure queue has value", func(t *testing.T) {
has := queue.Has(&value)
require.True(t, has)
})
t.Run("ensure queue does not have nonexistent value", func(t *testing.T) {
other := Value{String: "otherstring", Number: 2, Bool: false}
has := queue.Has(&other)
require.False(t, has)
})
}