|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | 3 | const { tspl } = require('@matteo.collina/tspl') |
4 | | -const { test, after, describe } = require('node:test') |
| 4 | +const { test, describe } = require('node:test') |
5 | 5 | const { Client, Pool, errors } = require('..') |
6 | 6 | const net = require('node:net') |
7 | 7 | const assert = require('node:assert') |
@@ -32,52 +32,51 @@ net.connect = function (options) { |
32 | 32 | return new net.Socket(options) |
33 | 33 | } |
34 | 34 |
|
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 | + }) |
40 | 61 | }) |
41 | | - after(() => client.close()) |
42 | 62 |
|
43 | | - const timeout = setTimeout(() => { |
44 | | - t.fail() |
45 | | - }, 2e3) |
| 63 | + await tt.completed |
| 64 | +} |
46 | 65 |
|
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 |
55 | 69 | }) |
| 70 | + t.after(() => client.close().catch(() => {})) |
56 | 71 |
|
57 | | - await t.completed |
| 72 | + await assertConnectTimeout(client, t) |
58 | 73 | }) |
59 | 74 |
|
60 | | -test('connect-timeout', { skip }, async t => { |
61 | | - t = tspl(t, { plan: 3 }) |
62 | | - |
| 75 | +test('connect-timeout (Pool)', { skip }, async t => { |
63 | 76 | const client = new Pool('http://localhost:9000', { |
64 | 77 | connectTimeout: 1e3 |
65 | 78 | }) |
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(() => {})) |
81 | 80 |
|
82 | | - await t.completed |
| 81 | + await assertConnectTimeout(client, t) |
83 | 82 | }) |
0 commit comments