Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions test/eventsource/eventsource-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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'

Expand All @@ -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'

Expand All @@ -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`)
})
})
Loading