Repository navigation
Expand file tree
/
Copy pathclient_test.go
More file actions
952 lines (787 loc) · 20.4 KB
/
Copy pathclient_test.go
File metadata and controls
952 lines (787 loc) · 20.4 KB
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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
package redis
import (
"context"
"errors"
"testing"
"time"
libredis "github.com/redis/go-redis/v9"
"github.com/stretchr/testify/require"
)
func TestNew(t *testing.T) {
t.Parallel()
srvOpts := &SrvOptions{
Addr: "test.redis.invalid:6379",
Username: "test_user",
Password: "test_password",
DB: 0,
}
got, err := New(
t.Context(),
srvOpts,
WithMessageEncodeFunc(nil),
)
require.Error(t, err)
require.ErrorIs(t, err, ErrNilEncodeFunc)
require.Nil(t, got)
got, err = New(
t.Context(),
srvOpts,
WithMessageDecodeFunc(nil),
)
require.Error(t, err)
require.ErrorIs(t, err, ErrNilDecodeFunc)
require.Nil(t, got)
got, err = New(
t.Context(),
nil,
)
require.Error(t, err)
require.ErrorIs(t, err, ErrInvalidOptions)
require.Nil(t, got)
got, err = New(
t.Context(),
srvOpts,
WithChannels("test_channel_1", ""),
)
require.Error(t, err)
require.ErrorIs(t, err, ErrInvalidChannelName)
require.Nil(t, got)
got, err = New(
t.Context(),
srvOpts,
)
require.NoError(t, err)
require.NotNil(t, got)
t.Cleanup(func() { _ = got.Close() })
}
func TestNew_withSubscription(t *testing.T) {
t.Parallel()
srvOpts := &SrvOptions{
Addr: "test.redis.invalid:6379",
Username: "test_user",
Password: "test_password",
DB: 0,
}
got, err := New(
t.Context(),
srvOpts,
WithChannels("test_channel_1", "test_channel_2"),
WithChannelOptions(libredis.WithChannelSize(1)),
)
require.NoError(t, err)
require.NotNil(t, got)
require.NotNil(t, got.rpubsub)
require.NotNil(t, got.subch)
require.Equal(t, 1, cap(got.subch))
t.Cleanup(func() { _ = got.Close() })
}
// TestNew_withInjectedClient_nilPubSub verifies that an injected client whose
// Subscribe returns a nil PubSub is reported as an error instead of a panic.
func TestNew_withInjectedClient_nilPubSub(t *testing.T) {
t.Parallel()
got, err := New(
t.Context(),
nil,
WithRedisClient(redisClientMock{subscribeFn: func(_ context.Context, _ ...string) *libredis.PubSub {
return nil
}}),
WithChannels("test_channel_1"),
)
require.Error(t, err)
require.ErrorIs(t, err, ErrInvalidOptions)
require.Nil(t, got)
}
func TestNew_withInjectedClient(t *testing.T) {
t.Parallel()
// With an injected client no connection is dialed,
// so no server options are required.
cli, err := New(
t.Context(),
nil,
WithRedisClient(redisClientMock{pingFn: func(_ context.Context) *libredis.StatusCmd {
return libredis.NewStatusResult("PONG", nil)
}}),
)
require.NoError(t, err)
require.NotNil(t, cli)
require.NoError(t, cli.HealthCheck(t.Context()))
}
func TestNew_withoutSubscription(t *testing.T) {
t.Parallel()
cli, err := New(
t.Context(),
nil,
WithRedisClient(redisClientMock{}),
)
require.NoError(t, err)
require.NotNil(t, cli)
// No subscription was configured: no Pub/Sub resources are allocated.
require.Nil(t, cli.rpubsub)
require.Nil(t, cli.subch)
// Receive must fail safely instead of blocking forever on a nil channel.
ch, val, err := cli.Receive(t.Context())
require.Error(t, err)
require.ErrorIs(t, err, ErrNoSubscription)
require.Empty(t, ch)
require.Empty(t, val)
// Close must not panic when there is no Pub/Sub subscription.
require.NoError(t, cli.Close())
}
type redisClientMock struct {
closeFn func() error
delFn func(ctx context.Context, keys ...string) *libredis.IntCmd
getFn func(ctx context.Context, key string) *libredis.StringCmd
pingFn func(ctx context.Context) *libredis.StatusCmd
publishFn func(ctx context.Context, channel string, message any) *libredis.IntCmd
setFn func(ctx context.Context, key string, value any, expiration time.Duration) *libredis.StatusCmd
subscribeFn func(ctx context.Context, channels ...string) *libredis.PubSub
}
func (m redisClientMock) Close() error {
if m.closeFn == nil {
return nil
}
return m.closeFn()
}
func (m redisClientMock) Del(ctx context.Context, keys ...string) *libredis.IntCmd {
return m.delFn(ctx, keys...)
}
func (m redisClientMock) Get(ctx context.Context, key string) *libredis.StringCmd {
return m.getFn(ctx, key)
}
func (m redisClientMock) Ping(ctx context.Context) *libredis.StatusCmd {
return m.pingFn(ctx)
}
func (m redisClientMock) Publish(ctx context.Context, channel string, message any) *libredis.IntCmd {
return m.publishFn(ctx, channel, message)
}
func (m redisClientMock) Set(ctx context.Context, key string, value any, expiration time.Duration) *libredis.StatusCmd {
return m.setFn(ctx, key, value, expiration)
}
func (m redisClientMock) Subscribe(ctx context.Context, channels ...string) *libredis.PubSub {
return m.subscribeFn(ctx, channels...)
}
type redisPubSubMock struct {
channelFn func(opts ...libredis.ChannelOption) <-chan *libredis.Message
closeFn func() error
}
func (m redisPubSubMock) Channel(opts ...libredis.ChannelOption) <-chan *libredis.Message {
return m.channelFn(opts...)
}
func (m redisPubSubMock) Close() error {
if m.closeFn == nil {
return nil
}
return m.closeFn()
}
// requireErrorMatches asserts that err is non-nil and, when set, that it
// contains the wantMsg substring and matches the wantIs sentinel.
func requireErrorMatches(t *testing.T, err error, wantMsg string, wantIs error) {
t.Helper()
require.Error(t, err)
if wantMsg != "" {
require.ErrorContains(t, err, wantMsg)
}
if wantIs != nil {
require.ErrorIs(t, err, wantIs)
}
}
// newTestClient builds a Client on an injected mock, optionally wiring a
// mocked Pub/Sub subscription, without dialing any connection.
func newTestClient(t *testing.T, rclient RClient, rpubsub RPubSub) *Client {
t.Helper()
cli, err := New(t.Context(), nil, WithRedisClient(rclient))
require.NoError(t, err)
require.NotNil(t, cli)
if rpubsub != nil {
cli.rpubsub = rpubsub
cli.subch = rpubsub.Channel()
}
t.Cleanup(func() { _ = cli.Close() })
return cli
}
func TestClose(t *testing.T) {
t.Parallel()
tests := []struct {
name string
rClientMock RClient
rPubSubMock RPubSub
wantErr bool
}{
{
name: "success",
rClientMock: redisClientMock{closeFn: func() error {
return nil
}},
rPubSubMock: redisPubSubMock{closeFn: func() error {
return nil
}},
wantErr: false,
},
{
name: "error PubSub",
rClientMock: redisClientMock{closeFn: func() error {
return nil
}},
rPubSubMock: redisPubSubMock{closeFn: func() error {
return errors.New("test error")
}},
wantErr: true,
},
{
name: "error Client",
rClientMock: redisClientMock{closeFn: func() error {
return errors.New("test error")
}},
rPubSubMock: redisPubSubMock{closeFn: func() error {
return nil
}},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
cli, err := New(t.Context(), nil, WithRedisClient(tt.rClientMock))
require.NoError(t, err)
require.NotNil(t, cli)
cli.rpubsub = tt.rPubSubMock
err = cli.Close()
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
})
}
}
// TestClose_always_closes_client verifies that the Redis client is closed and
// its error reported even when closing the Pub/Sub subscription fails.
func TestClose_always_closes_client(t *testing.T) {
t.Parallel()
clientClosed := false
cli := newTestClient(t,
redisClientMock{closeFn: func() error {
clientClosed = true
return errors.New("client close error")
}},
redisPubSubMock{
channelFn: func(_ ...libredis.ChannelOption) <-chan *libredis.Message {
return make(chan *libredis.Message)
},
closeFn: func() error {
return errors.New("pubsub close error")
},
},
)
err := cli.Close()
require.Error(t, err)
require.True(t, clientClosed, "rclient.Close must be attempted even when PubSub close fails")
require.ErrorContains(t, err, "pubsub close error")
require.ErrorContains(t, err, "client close error")
}
// TestClose_idempotent verifies that only the first Close call releases the
// resources and that subsequent calls return the result of the first.
func TestClose_idempotent(t *testing.T) {
t.Parallel()
closeCalls := 0
cli := newTestClient(t,
redisClientMock{closeFn: func() error {
closeCalls++
return errors.New("client close error")
}},
nil,
)
errFirst := cli.Close()
require.Error(t, errFirst)
errSecond := cli.Close()
require.ErrorIs(t, errSecond, errFirst)
require.Equal(t, 1, closeCalls, "rclient.Close must be called exactly once")
}
// TestClose_concurrentReceive verifies that closing the client while a
// Receive call is blocked unblocks it with ErrSubscriptionClosed, mirroring
// the real go-redis behavior where Close terminates the message channel.
func TestClose_concurrentReceive(t *testing.T) {
t.Parallel()
msgch := make(chan *libredis.Message)
cli := newTestClient(t,
redisClientMock{},
redisPubSubMock{
channelFn: func(_ ...libredis.ChannelOption) <-chan *libredis.Message {
return msgch
},
closeFn: func() error {
close(msgch)
return nil
},
},
)
var (
rerr error
done = make(chan struct{})
)
go func() {
defer close(done)
_, _, rerr = cli.Receive(t.Context())
}()
require.NoError(t, cli.Close())
<-done
require.ErrorIs(t, rerr, ErrSubscriptionClosed)
}
func TestSet(t *testing.T) {
t.Parallel()
tests := []struct {
name string
rClientMock RClient
wantErr bool
}{
{
name: "success",
rClientMock: redisClientMock{setFn: func(_ context.Context, _ string, _ any, _ time.Duration) *libredis.StatusCmd {
return libredis.NewStatusResult("", nil)
}},
wantErr: false,
},
{
name: "error",
rClientMock: redisClientMock{setFn: func(_ context.Context, _ string, _ any, _ time.Duration) *libredis.StatusCmd {
return libredis.NewStatusResult("", errors.New("test error"))
}},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
cli := newTestClient(t, tt.rClientMock, nil)
err := cli.Set(t.Context(), "key_1", "value_1", time.Second)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
})
}
}
func TestGet(t *testing.T) {
t.Parallel()
tests := []struct {
name string
rClientMock RClient
wantErr bool
}{
{
name: "success",
rClientMock: redisClientMock{getFn: func(_ context.Context, _ string) *libredis.StringCmd {
return libredis.NewStringResult("value_2", nil)
}},
wantErr: false,
},
{
name: "error",
rClientMock: redisClientMock{getFn: func(_ context.Context, _ string) *libredis.StringCmd {
return libredis.NewStringResult("", errors.New("test error"))
}},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
cli := newTestClient(t, tt.rClientMock, nil)
var got string
err := cli.Get(t.Context(), "key_2", &got)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
require.Equal(t, "value_2", got)
})
}
}
// TestGet_keyNotFound verifies that a missing key surfaces as ErrKeyNotFound
// and that the upstream go-redis Nil error is no longer in the chain.
func TestGet_keyNotFound(t *testing.T) {
t.Parallel()
cli := newTestClient(t,
redisClientMock{getFn: func(_ context.Context, _ string) *libredis.StringCmd {
return libredis.NewStringResult("", libredis.Nil)
}},
nil,
)
var got string
err := cli.Get(t.Context(), "missing_key", &got)
require.Error(t, err)
require.ErrorIs(t, err, ErrKeyNotFound)
require.NotErrorIs(t, err, libredis.Nil)
require.ErrorContains(t, err, "missing_key")
}
func TestDel(t *testing.T) {
t.Parallel()
tests := []struct {
name string
rClientMock RClient
wantErr bool
}{
{
name: "success",
rClientMock: redisClientMock{delFn: func(_ context.Context, _ ...string) *libredis.IntCmd {
return libredis.NewIntResult(0, nil)
}},
wantErr: false,
},
{
name: "error",
rClientMock: redisClientMock{delFn: func(_ context.Context, _ ...string) *libredis.IntCmd {
return libredis.NewIntResult(0, errors.New("test error"))
}},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
cli := newTestClient(t, tt.rClientMock, nil)
err := cli.Del(t.Context(), "key_3")
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
})
}
}
func TestSend(t *testing.T) {
t.Parallel()
tests := []struct {
name string
rClientMock RClient
wantErr bool
}{
{
name: "success",
rClientMock: redisClientMock{publishFn: func(_ context.Context, _ string, _ any) *libredis.IntCmd {
return libredis.NewIntResult(0, nil)
}},
wantErr: false,
},
{
name: "error",
rClientMock: redisClientMock{publishFn: func(_ context.Context, _ string, _ any) *libredis.IntCmd {
return libredis.NewIntResult(0, errors.New("test error"))
}},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
cli := newTestClient(t, tt.rClientMock, nil)
err := cli.Send(t.Context(), "channel_1", "message_1")
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
})
}
}
func TestSetData(t *testing.T) {
t.Parallel()
cli := newTestClient(t,
redisClientMock{setFn: func(_ context.Context, _ string, _ any, _ time.Duration) *libredis.StatusCmd {
return libredis.NewStatusResult("", nil)
}},
nil,
)
type TestData struct {
Alpha string
Beta int
}
err := cli.SetData(t.Context(), "key_4", TestData{Alpha: "abc123", Beta: -567}, time.Second)
require.NoError(t, err)
err = cli.SetData(t.Context(), "key_5", nil, time.Second)
require.Error(t, err)
require.ErrorContains(t, err, "cannot encode data for key key_5")
}
func TestGetData(t *testing.T) {
t.Parallel()
type TestData struct {
Alpha string
Beta int
}
tests := []struct {
name string
rClientMock RClient
wantErr bool
wantErrMsg string
wantErrIs error
}{
{
name: "success",
rClientMock: redisClientMock{getFn: func(_ context.Context, _ string) *libredis.StringCmd {
return libredis.NewStringResult("Kf+BAwEBCFRlc3REYXRhAf+CAAECAQVBbHBoYQEMAAEEQmV0YQEEAAAAD/+CAQZhYmMxMjMB/gLtAA==", nil)
}},
wantErr: false,
},
{
name: "error corrupted value",
rClientMock: redisClientMock{getFn: func(_ context.Context, _ string) *libredis.StringCmd {
return libredis.NewStringResult("INVALID", nil)
}},
wantErr: true,
wantErrMsg: "cannot decode data for key key_7",
},
{
name: "error key not found",
rClientMock: redisClientMock{getFn: func(_ context.Context, _ string) *libredis.StringCmd {
return libredis.NewStringResult("", libredis.Nil)
}},
wantErr: true,
wantErrIs: ErrKeyNotFound,
},
{
name: "error",
rClientMock: redisClientMock{getFn: func(_ context.Context, _ string) *libredis.StringCmd {
return libredis.NewStringResult("", errors.New("test error"))
}},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
cli := newTestClient(t, tt.rClientMock, nil)
var data TestData
err := cli.GetData(t.Context(), "key_7", &data)
if tt.wantErr {
requireErrorMatches(t, err, tt.wantErrMsg, tt.wantErrIs)
return
}
require.NoError(t, err)
require.Equal(t, "abc123", data.Alpha)
require.Equal(t, -375, data.Beta)
})
}
}
func TestSendData(t *testing.T) {
t.Parallel()
cli := newTestClient(t,
redisClientMock{publishFn: func(_ context.Context, _ string, _ any) *libredis.IntCmd {
return libredis.NewIntResult(0, nil)
}},
nil,
)
type TestData struct {
Alpha string
Beta int
}
err := cli.SendData(t.Context(), "channel_2", TestData{Alpha: "abc345", Beta: -678})
require.NoError(t, err)
err = cli.SendData(t.Context(), "channel_3", nil)
require.Error(t, err)
require.ErrorContains(t, err, "cannot encode data for channel_3 channel")
}
func TestReceive(t *testing.T) {
t.Parallel()
tests := []struct {
name string
rPubSubMock RPubSub
ctxTimeout time.Duration
wantErr bool
wantErrIs error
}{
{
name: "success",
rPubSubMock: redisPubSubMock{
channelFn: func(_ ...libredis.ChannelOption) <-chan *libredis.Message {
ch := make(chan *libredis.Message)
go func() {
ch <- &libredis.Message{
Channel: "channel_4",
Payload: "message_4",
}
}()
return ch
},
},
wantErr: false,
},
{
name: "success skips nil message",
rPubSubMock: redisPubSubMock{
channelFn: func(_ ...libredis.ChannelOption) <-chan *libredis.Message {
ch := make(chan *libredis.Message)
go func() {
ch <- nil
ch <- &libredis.Message{
Channel: "channel_4",
Payload: "message_4",
}
}()
return ch
},
},
wantErr: false,
},
{
name: "error closed channel",
rPubSubMock: redisPubSubMock{
channelFn: func(_ ...libredis.ChannelOption) <-chan *libredis.Message {
ch := make(chan *libredis.Message)
go func() {
close(ch)
}()
return ch
},
},
wantErr: true,
wantErrIs: ErrSubscriptionClosed,
},
{
name: "error context timeout",
rPubSubMock: redisPubSubMock{
channelFn: func(_ ...libredis.ChannelOption) <-chan *libredis.Message {
return make(chan *libredis.Message)
},
},
ctxTimeout: 1 * time.Millisecond,
wantErr: true,
wantErrIs: context.DeadlineExceeded,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
cli := newTestClient(t, redisClientMock{}, tt.rPubSubMock)
ctx := t.Context()
if tt.ctxTimeout > 0 {
cctx, cancel := context.WithTimeout(ctx, tt.ctxTimeout)
ctx = cctx
defer cancel()
}
ch, val, err := cli.Receive(ctx)
if tt.wantErr {
requireErrorMatches(t, err, "", tt.wantErrIs)
return
}
require.NoError(t, err)
require.Equal(t, "channel_4", ch)
require.Equal(t, "message_4", val)
})
}
}
func TestReceiveData(t *testing.T) {
t.Parallel()
type TestData struct {
Alpha string
Beta int
}
tests := []struct {
name string
rPubSubMock RPubSub
wantErr bool
wantErrMsg string
}{
{
name: "success",
rPubSubMock: redisPubSubMock{
channelFn: func(_ ...libredis.ChannelOption) <-chan *libredis.Message {
ch := make(chan *libredis.Message)
go func() {
ch <- &libredis.Message{
Channel: "channel_5",
Payload: "Kf+BAwEBCFRlc3REYXRhAf+CAAECAQVBbHBoYQEMAAEEQmV0YQEEAAAAD/+CAQZhYmMxMjMB/gLtAA==",
}
}()
return ch
},
},
wantErr: false,
},
{
name: "error corrupted value",
rPubSubMock: redisPubSubMock{
channelFn: func(_ ...libredis.ChannelOption) <-chan *libredis.Message {
ch := make(chan *libredis.Message)
go func() {
ch <- &libredis.Message{
Channel: "channel_5",
Payload: "INVALID",
}
}()
return ch
},
},
wantErr: true,
wantErrMsg: "cannot decode data from channel_5 channel",
},
{
name: "error closed channel",
rPubSubMock: redisPubSubMock{
channelFn: func(_ ...libredis.ChannelOption) <-chan *libredis.Message {
ch := make(chan *libredis.Message)
go func() {
close(ch)
}()
return ch
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
cli := newTestClient(t, redisClientMock{}, tt.rPubSubMock)
var data TestData
ch, err := cli.ReceiveData(t.Context(), &data)
if tt.wantErr {
requireErrorMatches(t, err, tt.wantErrMsg, nil)
// On any error, including a decode failure,
// the returned channel name is empty.
require.Empty(t, ch)
return
}
require.NoError(t, err)
require.Equal(t, "channel_5", ch)
require.Equal(t, "abc123", data.Alpha)
require.Equal(t, -375, data.Beta)
})
}
}
func TestHealthCheck(t *testing.T) {
t.Parallel()
tests := []struct {
name string
rClientMock RClient
wantErr bool
}{
{
name: "success",
rClientMock: redisClientMock{pingFn: func(_ context.Context) *libredis.StatusCmd {
return libredis.NewStatusResult("", nil)
}},
wantErr: false,
},
{
name: "error",
rClientMock: redisClientMock{pingFn: func(_ context.Context) *libredis.StatusCmd {
return libredis.NewStatusResult("", errors.New("test error"))
}},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
cli := newTestClient(t, tt.rClientMock, nil)
err := cli.HealthCheck(t.Context())
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
})
}
}