@@ -9,6 +9,7 @@ import { MATH_FUNCTION, loadMathFunction } from '../commands/FUNCTION_LOAD.spec'
9
9
import { RESP_TYPES } from '../RESP/decoder' ;
10
10
import { BlobStringReply , NumberReply } from '../RESP/types' ;
11
11
import { SortedSetMember } from '../commands/generic-transformers' ;
12
+ import { createClient } from '../..' ;
12
13
13
14
export const SQUARE_SCRIPT = defineScript ( {
14
15
SCRIPT :
@@ -769,4 +770,79 @@ describe('Client', () => {
769
770
}
770
771
} , GLOBAL . SERVERS . OPEN ) ;
771
772
} ) ;
773
+
774
+ describe ( 'Push Handlers' , ( ) => {
775
+ testUtils . testWithClient ( 'RESP2: add/remove invalidate handler, and validate its called' , async client => {
776
+ const key = 'x'
777
+
778
+ const duplicate = await client . duplicate ( ) . connect ( ) ;
779
+ try {
780
+ const id = await duplicate . clientId ( ) ;
781
+
782
+ let nodeResolve ;
783
+
784
+ const promise = new Promise ( ( res ) => {
785
+ nodeResolve = res ;
786
+ } ) ;
787
+
788
+ duplicate . addPushHandler ( "invalidate" , ( push : Array < any > ) => {
789
+ assert . equal ( push [ 0 ] . toString ( ) , "invalidate" ) ;
790
+ assert . notEqual ( push [ 1 ] , null ) ;
791
+ assert . equal ( push [ 1 ] . length , 1 ) ;
792
+ assert . equal ( push [ 1 ] [ 0 ] . toString ( ) , key ) ;
793
+ // this test removing the handler,
794
+ // as flushAll in cleanup of test will issue a full invalidate,
795
+ // which would fail if this handler is called on it
796
+ duplicate . removePushHandler ( "invalidate" ) ;
797
+ nodeResolve ( ) ;
798
+ } )
799
+
800
+ await client . sendCommand ( [ 'CLIENT' , 'TRACKING' , 'ON' , 'REDIRECT' , id . toString ( ) ] ) ;
801
+ await client . get ( key ) ;
802
+ await client . set ( key , '1' ) ;
803
+
804
+ // force an invalidate all
805
+ await client . flushAll ( ) ;
806
+
807
+ await nodeResolve ;
808
+ } finally {
809
+ duplicate . destroy ( ) ;
810
+ }
811
+ } , {
812
+ ...GLOBAL . SERVERS . OPEN
813
+ } ) ;
814
+
815
+ testUtils . testWithClient ( 'RESP3: add/remove invalidate handler, and validate its called' , async client => {
816
+ const key = 'x'
817
+
818
+ let nodeResolve ;
819
+
820
+ const promise = new Promise ( ( res ) => {
821
+ nodeResolve = res ;
822
+ } ) ;
823
+
824
+ client . addPushHandler ( "invalidate" , ( push : Array < any > ) => {
825
+ assert . equal ( push [ 0 ] . toString ( ) , "invalidate" ) ;
826
+ assert . equal ( push [ 1 ] . length , 1 ) ;
827
+ assert . equal ( push [ 1 ] . length , 1 ) ;
828
+ assert . equal ( push [ 1 ] [ 0 ] . toString ( ) , key ) ;
829
+ // this test removing the handler,
830
+ // as flushAll in cleanup of test will issue a full invalidate,
831
+ // which would fail if this handler is called on it
832
+ client . removePushHandler ( "invalidate" ) ;
833
+ nodeResolve ( ) ;
834
+ } )
835
+
836
+ await client . sendCommand ( [ 'CLIENT' , 'TRACKING' , 'ON' ] ) ;
837
+ await client . get ( key ) ;
838
+ await client . set ( key , '1' ) ;
839
+
840
+ await nodeResolve ;
841
+ } , {
842
+ ...GLOBAL . SERVERS . OPEN ,
843
+ clientOptions : {
844
+ RESP : 3
845
+ }
846
+ } ) ;
847
+ } ) ;
772
848
} ) ;
0 commit comments