33const assert = require ( 'node:assert' )
44const events = require ( 'node:events' )
55const http = require ( 'node:http' )
6- const { test, describe } = require ( 'node:test' )
6+ const { test, describe, after } = require ( 'node:test' )
7+ const FakeTimers = require ( '@sinonjs/fake-timers' )
78const { EventSource, defaultReconnectionTime } = require ( '../../lib/web/eventsource/eventsource' )
89const { randomInt } = require ( 'node:crypto' )
910
@@ -184,6 +185,9 @@ describe('EventSource - received response must have content-type to be text/even
184185 } )
185186
186187 test ( 'should try to connect again if server is unreachable' , async ( ) => {
188+ const clock = FakeTimers . install ( )
189+
190+ after ( ( ) => clock . uninstall ( ) )
187191 const reconnectionTime = defaultReconnectionTime
188192 const domain = 'bad.n' + randomInt ( 1e10 ) . toString ( 36 ) + '.proxy'
189193
@@ -193,21 +197,29 @@ describe('EventSource - received response must have content-type to be text/even
193197 eventSourceInstance . onerror = ( error ) => {
194198 onerrorCalls . push ( error )
195199 }
200+ clock . tick ( reconnectionTime )
201+
196202 await events . once ( eventSourceInstance , 'error' )
197203
198204 const start = Date . now ( )
205+ clock . tick ( reconnectionTime )
199206 await events . once ( eventSourceInstance , 'error' )
207+ clock . tick ( reconnectionTime )
200208 await events . once ( eventSourceInstance , 'error' )
209+ clock . tick ( reconnectionTime )
201210 await events . once ( eventSourceInstance , 'error' )
202211 const end = Date . now ( )
203212
204213 eventSourceInstance . close ( )
205214
206- assert . strictEqual ( end - start < ( 3.5 * reconnectionTime ) , true )
215+ assert . strictEqual ( onerrorCalls . length , 4 , 'Expected 4 error events' )
216+ assert . strictEqual ( end - start , 3 * reconnectionTime , `Expected reconnection to happen after ${ 3 * reconnectionTime } ms, but took ${ end - start } ms` )
207217 } )
208218
209- test ( 'should try to connect again if server is unreachable' , async ( ) => {
210- const reconnectionTime = 500
219+ test ( 'should try to connect again if server is unreachable, configure reconnectionTime' , async ( ) => {
220+ const reconnectionTime = 1000
221+ const clock = FakeTimers . install ( )
222+ after ( ( ) => clock . uninstall ( ) )
211223
212224 const domain = 'bad.n' + randomInt ( 1e10 ) . toString ( 36 ) + '.proxy'
213225
@@ -216,16 +228,26 @@ describe('EventSource - received response must have content-type to be text/even
216228 reconnectionTime
217229 }
218230 } )
231+
232+ const onerrorCalls = [ ]
233+ eventSourceInstance . onerror = ( error ) => {
234+ onerrorCalls . push ( error )
235+ }
236+
219237 await events . once ( eventSourceInstance , 'error' )
220238
221239 const start = Date . now ( )
240+ clock . tick ( reconnectionTime )
222241 await events . once ( eventSourceInstance , 'error' )
242+ clock . tick ( reconnectionTime )
223243 await events . once ( eventSourceInstance , 'error' )
244+ clock . tick ( reconnectionTime )
224245 await events . once ( eventSourceInstance , 'error' )
225246 const end = Date . now ( )
226247
227248 eventSourceInstance . close ( )
228249
229- assert . strictEqual ( end - start < ( 3.5 * reconnectionTime ) , true )
250+ assert . strictEqual ( onerrorCalls . length , 4 , 'Expected 4 error events' )
251+ assert . strictEqual ( end - start , 3 * reconnectionTime , `Expected reconnection to happen after ${ 3 * reconnectionTime } ms, but took ${ end - start } ms` )
230252 } )
231253} )
0 commit comments