@@ -9,6 +9,7 @@ import { MATH_FUNCTION, loadMathFunction } from '../commands/FUNCTION_LOAD.spec'
99import { RESP_TYPES } from '../RESP/decoder' ;
1010import { BlobStringReply , NumberReply } from '../RESP/types' ;
1111import { SortedSetMember } from '../commands/generic-transformers' ;
12+ import { createClient } from '../..' ;
1213
1314export const SQUARE_SCRIPT = defineScript ( {
1415 SCRIPT :
@@ -769,4 +770,79 @@ describe('Client', () => {
769770 }
770771 } , GLOBAL . SERVERS . OPEN ) ;
771772 } ) ;
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+ } ) ;
772848} ) ;
0 commit comments