@@ -19,6 +19,41 @@ import (
19
19
"go.uber.org/goleak"
20
20
)
21
21
22
+ func pingPong (t * testing.T , c * redjet.Client ) {
23
+ ctx := context .Background ()
24
+ got , err := c .Command (ctx , "PING" ).String ()
25
+ require .NoError (t , err )
26
+ require .Equal (t , "PONG" , got )
27
+ }
28
+
29
+ func TestNewFromURL (t * testing.T ) {
30
+ t .Parallel ()
31
+
32
+ t .Run ("Normal" , func (t * testing.T ) {
33
+ t .Parallel ()
34
+
35
+ addr , _ := redtest .StartRedisServer (t )
36
+
37
+ c , err := redjet .NewFromURL ("redis://" + addr )
38
+ require .NoError (t , err )
39
+ defer c .Close ()
40
+
41
+ pingPong (t , c )
42
+ })
43
+
44
+ t .Run ("Password" , func (t * testing.T ) {
45
+ t .Parallel ()
46
+
47
+ addr , _ := redtest .StartRedisServer (t , "--requirepass" , "hunter2" )
48
+
49
+ c , err := redjet .NewFromURL ("redis://:hunter2@" + addr )
50
+ require .NoError (t , err )
51
+ defer c .Close ()
52
+
53
+ pingPong (t , c )
54
+ })
55
+ }
56
+
22
57
func TestClient_SetGet (t * testing.T ) {
23
58
t .Parallel ()
24
59
@@ -313,33 +348,50 @@ func TestClient_Auth(t *testing.T) {
313
348
func TestClient_PubSub (t * testing.T ) {
314
349
t .Parallel ()
315
350
316
- _ , client := redtest .StartRedisServer (t )
351
+ t .Run ("NoCommand" , func (t * testing.T ) {
352
+ _ , client := redtest .StartRedisServer (t )
317
353
318
- ctx := context .Background ()
319
- subCmd := client .Command (ctx , "SUBSCRIBE" , "foo" )
320
- defer subCmd .Close ()
354
+ ctx := context .Background ()
355
+ subCmd := client .Command (ctx , "SUBSCRIBE" , "foo" )
356
+ defer subCmd .Close ()
321
357
322
- msg , err := subCmd .NextSubMessage ()
323
- require .NoError (t , err )
358
+ _ , err := subCmd .NextSubMessage ()
359
+ require .Error (t , err )
360
+ })
324
361
325
- require .Equal (t , & redjet.SubMessage {
326
- Channel : "foo" ,
327
- Type : "subscribe" ,
328
- Payload : "1" ,
329
- }, msg )
362
+ t .Run ("OK" , func (t * testing.T ) {
363
+ _ , client := redtest .StartRedisServer (t )
330
364
331
- n , err := client .Command (ctx , "PUBLISH" , "foo" , "bar" ).Int ()
332
- require .NoError (t , err )
333
- require .Equal (t , 1 , n )
365
+ ctx := context .Background ()
334
366
335
- msg , err = subCmd .NextSubMessage ()
336
- require .NoError (t , err )
367
+ subCmd := client .Pipeline (ctx , nil , "SUBSCRIBE" , "foo" )
368
+ defer subCmd .Close ()
369
+
370
+ msg , err := subCmd .NextSubMessage ()
371
+ require .NoError (t , err )
372
+
373
+ require .Equal (t , & redjet.SubMessage {
374
+ Channel : "foo" ,
375
+ Type : "subscribe" ,
376
+ Payload : "1" ,
377
+ }, msg )
378
+
379
+ pubPipe := client .Pipeline (ctx , nil , "PUBLISH" , "foo" , "bar" )
380
+ defer pubPipe .Close ()
337
381
338
- require .Equal (t , & redjet.SubMessage {
339
- Channel : "foo" ,
340
- Type : "message" ,
341
- Payload : "bar" ,
342
- }, msg )
382
+ n , err := pubPipe .Int ()
383
+ require .NoError (t , err )
384
+ require .Equal (t , 1 , n )
385
+
386
+ msg , err = subCmd .NextSubMessage ()
387
+ require .NoError (t , err )
388
+
389
+ require .Equal (t , & redjet.SubMessage {
390
+ Channel : "foo" ,
391
+ Type : "message" ,
392
+ Payload : "bar" ,
393
+ }, msg )
394
+ })
343
395
}
344
396
345
397
func TestClient_ConnReuse (t * testing.T ) {
0 commit comments