Skip to content

Commit be6f49e

Browse files
committed
test: deflake connect-timeout watchdog
1 parent 9c12e26 commit be6f49e

1 file changed

Lines changed: 37 additions & 38 deletions

File tree

test/connect-timeout.js

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const { tspl } = require('@matteo.collina/tspl')
4-
const { test, after, describe } = require('node:test')
4+
const { test, describe } = require('node:test')
55
const { Client, Pool, errors } = require('..')
66
const net = require('node:net')
77
const assert = require('node:assert')
@@ -32,52 +32,51 @@ net.connect = function (options) {
3232
return new net.Socket(options)
3333
}
3434

35-
test('connect-timeout', { skip }, async t => {
36-
t = tspl(t, { plan: 3 })
37-
38-
const client = new Client('http://localhost:9000', {
39-
connectTimeout: 1e3
35+
async function assertConnectTimeout (dispatcher, t) {
36+
const tt = tspl(t, { plan: 3 })
37+
38+
await new Promise((resolve, reject) => {
39+
// Connection timeouts use FastTimers, which have a deliberately low
40+
// resolution, and Windows adds an extra setImmediate before timing out.
41+
const timeout = setTimeout(() => {
42+
dispatcher.destroy()
43+
reject(new Error('connect-timeout callback did not fire'))
44+
}, 5e3)
45+
46+
dispatcher.request({
47+
path: '/',
48+
method: 'GET'
49+
}, (err) => {
50+
try {
51+
tt.ok(err instanceof errors.ConnectTimeoutError)
52+
tt.strictEqual(err.code, 'UND_ERR_CONNECT_TIMEOUT')
53+
tt.strictEqual(err.message, 'Connect Timeout Error (attempted address: localhost:9000, timeout: 1000ms)')
54+
clearTimeout(timeout)
55+
resolve()
56+
} catch (error) {
57+
clearTimeout(timeout)
58+
reject(error)
59+
}
60+
})
4061
})
41-
after(() => client.close())
4262

43-
const timeout = setTimeout(() => {
44-
t.fail()
45-
}, 2e3)
63+
await tt.completed
64+
}
4665

47-
client.request({
48-
path: '/',
49-
method: 'GET'
50-
}, (err) => {
51-
t.ok(err instanceof errors.ConnectTimeoutError)
52-
t.strictEqual(err.code, 'UND_ERR_CONNECT_TIMEOUT')
53-
t.strictEqual(err.message, 'Connect Timeout Error (attempted address: localhost:9000, timeout: 1000ms)')
54-
clearTimeout(timeout)
66+
test('connect-timeout (Client)', { skip }, async t => {
67+
const client = new Client('http://localhost:9000', {
68+
connectTimeout: 1e3
5569
})
70+
t.after(() => client.close().catch(() => {}))
5671

57-
await t.completed
72+
await assertConnectTimeout(client, t)
5873
})
5974

60-
test('connect-timeout', { skip }, async t => {
61-
t = tspl(t, { plan: 3 })
62-
75+
test('connect-timeout (Pool)', { skip }, async t => {
6376
const client = new Pool('http://localhost:9000', {
6477
connectTimeout: 1e3
6578
})
66-
after(() => client.close())
67-
68-
const timeout = setTimeout(() => {
69-
t.fail()
70-
}, 2e3)
71-
72-
client.request({
73-
path: '/',
74-
method: 'GET'
75-
}, (err) => {
76-
t.ok(err instanceof errors.ConnectTimeoutError)
77-
t.strictEqual(err.code, 'UND_ERR_CONNECT_TIMEOUT')
78-
t.strictEqual(err.message, 'Connect Timeout Error (attempted address: localhost:9000, timeout: 1000ms)')
79-
clearTimeout(timeout)
80-
})
79+
t.after(() => client.close().catch(() => {}))
8180

82-
await t.completed
81+
await assertConnectTimeout(client, t)
8382
})

0 commit comments

Comments
 (0)