@@ -68,71 +68,74 @@ describe("Exponential backoff delay", () => {
6868 describe ( "with rate 2, backoffLimit 8000 ms" , ( ) => {
6969 // The initial delay shall be 1 s
7070 const initialDelay = 1000 ;
71- const exponentialBackoffParams : ExponentialBackoffParams = {
71+ const exponentialBackoff : ExponentialBackoffParams = {
7272 backoffRate : 2 ,
7373 // We put the ceiling at exactly 8000 ms
7474 backoffLimit : 8000 ,
7575 } ;
76+ const attempts : [ number , number ] [ ] = [
77+ [ 1000 , 0 ] ,
78+ [ 2000 , 1 ] ,
79+ [ 4000 , 2 ] ,
80+ [ 8000 , 3 ] ,
81+ [ 8000 , 4 ] ,
82+ ] ;
7683 it ( "will never be more than 8000 ms with rate set to 2" , ( ) => {
77- expect (
78- calculateRetryDelayFactor ( exponentialBackoffParams , initialDelay , 0 ) ,
79- ) . toBe ( 1000 ) ;
80- expect (
81- calculateRetryDelayFactor ( exponentialBackoffParams , initialDelay , 1 ) ,
82- ) . toBe ( 2000 ) ;
83- expect (
84- calculateRetryDelayFactor ( exponentialBackoffParams , initialDelay , 2 ) ,
85- ) . toBe ( 4000 ) ;
86- expect (
87- calculateRetryDelayFactor ( exponentialBackoffParams , initialDelay , 3 ) ,
88- ) . toBe ( 8000 ) ;
89- expect (
90- calculateRetryDelayFactor ( exponentialBackoffParams , initialDelay , 4 ) ,
91- ) . toBe ( 8000 ) ;
84+ attempts . forEach ( ( [ delay , failedAttempts ] ) => {
85+ expect (
86+ calculateRetryDelayFactor (
87+ exponentialBackoff ,
88+ initialDelay ,
89+ failedAttempts ,
90+ ) ,
91+ ) . toBe ( delay ) ;
92+ } ) ;
9293 } ) ;
9394
9495 it ( "should delay reconnection attempts exponentially" , async ( ) => {
96+ // Somehow we need to convincen typescript here that "WebSocket" is
97+ // totally valid. Could be because it doesn't assume WebSocket is part of
98+ // global / the index key is missing
9599 const webSocketSpy = jest . spyOn ( global , "WebSocket" as any ) ;
96100 webSocketSpy . mockImplementation ( ( ) => { } ) ;
97101 const setTimeoutSpy = jest . spyOn ( global , "setTimeout" ) ;
98- const sarus = new Sarus ( {
99- url,
100- exponentialBackoff : exponentialBackoffParams ,
101- } ) ;
102+ const sarus = new Sarus ( { url, exponentialBackoff } ) ;
102103 expect ( sarus . state ) . toStrictEqual ( {
103104 kind : "connecting" ,
104105 failedConnectionAttempts : 0 ,
105106 } ) ;
106107 let instance : WebSocket ;
108+ // Get the first WebSocket instance, and ...
107109 [ instance ] = webSocketSpy . mock . instances ;
108110 if ( ! instance . onopen ) {
109111 throw new Error ( ) ;
110112 }
113+ // tell the sarus instance that it is open, and ...
111114 instance . onopen ( new Event ( "open" ) ) ;
112115 if ( ! instance . onclose ) {
113116 throw new Error ( ) ;
114117 }
118+ // close it immediately
115119 instance . onclose ( new CloseEvent ( "close" ) ) ;
120+ expect ( sarus . state ) . toStrictEqual ( {
121+ kind : "closed" ,
122+ failedConnectionAttempts : 0 ,
123+ } ) ;
116124
117125 let cb : Sarus [ "connect" ] ;
118126 // We iteratively call sarus.connect() and let it fail, seeing
119127 // if it reaches 8000 as a delay and stays there
120- const attempts : [ number , number ] [ ] = [
121- [ 1000 , 1 ] ,
122- [ 2000 , 2 ] ,
123- [ 4000 , 3 ] ,
124- [ 8000 , 4 ] ,
125- [ 8000 , 5 ] ,
126- ] ;
127- attempts . forEach ( ( [ delay , failedAttempts ] : [ number , number ] ) => {
128+ attempts . forEach ( ( [ delay , failedAttempts ] ) => {
128129 const call =
129130 setTimeoutSpy . mock . calls [ setTimeoutSpy . mock . calls . length - 1 ] ;
130131 if ( ! call ) {
131132 throw new Error ( ) ;
132133 }
134+ // Make sure that setTimeout was called with the correct delay
133135 expect ( call [ 1 ] ) . toBe ( delay ) ;
134136 cb = call [ 0 ] ;
135137 cb ( ) ;
138+ // Get the most recent WebSocket instance
136139 instance =
137140 webSocketSpy . mock . instances [ webSocketSpy . mock . instances . length - 1 ] ;
138141 if ( ! instance . onclose ) {
@@ -141,7 +144,7 @@ describe("Exponential backoff delay", () => {
141144 instance . onclose ( new CloseEvent ( "close" ) ) ;
142145 expect ( sarus . state ) . toStrictEqual ( {
143146 kind : "connecting" ,
144- failedConnectionAttempts : failedAttempts ,
147+ failedConnectionAttempts : failedAttempts + 1 ,
145148 } ) ;
146149 } ) ;
147150 } ) ;
0 commit comments