forked from nodejs/undici
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient-reconnect.js
57 lines (48 loc) · 1.36 KB
/
client-reconnect.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'use strict'
const { tspl } = require('@matteo.collina/tspl')
const { test, after } = require('node:test')
const { once } = require('node:events')
const { Client } = require('..')
const { createServer } = require('node:http')
const FakeTimers = require('@sinonjs/fake-timers')
const timers = require('../lib/util/timers')
test('multiple reconnect', async (t) => {
t = tspl(t, { plan: 5 })
let n = 0
const clock = FakeTimers.install()
after(() => clock.uninstall())
const orgTimers = { ...timers }
Object.assign(timers, { setTimeout, clearTimeout })
after(() => {
Object.assign(timers, orgTimers)
})
const server = createServer((req, res) => {
n === 0 ? res.destroy() : res.end('ok')
})
after(() => server.close())
server.listen(0)
await once(server, 'listening')
const client = new Client(`http://localhost:${server.address().port}`)
after(client.destroy.bind(client))
client.request({ path: '/', method: 'GET' }, (err, data) => {
t.ok(err)
t.strictEqual(err.code, 'UND_ERR_SOCKET')
})
client.request({ path: '/', method: 'GET' }, (err, data) => {
t.ifError(err)
data.body
.resume()
.on('end', () => {
t.ok(true, 'pass')
})
})
client.on('disconnect', () => {
if (++n === 1) {
t.ok(true, 'pass')
}
process.nextTick(() => {
clock.tick(1000)
})
})
await t.completed
})