@@ -560,73 +560,104 @@ describe('Client', () => {
560
560
) ;
561
561
} , GLOBAL . SERVERS . OPEN ) ;
562
562
563
- testUtils . testWithClient ( 'PubSub' , async publisher => {
564
- function assertStringListener ( message : string , channel : string ) {
565
- assert . ok ( typeof message === 'string' ) ;
566
- assert . ok ( typeof channel === 'string' ) ;
567
- }
563
+ describe ( 'PubSub' , ( ) => {
564
+ testUtils . testWithClient ( 'should be able to publish and subscribe to messages' , async publisher => {
565
+ function assertStringListener ( message : string , channel : string ) {
566
+ assert . ok ( typeof message === 'string' ) ;
567
+ assert . ok ( typeof channel === 'string' ) ;
568
+ }
568
569
569
- function assertBufferListener ( message : Buffer , channel : Buffer ) {
570
- assert . ok ( Buffer . isBuffer ( message ) ) ;
571
- assert . ok ( Buffer . isBuffer ( channel ) ) ;
572
- }
570
+ function assertBufferListener ( message : Buffer , channel : Buffer ) {
571
+ assert . ok ( Buffer . isBuffer ( message ) ) ;
572
+ assert . ok ( Buffer . isBuffer ( channel ) ) ;
573
+ }
573
574
574
- const subscriber = publisher . duplicate ( ) ;
575
+ const subscriber = publisher . duplicate ( ) ;
576
+
577
+ await subscriber . connect ( ) ;
578
+
579
+ try {
580
+ const channelListener1 = spy ( assertBufferListener ) ,
581
+ channelListener2 = spy ( assertStringListener ) ,
582
+ patternListener = spy ( assertStringListener ) ;
583
+
584
+ await Promise . all ( [
585
+ subscriber . subscribe ( 'channel' , channelListener1 , true ) ,
586
+ subscriber . subscribe ( 'channel' , channelListener2 ) ,
587
+ subscriber . pSubscribe ( 'channel*' , patternListener )
588
+ ] ) ;
589
+ await Promise . all ( [
590
+ waitTillBeenCalled ( channelListener1 ) ,
591
+ waitTillBeenCalled ( channelListener2 ) ,
592
+ waitTillBeenCalled ( patternListener ) ,
593
+ publisher . publish ( Buffer . from ( 'channel' ) , Buffer . from ( 'message' ) )
594
+ ] ) ;
595
+
596
+ assert . ok ( channelListener1 . calledOnceWithExactly ( Buffer . from ( 'message' ) , Buffer . from ( 'channel' ) ) ) ;
597
+ assert . ok ( channelListener2 . calledOnceWithExactly ( 'message' , 'channel' ) ) ;
598
+ assert . ok ( patternListener . calledOnceWithExactly ( 'message' , 'channel' ) ) ;
599
+
600
+ await subscriber . unsubscribe ( 'channel' , channelListener1 , true ) ;
601
+ await Promise . all ( [
602
+ waitTillBeenCalled ( channelListener2 ) ,
603
+ waitTillBeenCalled ( patternListener ) ,
604
+ publisher . publish ( 'channel' , 'message' )
605
+ ] ) ;
606
+ assert . ok ( channelListener1 . calledOnce ) ;
607
+ assert . ok ( channelListener2 . calledTwice ) ;
608
+ assert . ok ( channelListener2 . secondCall . calledWithExactly ( 'message' , 'channel' ) ) ;
609
+ assert . ok ( patternListener . calledTwice ) ;
610
+ assert . ok ( patternListener . secondCall . calledWithExactly ( 'message' , 'channel' ) ) ;
611
+ await subscriber . unsubscribe ( 'channel' ) ;
612
+ await Promise . all ( [
613
+ waitTillBeenCalled ( patternListener ) ,
614
+ publisher . publish ( 'channel' , 'message' )
615
+ ] ) ;
616
+ assert . ok ( channelListener1 . calledOnce ) ;
617
+ assert . ok ( channelListener2 . calledTwice ) ;
618
+ assert . ok ( patternListener . calledThrice ) ;
619
+ assert . ok ( patternListener . thirdCall . calledWithExactly ( 'message' , 'channel' ) ) ;
620
+ await subscriber . pUnsubscribe ( ) ;
621
+ await publisher . publish ( 'channel' , 'message' ) ;
622
+ assert . ok ( channelListener1 . calledOnce ) ;
623
+ assert . ok ( channelListener2 . calledTwice ) ;
624
+ assert . ok ( patternListener . calledThrice ) ;
625
+ // should be able to send commands when unsubsribed from all channels (see #1652)
626
+ await assert . doesNotReject ( subscriber . ping ( ) ) ;
627
+ } finally {
628
+ await subscriber . disconnect ( ) ;
629
+ }
630
+ } , GLOBAL . SERVERS . OPEN ) ;
575
631
576
- await subscriber . connect ( ) ;
632
+ testUtils . testWithClient ( 'should resubscribe' , async publisher => {
633
+ const subscriber = publisher . duplicate ( ) ;
577
634
578
- try {
579
- const channelListener1 = spy ( assertBufferListener ) ,
580
- channelListener2 = spy ( assertStringListener ) ,
581
- patternListener = spy ( assertStringListener ) ;
635
+ await subscriber . connect ( ) ;
582
636
583
- await Promise . all ( [
584
- subscriber . subscribe ( 'channel' , channelListener1 , true ) ,
585
- subscriber . subscribe ( 'channel' , channelListener2 ) ,
586
- subscriber . pSubscribe ( 'channel*' , patternListener )
587
- ] ) ;
588
- await Promise . all ( [
589
- waitTillBeenCalled ( channelListener1 ) ,
590
- waitTillBeenCalled ( channelListener2 ) ,
591
- waitTillBeenCalled ( patternListener ) ,
592
- publisher . publish ( Buffer . from ( 'channel' ) , Buffer . from ( 'message' ) )
593
- ] ) ;
637
+ try {
638
+ const listener = spy ( ) ;
639
+ await subscriber . subscribe ( 'channel' , listener ) ;
640
+
641
+ subscriber . on ( 'error' , err => {
642
+ console . error ( 'subscriber err' , err . message ) ;
643
+ } ) ;
594
644
595
- assert . ok ( channelListener1 . calledOnceWithExactly ( Buffer . from ( 'message' ) , Buffer . from ( 'channel' ) ) ) ;
596
- assert . ok ( channelListener2 . calledOnceWithExactly ( 'message' , 'channel' ) ) ;
597
- assert . ok ( patternListener . calledOnceWithExactly ( 'message' , 'channel' ) ) ;
645
+ await Promise . all ( [
646
+ once ( subscriber , 'error' ) ,
647
+ publisher . sendCommand ( [ 'CLIENT' , 'KILL' , 'SKIPME' , 'yes' ] )
648
+ ] ) ;
598
649
599
- await subscriber . unsubscribe ( 'channel' , channelListener1 , true ) ;
600
- await Promise . all ( [
601
- waitTillBeenCalled ( channelListener2 ) ,
602
- waitTillBeenCalled ( patternListener ) ,
603
- publisher . publish ( 'channel' , 'message' )
604
- ] ) ;
605
- assert . ok ( channelListener1 . calledOnce ) ;
606
- assert . ok ( channelListener2 . calledTwice ) ;
607
- assert . ok ( channelListener2 . secondCall . calledWithExactly ( 'message' , 'channel' ) ) ;
608
- assert . ok ( patternListener . calledTwice ) ;
609
- assert . ok ( patternListener . secondCall . calledWithExactly ( 'message' , 'channel' ) ) ;
610
- await subscriber . unsubscribe ( 'channel' ) ;
611
- await Promise . all ( [
612
- waitTillBeenCalled ( patternListener ) ,
613
- publisher . publish ( 'channel' , 'message' )
614
- ] ) ;
615
- assert . ok ( channelListener1 . calledOnce ) ;
616
- assert . ok ( channelListener2 . calledTwice ) ;
617
- assert . ok ( patternListener . calledThrice ) ;
618
- assert . ok ( patternListener . thirdCall . calledWithExactly ( 'message' , 'channel' ) ) ;
619
- await subscriber . pUnsubscribe ( ) ;
620
- await publisher . publish ( 'channel' , 'message' ) ;
621
- assert . ok ( channelListener1 . calledOnce ) ;
622
- assert . ok ( channelListener2 . calledTwice ) ;
623
- assert . ok ( patternListener . calledThrice ) ;
624
- // should be able to send commands when unsubsribed from all channels (see #1652)
625
- await assert . doesNotReject ( subscriber . ping ( ) ) ;
626
- } finally {
627
- await subscriber . disconnect ( ) ;
628
- }
629
- } , GLOBAL . SERVERS . OPEN ) ;
650
+ await once ( subscriber , 'ready' ) ;
651
+
652
+ await Promise . all ( [
653
+ waitTillBeenCalled ( listener ) ,
654
+ publisher . publish ( 'channel' , 'message' )
655
+ ] ) ;
656
+ } finally {
657
+ await subscriber . disconnect ( ) ;
658
+ }
659
+ } , GLOBAL . SERVERS . OPEN ) ;
660
+ } ) ;
630
661
631
662
testUtils . testWithClient ( 'ConnectionTimeoutError' , async client => {
632
663
const promise = assert . rejects ( client . connect ( ) , ConnectionTimeoutError ) ,
0 commit comments