diff --git a/test/eventsource/eventsource-connect.js b/test/eventsource/eventsource-connect.js index 55683fc6951..95af14d5567 100644 --- a/test/eventsource/eventsource-connect.js +++ b/test/eventsource/eventsource-connect.js @@ -3,7 +3,8 @@ const assert = require('node:assert') const events = require('node:events') const http = require('node:http') -const { test, describe } = require('node:test') +const { test, describe, after } = require('node:test') +const FakeTimers = require('@sinonjs/fake-timers') const { EventSource, defaultReconnectionTime } = require('../../lib/web/eventsource/eventsource') const { randomInt } = require('node:crypto') @@ -184,6 +185,9 @@ describe('EventSource - received response must have content-type to be text/even }) test('should try to connect again if server is unreachable', async () => { + const clock = FakeTimers.install() + + after(() => clock.uninstall()) const reconnectionTime = defaultReconnectionTime const domain = 'bad.n' + randomInt(1e10).toString(36) + '.proxy' @@ -193,21 +197,29 @@ describe('EventSource - received response must have content-type to be text/even eventSourceInstance.onerror = (error) => { onerrorCalls.push(error) } + clock.tick(reconnectionTime) + await events.once(eventSourceInstance, 'error') const start = Date.now() + clock.tick(reconnectionTime) await events.once(eventSourceInstance, 'error') + clock.tick(reconnectionTime) await events.once(eventSourceInstance, 'error') + clock.tick(reconnectionTime) await events.once(eventSourceInstance, 'error') const end = Date.now() eventSourceInstance.close() - assert.strictEqual(end - start < (3.5 * reconnectionTime), true) + assert.strictEqual(onerrorCalls.length, 4, 'Expected 4 error events') + assert.strictEqual(end - start, 3 * reconnectionTime, `Expected reconnection to happen after ${3 * reconnectionTime}ms, but took ${end - start}ms`) }) - test('should try to connect again if server is unreachable', async () => { - const reconnectionTime = 500 + test('should try to connect again if server is unreachable, configure reconnectionTime', async () => { + const reconnectionTime = 1000 + const clock = FakeTimers.install() + after(() => clock.uninstall()) const domain = 'bad.n' + randomInt(1e10).toString(36) + '.proxy' @@ -216,16 +228,26 @@ describe('EventSource - received response must have content-type to be text/even reconnectionTime } }) + + const onerrorCalls = [] + eventSourceInstance.onerror = (error) => { + onerrorCalls.push(error) + } + await events.once(eventSourceInstance, 'error') const start = Date.now() + clock.tick(reconnectionTime) await events.once(eventSourceInstance, 'error') + clock.tick(reconnectionTime) await events.once(eventSourceInstance, 'error') + clock.tick(reconnectionTime) await events.once(eventSourceInstance, 'error') const end = Date.now() eventSourceInstance.close() - assert.strictEqual(end - start < (3.5 * reconnectionTime), true) + assert.strictEqual(onerrorCalls.length, 4, 'Expected 4 error events') + assert.strictEqual(end - start, 3 * reconnectionTime, `Expected reconnection to happen after ${3 * reconnectionTime}ms, but took ${end - start}ms`) }) })