-
Notifications
You must be signed in to change notification settings - Fork 1
/
rsmq_test.go
674 lines (539 loc) · 26.9 KB
/
rsmq_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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
package rsmq
import (
"context"
"fmt"
"log"
"os"
"testing"
"github.com/go-redis/redis"
"github.com/stretchr/testify/assert"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
type redisContainer struct {
testcontainers.Container
address string
}
func setupRedis(ctx context.Context) (*redisContainer, error) {
req := testcontainers.ContainerRequest{
Image: "redis:6.2.6-alpine",
ExposedPorts: []string{"6379/tcp"},
WaitingFor: wait.ForLog("* Ready to accept connections"),
}
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
if err != nil {
return nil, err
}
mappedPort, err := container.MappedPort(ctx, "6379")
if err != nil {
return nil, err
}
hostIP, err := container.Host(ctx)
if err != nil {
return nil, err
}
address := fmt.Sprintf("%s:%d", hostIP, mappedPort.Int())
return &redisContainer{Container: container, address: address}, nil
}
var redisCnt *redisContainer
var ctx context.Context
func setup() error {
ctx = context.Background()
rc, err := setupRedis(ctx)
redisCnt = rc
return err
}
func shutdown() error {
if redisCnt != nil {
return redisCnt.Container.Terminate(ctx)
}
return nil
}
func TestMain(m *testing.M) {
err := setup()
if err != nil {
log.Print(err)
}
code := m.Run()
err = shutdown()
if err != nil {
log.Print(err)
}
os.Exit(code)
}
func preIntegrationTest(t *testing.T) *redis.Client {
if testing.Short() {
t.Skip("Skipping integration test")
}
if redisCnt == nil || ctx == nil {
t.Error("env cannot be set correctly")
t.FailNow()
}
client := redis.NewClient(&redis.Options{
Addr: redisCnt.address,
})
t.Cleanup(func() {
client.FlushAll()
client.ScriptFlush()
client.FlushDB()
})
return client
}
func TestNewRedisSMQ(t *testing.T) {
client := preIntegrationTest(t)
ns := "test"
rsmq := NewRedisSMQ(client, ns)
assert.NotNil(t, rsmq, "rsmq is nil")
assert.NotNil(t, rsmq.client, "clint in rsmq is nil")
assert.Equal(t, ns+":", rsmq.ns, "namespace is not as expected")
t.Run("client with empty namespace", func(t *testing.T) {
rsmq := NewRedisSMQ(client, "")
assert.NotNil(t, rsmq, "rsmq is nil")
assert.Equal(t, defaultNs+":", rsmq.ns, "namespace is not as expected")
})
t.Run("panic when redis client is nil", func(t *testing.T) {
assert.Panics(t, func() {
NewRedisSMQ(nil, ns)
}, "not panicking when redis client is not")
})
}
func TestRedisSMQ_CreateQueue(t *testing.T) {
client := preIntegrationTest(t)
rsmq := NewRedisSMQ(client, "test")
qname := "que"
err := rsmq.CreateQueue(qname, UnsetVt, UnsetDelay, UnsetMaxsize)
assert.Nil(t, err, "error is not nil on creating a queue")
t.Run("error when the queue already exists", func(t *testing.T) {
err = rsmq.CreateQueue(qname, UnsetVt, UnsetDelay, UnsetMaxsize)
assert.NotNil(t, err, "error is nil on creating the existing queue")
assert.Equal(t, ErrQueueExists, err, "error is not as expected")
})
t.Run("error when the queue name is not valid", func(t *testing.T) {
err = rsmq.CreateQueue("it is invalid queue name", UnsetVt, UnsetDelay, UnsetMaxsize)
assert.NotNil(t, err, "error is nil on creating a queue with invalid name")
})
t.Run("error when the queue attribute vt is not valid", func(t *testing.T) {
err = rsmq.CreateQueue("queue-invalid-vt", 10_000_000, UnsetDelay, UnsetMaxsize)
assert.NotNil(t, err, "error is nil on creating a queue with invalid vt")
assert.Equal(t, ErrInvalidVt, err, "error is not as expected")
})
t.Run("error when the queue attribute delay is not valid", func(t *testing.T) {
err = rsmq.CreateQueue("queue-invalid-delay", UnsetVt, 10_000_000, UnsetMaxsize)
assert.NotNil(t, err, "error is nil on creating a queue with invalid delay")
assert.Equal(t, ErrInvalidDelay, err, "error is not as expected")
})
t.Run("error when the queue attribute maxsize is not valid", func(t *testing.T) {
err = rsmq.CreateQueue("queue-invalid-maxsize", UnsetVt, UnsetDelay, 1023)
assert.NotNil(t, err, "error is nil on creating a queue with invalid maxsize")
assert.Equal(t, ErrInvalidMaxsize, err, "error is not as expected")
err = rsmq.CreateQueue("queue-invalid-maxsize", UnsetVt, UnsetDelay, 65537)
assert.NotNil(t, err, "error is nil on creating a queue with invalid maxsize")
assert.Equal(t, ErrInvalidMaxsize, err, "error is not as expected")
})
}
func TestRedisSMQ_ListQueues(t *testing.T) {
client := preIntegrationTest(t)
rsmq := NewRedisSMQ(client, "test")
qname1 := "que1"
qname2 := "que2"
qname3 := "que3"
err := rsmq.CreateQueue(qname1, UnsetVt, UnsetDelay, UnsetMaxsize)
assert.Nil(t, err, "error is not nil on creating a queue")
err = rsmq.CreateQueue(qname2, UnsetVt, UnsetDelay, UnsetMaxsize)
assert.Nil(t, err, "error is not nil on creating a queue")
err = rsmq.CreateQueue(qname3, UnsetVt, UnsetDelay, UnsetMaxsize)
assert.Nil(t, err, "error is not nil on creating a queue")
queues, err := rsmq.ListQueues()
assert.Nil(t, err, "error is not nil on listing queues")
assert.Len(t, queues, 3, "queues length is not as expected")
assert.Contains(t, queues, qname1)
assert.Contains(t, queues, qname2)
assert.Contains(t, queues, qname3)
}
func TestRedisSMQ_GetQueueAttributes(t *testing.T) {
client := preIntegrationTest(t)
rsmq := NewRedisSMQ(client, "test")
qname := "que"
err := rsmq.CreateQueue(qname, UnsetVt, UnsetDelay, UnsetMaxsize)
assert.Nil(t, err, "error is not nil on creating a queue")
queAttrib, err := rsmq.GetQueueAttributes(qname)
assert.Nil(t, err, "error is not nil on getting queue attributes")
assert.NotNil(t, queAttrib, "queueAttributes is nil")
assert.EqualValues(t, defaultVt, queAttrib.Vt, "queueAttributes vt is not as expected")
assert.EqualValues(t, defaultDelay, queAttrib.Delay, "queueAttributes delay is not as expected")
assert.Equal(t, defaultMaxsize, queAttrib.Maxsize, "queueAttributes maxsize is not as expected")
assert.Zero(t, queAttrib.TotalRecv, "queueAttributes totalRecv is not zero")
assert.Zero(t, queAttrib.TotalSent, "queueAttributes totalSent is not zero")
assert.NotZero(t, queAttrib.Created, "queueAttributes created is zero")
assert.NotZero(t, queAttrib.Modified, "queueAttributes modified is zero")
assert.Equal(t, queAttrib.Created, queAttrib.Modified, "queueAttributes created is not equal to modified")
assert.Zero(t, queAttrib.Msgs, "queueAttributes msgs is not zero")
assert.Zero(t, queAttrib.HiddenMsgs, "queueAttributes hiddenMsgs is not zero")
t.Run("attributes of the queue with custom configs", func(t *testing.T) {
qname := "queue-custom-config"
vt := uint(90)
delay := uint(30)
maxsize := 2048
err := rsmq.CreateQueue(qname, vt, delay, maxsize)
assert.Nil(t, err, "error is not nil on creating a queue")
queAttrib, err := rsmq.GetQueueAttributes(qname)
assert.Nil(t, err, "error is not nil on getting queue attributes")
assert.NotNil(t, queAttrib, "queueAttributes is nil")
assert.Equal(t, vt, queAttrib.Vt, "queueAttributes vt is not as expected")
assert.Equal(t, delay, queAttrib.Delay, "queueAttributes delay is not as expected")
assert.Equal(t, maxsize, queAttrib.Maxsize, "queueAttributes maxsize is not as expected")
})
t.Run("attributes after sending, receiving and pop messages", func(t *testing.T) {
_, err := rsmq.SendMessage(qname, "msg-1", UnsetDelay)
assert.Nil(t, err, "error is not nil on sending message to the queue")
queAttrib, err := rsmq.GetQueueAttributes(qname)
assert.Nil(t, err, "error is not nil on getting queue attributes")
assert.NotNil(t, queAttrib, "queueAttributes is nil")
assert.Zero(t, queAttrib.TotalRecv, "queueAttributes totalRecv is not zero")
assert.EqualValues(t, 1, queAttrib.TotalSent, "queueAttributes totalSent is not as expected")
assert.EqualValues(t, 1, queAttrib.Msgs, "queueAttributes msg is not as expected")
_, err = rsmq.SendMessage(qname, "msg-2", UnsetDelay)
assert.Nil(t, err, "error is not nil on sending message to the queue")
queAttrib, err = rsmq.GetQueueAttributes(qname)
assert.Nil(t, err, "error is not nil on getting queue attributes")
assert.NotNil(t, queAttrib, "queueAttributes is nil")
assert.Zero(t, queAttrib.TotalRecv, "queueAttributes totalRecv is not zero")
assert.EqualValues(t, 2, queAttrib.TotalSent, "queueAttributes totalSent is not as expected")
assert.EqualValues(t, 2, queAttrib.Msgs, "queueAttributes msg is not as expected")
_, err = rsmq.ReceiveMessage(qname, UnsetVt)
assert.Nil(t, err, "error is not nil on receiving message from the queue")
queAttrib, err = rsmq.GetQueueAttributes(qname)
assert.Nil(t, err, "error is not nil on getting queue attributes")
assert.NotNil(t, queAttrib, "queueAttributes is nil")
assert.EqualValues(t, 1, queAttrib.TotalRecv, "queueAttributes totalRecv is not as expected")
assert.EqualValues(t, 2, queAttrib.TotalSent, "queueAttributes totalSent is not as expected")
assert.EqualValues(t, 2, queAttrib.Msgs, "queueAttributes msg is not as expected")
_, err = rsmq.PopMessage(qname)
assert.Nil(t, err, "error is not nil on pop message from the queue")
queAttrib, err = rsmq.GetQueueAttributes(qname)
assert.Nil(t, err, "error is not nil on getting queue attributes")
assert.NotNil(t, queAttrib, "queueAttributes is nil")
assert.EqualValues(t, 2, queAttrib.TotalRecv, "queueAttributes totalRecv is not as expected")
assert.EqualValues(t, 2, queAttrib.TotalSent, "queueAttributes totalSent is not as expected")
assert.EqualValues(t, 1, queAttrib.Msgs, "queueAttributes msg is not as expected")
})
t.Run("error when the queue does not exist", func(t *testing.T) {
queAttrib, err = rsmq.GetQueueAttributes("non-existing")
assert.NotNil(t, err, "error is nil on getting attributes of non-existing queue")
assert.Equal(t, ErrQueueNotFound, err, "error is not as expected")
assert.Nil(t, queAttrib, "queueAttributes is not nil on getting attributes of non-existing queue")
})
t.Run("error when the queue name is not valid", func(t *testing.T) {
queAttrib, err = rsmq.GetQueueAttributes("it is invalid queue name")
assert.NotNil(t, err, "error is nil on getting attributes of queue with invalid name")
assert.Equal(t, ErrInvalidQname, err, "error is not as expected")
assert.Nil(t, queAttrib, "queueAttributes is not nil on getting attributes of queue with invalid name")
})
}
func TestRedisSMQ_SetQueueAttributes(t *testing.T) {
client := preIntegrationTest(t)
rsmq := NewRedisSMQ(client, "test")
qname := "que"
err := rsmq.CreateQueue(qname, UnsetVt, UnsetDelay, UnsetMaxsize)
assert.Nil(t, err, "error is not nil on creating a queue")
newVt := uint(90)
newDelay := uint(30)
newMaxsize := 2048
queAttrib, err := rsmq.SetQueueAttributes(qname, newVt, newDelay, newMaxsize)
assert.Nil(t, err, "error is not nil on setting queue attributes")
assert.NotNil(t, queAttrib, "queueAttributes is nil")
queAttrib, err = rsmq.GetQueueAttributes(qname)
assert.Nil(t, err, "error is not nil on getting queue attributes")
assert.NotNil(t, queAttrib, "queueAttributes is nil")
assert.Equal(t, newVt, queAttrib.Vt, "queueAttributes vt is not as expected")
assert.Equal(t, newDelay, queAttrib.Delay, "queueAttributes delay is not as expected")
assert.Equal(t, newMaxsize, queAttrib.Maxsize, "queueAttributes maxsize is not as expected")
assert.True(t, queAttrib.Modified > queAttrib.Created, "queueAttributes modified is not greater than created")
t.Run("error when the queue does not exist", func(t *testing.T) {
queAttrib, err = rsmq.SetQueueAttributes("non-existing", UnsetVt, UnsetDelay, UnsetMaxsize)
assert.NotNil(t, err, "error is nil on setting attributes of non-existing queue")
assert.Equal(t, ErrQueueNotFound, err, "error is not as expected")
assert.Nil(t, queAttrib, "queueAttributes is not nil on setting attributes of non-existing queue")
})
t.Run("error when the queue name is not valid", func(t *testing.T) {
queAttrib, err = rsmq.SetQueueAttributes("it is invalid queue name", UnsetVt, UnsetDelay, UnsetMaxsize)
assert.NotNil(t, err, "error is nil on setting attributes of queue with invalid name")
assert.Equal(t, ErrInvalidQname, err, "error is not as expected")
assert.Nil(t, queAttrib, "queueAttributes is not nil on setting attributes of queue with invalid name")
})
t.Run("error when the queue attribute vt is not valid", func(t *testing.T) {
qname := "queue-set-invalid-vt"
err = rsmq.CreateQueue(qname, UnsetVt, UnsetDelay, UnsetMaxsize)
assert.Nil(t, err, "error is not nil on creating a queue")
queAttrib, err := rsmq.SetQueueAttributes(qname, 10_000_000, UnsetDelay, UnsetMaxsize)
assert.NotNil(t, err, "error is not nil on setting invalid queue attribute vt")
assert.Nil(t, queAttrib, "queAttrib is not nil on setting invalid queue attribute vt")
queAttrib, err = rsmq.GetQueueAttributes(qname)
assert.Nil(t, err, "error is not nil on getting queue attributes")
assert.NotNil(t, queAttrib, "queueAttributes is nil")
assert.EqualValues(t, defaultVt, queAttrib.Vt, "queueAttributes vt is not as expected")
})
t.Run("error when the queue attribute delay is not valid", func(t *testing.T) {
qname := "queue-set-invalid-delay"
err = rsmq.CreateQueue(qname, UnsetVt, UnsetDelay, UnsetMaxsize)
assert.Nil(t, err, "error is not nil on creating a queue")
queAttrib, err := rsmq.SetQueueAttributes(qname, UnsetVt, 10_000_000, UnsetMaxsize)
assert.NotNil(t, err, "error is not nil on setting invalid queue attribute delay")
assert.Nil(t, queAttrib, "queAttrib is not nil on setting invalid queue attribute delay")
queAttrib, err = rsmq.GetQueueAttributes(qname)
assert.Nil(t, err, "error is not nil on getting queue attributes")
assert.NotNil(t, queAttrib, "queueAttributes is nil")
assert.EqualValues(t, defaultDelay, queAttrib.Delay, "queueAttributes delay is not as expected")
})
t.Run("error when the queue attribute maxsize is not valid", func(t *testing.T) {
qname := "queue-set-invalid-maxsize"
err = rsmq.CreateQueue(qname, UnsetVt, UnsetDelay, UnsetMaxsize)
assert.Nil(t, err, "error is not nil on creating a queue")
queAttrib, err := rsmq.SetQueueAttributes(qname, UnsetVt, UnsetDelay, 1023)
assert.NotNil(t, err, "error is not nil on setting invalid queue attribute maxsize")
assert.Nil(t, queAttrib, "queAttrib is not nil on setting invalid queue attribute maxsize")
queAttrib, err = rsmq.GetQueueAttributes(qname)
assert.Nil(t, err, "error is not nil on getting queue attributes")
assert.NotNil(t, queAttrib, "queueAttributes is nil")
assert.Equal(t, defaultMaxsize, queAttrib.Maxsize, "queueAttributes maxsize is not as expected")
queAttrib, err = rsmq.SetQueueAttributes(qname, UnsetVt, UnsetDelay, 65537)
assert.NotNil(t, err, "error is not nil on setting invalid queue attribute maxsize")
assert.Nil(t, queAttrib, "queAttrib is not nil on setting invalid queue attribute maxsize")
queAttrib, err = rsmq.GetQueueAttributes(qname)
assert.Nil(t, err, "error is not nil on getting queue attributes")
assert.NotNil(t, queAttrib, "queueAttributes is nil")
assert.Equal(t, defaultMaxsize, queAttrib.Maxsize, "queueAttributes maxsize is not as expected")
})
}
func TestRedisSMQ_Quit(t *testing.T) {
client := preIntegrationTest(t)
rsmq := NewRedisSMQ(client, "test")
err := rsmq.Quit()
assert.Nil(t, err, "error is not nil on quit")
}
func TestRedisSMQ_DeleteQueue(t *testing.T) {
client := preIntegrationTest(t)
rsmq := NewRedisSMQ(client, "test")
qname := "que"
err := rsmq.CreateQueue(qname, UnsetVt, UnsetDelay, UnsetMaxsize)
assert.Nil(t, err, "error is not nil on creating a queue")
err = rsmq.DeleteQueue(qname)
assert.Nil(t, err, "error is not nil on deleting the queue")
queues, err := rsmq.ListQueues()
assert.Nil(t, err, "error is not nil on listing queues")
assert.Empty(t, queues, "queue slice is not empty")
t.Run("error when the queue does not exist", func(t *testing.T) {
err = rsmq.DeleteQueue("non-existing")
assert.NotNil(t, err, "error is nil on deleting non-existing queue")
assert.Equal(t, ErrQueueNotFound, err, "error is not as expected")
})
t.Run("error when the queue name is not valid", func(t *testing.T) {
err = rsmq.DeleteQueue("it is invalid queue name")
assert.NotNil(t, err, "error is nil on deleting queue with invalid name")
assert.Equal(t, ErrInvalidQname, err, "error is not as expected")
})
}
func TestRedisSMQ_SendMessage(t *testing.T) {
client := preIntegrationTest(t)
rsmq := NewRedisSMQ(client, "test")
qname1 := "que1"
err := rsmq.CreateQueue(qname1, UnsetVt, UnsetDelay, UnsetMaxsize)
assert.Nil(t, err, "error is not nil on creating a queue")
message := "message"
id, err := rsmq.SendMessage(qname1, message, UnsetDelay)
assert.Nil(t, err, "error is not nil on sending a message")
assert.NotEmpty(t, id, "id is empty on sending a message")
t.Run("error when the message size limit is exceeded", func(t *testing.T) {
qname2 := "que2"
err = rsmq.CreateQueue(qname2, UnsetVt, UnsetDelay, 1024)
assert.Nil(t, err, "error is not nil on creating a queue")
b := make([]byte, 2048)
message = string(b)
id, err = rsmq.SendMessage(qname2, message, UnsetDelay)
assert.NotNil(t, err, "error is nil on sending a message which exceeds the size limit")
assert.Equal(t, ErrMessageTooLong, err, "error is not as expected")
assert.Empty(t, id, "id is not empty on sending a message which exceeds the size limit")
})
t.Run("error when the queue does not exist", func(t *testing.T) {
id, err = rsmq.SendMessage("non-existing", message, UnsetDelay)
assert.NotNil(t, err, "error is nil on sending a message to non-existing queue")
assert.Equal(t, ErrQueueNotFound, err, "error is not as expected")
assert.Empty(t, id, "id is not empty on sending a message to non-existing queue")
})
t.Run("error when the queue name is not valid", func(t *testing.T) {
id, err = rsmq.SendMessage("it is invalid queue name", message, UnsetDelay)
assert.NotNil(t, err, "error is nil on sending a message to the queue with invalid name")
assert.Equal(t, ErrInvalidQname, err, "error is not as expected")
assert.Empty(t, id, "id is not empty on sending a message to the queue with invalid name")
})
t.Run("error when delay is not valid", func(t *testing.T) {
id, err := rsmq.SendMessage(qname1, message, 10_000_000)
assert.NotNil(t, err, "error is nil on sending with invalid delay")
assert.Equal(t, ErrInvalidDelay, err, "error is not as expected")
assert.Empty(t, id, "id is not empty on sending a message with invalid delay")
})
}
func TestRedisSMQ_ReceiveMessage(t *testing.T) {
client := preIntegrationTest(t)
rsmq := NewRedisSMQ(client, "test")
qname := "que"
err := rsmq.CreateQueue(qname, UnsetVt, UnsetDelay, UnsetMaxsize)
assert.Nil(t, err, "error is not nil on creating a queue")
message := "message"
id, err := rsmq.SendMessage(qname, message, UnsetDelay)
assert.Nil(t, err, "error is not nil on sending a message")
assert.NotEmpty(t, id, "id is empty on sending a message")
queMsg, err := rsmq.ReceiveMessage(qname, UnsetVt)
assert.Nil(t, err, "error is not nil on receiving the message")
assert.NotNil(t, queMsg, "queueMessage is nil")
assert.Equal(t, id, queMsg.ID, "queueMessage ID is not as expected")
assert.Equal(t, message, queMsg.Message, "queueMessage Message is not as expected")
t.Run("no error when the queue is empty", func(t *testing.T) {
queMsg, err = rsmq.ReceiveMessage(qname, UnsetVt)
assert.Nil(t, err, "error is not nil on receiving a message from empty queue")
assert.Nil(t, queMsg, "queueMessage is not nil on receiving a message from empty queue")
})
t.Run("error when the queue does not exist", func(t *testing.T) {
queMsg, err = rsmq.ReceiveMessage("non-existing", UnsetVt)
assert.NotNil(t, err, "error is nil on receiving the message from non-existing queue")
assert.Equal(t, ErrQueueNotFound, err, "error is not as expected")
assert.Empty(t, queMsg, "queueMessage is not empty on receiving the message from non-existing queue")
})
t.Run("error when the queue name is not valid", func(t *testing.T) {
queMsg, err = rsmq.ReceiveMessage("it is invalid queue name", UnsetVt)
assert.NotNil(t, err, "error is nil on receiving the message from the queue with invalid name")
assert.Equal(t, ErrInvalidQname, err, "error is not as expected")
assert.Empty(t, queMsg, "queueMessage is not empty on receiving the message from the queue with invalid name")
})
t.Run("error when vt is not valid", func(t *testing.T) {
queMsg, err := rsmq.ReceiveMessage(qname, 10_000_000)
assert.NotNil(t, err, "error is nil on receiving the message with invalid vt")
assert.Equal(t, ErrInvalidVt, err, "error is not as expected")
assert.Nil(t, queMsg, "queueMessage is not nil when the vt is not valid")
})
}
func TestRedisSMQ_PopMessage(t *testing.T) {
client := preIntegrationTest(t)
rsmq := NewRedisSMQ(client, "test")
qname := "que"
err := rsmq.CreateQueue(qname, UnsetVt, UnsetDelay, UnsetMaxsize)
assert.Nil(t, err, "error is not nil on creating a queue")
message := "message"
id, err := rsmq.SendMessage(qname, message, UnsetDelay)
assert.Nil(t, err, "error is not nil on sending a message")
assert.NotEmpty(t, id, "id is empty on sending a message")
queMsg, err := rsmq.PopMessage(qname)
assert.Nil(t, err, "error is not nil on pop the message")
assert.NotNil(t, queMsg, "queueMessage is nil")
assert.Equal(t, id, queMsg.ID, "queueMessage ID is not as expected")
assert.Equal(t, message, queMsg.Message, "queueMessage Message is not as expected")
t.Run("no error when the queue is empty", func(t *testing.T) {
queMsg, err = rsmq.PopMessage(qname)
assert.Nil(t, err, "error is not nil on pop a message from empty queue")
assert.Nil(t, queMsg, "queueMessage is not nil on pop a message from empty queue")
})
t.Run("error when the queue does not exist", func(t *testing.T) {
queMsg, err = rsmq.PopMessage("non-existing")
assert.NotNil(t, err, "error is nil on pop the message from non-existing queue")
assert.Equal(t, ErrQueueNotFound, err, "error is not as expected")
assert.Empty(t, queMsg, "queueMessage is not empty on pop the message from non-existing queue")
})
t.Run("error when the queue name is not valid", func(t *testing.T) {
queMsg, err = rsmq.PopMessage("it is invalid queue name")
assert.NotNil(t, err, "error is nil on pop the message from the queue with invalid name")
assert.Equal(t, ErrInvalidQname, err, "error is not as expected")
assert.Empty(t, queMsg, "queueMessage is not empty on pop the message from the queue with invalid name")
})
}
func TestRedisSMQ_ChangeMessageVisibility(t *testing.T) {
client := preIntegrationTest(t)
rsmq := NewRedisSMQ(client, "test")
qname := "que"
err := rsmq.CreateQueue(qname, UnsetVt, UnsetDelay, UnsetMaxsize)
assert.Nil(t, err, "error is not nil on creating a queue")
message := "message"
id, err := rsmq.SendMessage(qname, message, UnsetDelay)
assert.Nil(t, err, "error is not nil on sending a message")
assert.NotEmpty(t, id, "id is empty on sending a message")
newVt := uint(0)
err = rsmq.ChangeMessageVisibility(qname, id, newVt)
assert.Nil(t, err, "error is not nil on changing the message visibility")
queMsg, err := rsmq.PopMessage(qname)
assert.Nil(t, err, "error is not nil on pop the message")
assert.NotNil(t, queMsg, "queueMessage is nil")
assert.Equal(t, id, queMsg.ID, "queueMessage ID is not as expected")
assert.Equal(t, message, queMsg.Message, "queueMessage Message is not as expected")
t.Run("error when the message does not exist", func(t *testing.T) {
err = rsmq.ChangeMessageVisibility(qname, id, UnsetVt)
assert.NotNil(t, err, "error is nil on changing the visibility of message of non-existing message")
assert.Equal(t, ErrMessageNotFound, err, "error is not as expected")
})
t.Run("error when the queue does not exist", func(t *testing.T) {
err = rsmq.ChangeMessageVisibility("non-existing", id, UnsetVt)
assert.NotNil(t, err, "error is nil on changing the visibility of a message in non-existing queue")
assert.Equal(t, ErrQueueNotFound, err, "error is not as expected")
})
t.Run("error when the queue name is not valid", func(t *testing.T) {
err = rsmq.ChangeMessageVisibility("it is invalid queue name", id, UnsetVt)
assert.NotNil(t, err, "error is nil on changing the visibility of a message in the queue with invalid name")
assert.Equal(t, ErrInvalidQname, err, "error is not as expected")
})
t.Run("error when the message id is not valid", func(t *testing.T) {
err = rsmq.ChangeMessageVisibility(qname, "invalid message id", UnsetVt)
assert.NotNil(t, err, "error is nil on changing the visibility of a message with invalid id")
assert.Equal(t, ErrInvalidID, err, "error is not as expected")
})
t.Run("error when vt is not valid", func(t *testing.T) {
message := "message"
id, err := rsmq.SendMessage(qname, message, UnsetDelay)
assert.Nil(t, err, "error is not nil on sending a message")
assert.NotEmpty(t, id, "id is empty on sending a message")
newVt := uint(10_000_000)
err = rsmq.ChangeMessageVisibility(qname, id, newVt)
assert.NotNil(t, ErrInvalidVt, err, "error is nil on changing the message visibility with invalid vt")
queMsg, err := rsmq.PopMessage(qname)
assert.Nil(t, err, "error is not nil on pop the message")
assert.NotNil(t, queMsg, "queueMessage is nil")
assert.Equal(t, id, queMsg.ID, "queueMessage ID is not as expected")
assert.Equal(t, message, queMsg.Message, "queueMessage Message is not as expected")
})
}
func TestRedisSMQ_DeleteMessage(t *testing.T) {
client := preIntegrationTest(t)
rsmq := NewRedisSMQ(client, "test")
qname := "que"
err := rsmq.CreateQueue(qname, UnsetVt, UnsetDelay, UnsetMaxsize)
assert.Nil(t, err, "error is not nil on creating a queue")
message := "message"
id, err := rsmq.SendMessage(qname, message, UnsetVt)
assert.Nil(t, err, "error is not nil on sending a message")
assert.NotEmpty(t, id, "id is empty on sending a message")
err = rsmq.DeleteMessage(qname, id)
assert.Nil(t, err, "error is not nil on deleting the message")
queMsg, err := rsmq.ReceiveMessage(qname, UnsetVt)
assert.Nil(t, err, "error is not nil on receiving a message from empty queue")
assert.Nil(t, queMsg, "queueMessage is not nil on receiving a message from empty queue")
t.Run("error when the message does not exist", func(t *testing.T) {
err = rsmq.DeleteMessage(qname, id)
assert.NotNil(t, err, "error is nil on deleting non-existing message")
assert.Equal(t, ErrMessageNotFound, err, "error is not as expected")
})
t.Run("error when the queue does not exist", func(t *testing.T) {
err = rsmq.DeleteMessage("non-existing", id)
assert.NotNil(t, err, "error is nil on deleting a message in non-existing queue")
assert.Equal(t, ErrQueueNotFound, err, "error is not as expected")
})
t.Run("error when the queue name is not valid", func(t *testing.T) {
err = rsmq.DeleteMessage("it is invalid queue name", id)
assert.NotNil(t, err, "error is nil on deleting a message in the queue with invalid name")
assert.Equal(t, ErrInvalidQname, err, "error is not as expected")
})
t.Run("error when the message id is not valid", func(t *testing.T) {
err = rsmq.DeleteMessage(qname, "invalid message id")
assert.NotNil(t, err, "error is nil on deleting a message with invalid id")
assert.Equal(t, ErrInvalidID, err, "error is not as expected")
})
}