Skip to content

Commit ca967f9

Browse files
authored
test: drive request timeout ticks after connect (#5251)
1 parent 827cac7 commit ca967f9

1 file changed

Lines changed: 58 additions & 41 deletions

File tree

test/request-timeout.js

Lines changed: 58 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const { resolve: pathResolve } = require('node:path')
55
const { test, after, beforeEach } = require('node:test')
66
const { createReadStream, writeFileSync, unlinkSync } = require('node:fs')
77
const { Client, errors } = require('..')
8-
const { kConnect } = require('../lib/core/symbols')
98
const { createServer } = require('node:http')
109
const EventEmitter = require('node:events')
1110
const FakeTimers = require('@sinonjs/fake-timers')
@@ -25,6 +24,12 @@ beforeEach(() => {
2524
resetFastTimers()
2625
})
2726

27+
function tickOnConnect (client, tick) {
28+
client.once('connect', () => {
29+
process.nextTick(tick)
30+
})
31+
}
32+
2833
test('request timeout', async (t) => {
2934
t = tspl(t, { plan: 1 })
3035

@@ -89,6 +94,11 @@ test('body timeout', async (t) => {
8994
const client = new Client(`http://localhost:${server.address().port}`, { bodyTimeout: 50 })
9095
after(() => client.destroy())
9196

97+
tickOnConnect(client, () => {
98+
clock.tick(50)
99+
fastTimersTick(50)
100+
})
101+
92102
client.request({ path: '/', method: 'GET' }, (err, { body }) => {
93103
t.ifError(err)
94104
body.on('data', () => {
@@ -98,9 +108,6 @@ test('body timeout', async (t) => {
98108
t.ok(err instanceof errors.BodyTimeoutError)
99109
})
100110
})
101-
102-
clock.tick(50)
103-
fastTimersTick(50)
104111
})
105112

106113
await t.completed
@@ -128,12 +135,14 @@ test('overridden request timeout', async (t) => {
128135
const client = new Client(`http://localhost:${server.address().port}`, { headersTimeout: 500 })
129136
after(() => client.destroy())
130137

138+
tickOnConnect(client, () => {
139+
clock.tick(50)
140+
fastTimersTick(50)
141+
})
142+
131143
client.request({ path: '/', method: 'GET', headersTimeout: 50 }, (err, response) => {
132144
t.ok(err instanceof errors.HeadersTimeoutError)
133145
})
134-
135-
clock.tick(50)
136-
fastTimersTick(50)
137146
})
138147

139148
await t.completed
@@ -157,6 +166,11 @@ test('overridden body timeout', async (t) => {
157166
const client = new Client(`http://localhost:${server.address().port}`, { bodyTimeout: 500 })
158167
after(() => client.destroy())
159168

169+
tickOnConnect(client, () => {
170+
fastTimersTick()
171+
fastTimersTick()
172+
})
173+
160174
client.request({ path: '/', method: 'GET', bodyTimeout: 50 }, (err, { body }) => {
161175
t.ifError(err)
162176
body.on('data', () => {
@@ -166,9 +180,6 @@ test('overridden body timeout', async (t) => {
166180
t.ok(err instanceof errors.BodyTimeoutError)
167181
})
168182
})
169-
170-
fastTimersTick()
171-
fastTimersTick()
172183
})
173184

174185
await t.completed
@@ -199,12 +210,14 @@ test('With EE signal', async (t) => {
199210
const ee = new EventEmitter()
200211
after(() => client.destroy())
201212

213+
tickOnConnect(client, () => {
214+
clock.tick(50)
215+
fastTimersTick(50)
216+
})
217+
202218
client.request({ path: '/', method: 'GET', signal: ee }, (err, response) => {
203219
t.ok(err instanceof errors.HeadersTimeoutError)
204220
})
205-
206-
clock.tick(50)
207-
fastTimersTick(50)
208221
})
209222

210223
await t.completed
@@ -235,12 +248,14 @@ test('With abort-controller signal', async (t) => {
235248
const abortController = new AbortController()
236249
after(() => client.destroy())
237250

251+
tickOnConnect(client, () => {
252+
clock.tick(50)
253+
fastTimersTick(50)
254+
})
255+
238256
client.request({ path: '/', method: 'GET', signal: abortController.signal }, (err, response) => {
239257
t.ok(err instanceof errors.HeadersTimeoutError)
240258
})
241-
242-
clock.tick(50)
243-
fastTimersTick(50)
244259
})
245260

246261
await t.completed
@@ -383,12 +398,14 @@ test('Global option', async (t) => {
383398
})
384399
after(() => client.destroy())
385400

401+
tickOnConnect(client, () => {
402+
clock.tick(50)
403+
fastTimersTick(50)
404+
})
405+
386406
client.request({ path: '/', method: 'GET' }, (err, response) => {
387407
t.ok(err instanceof errors.HeadersTimeoutError)
388408
})
389-
390-
clock.tick(50)
391-
fastTimersTick(50)
392409
})
393410

394411
await t.completed
@@ -418,12 +435,14 @@ test('Request options overrides global option', async (t) => {
418435
})
419436
after(() => client.destroy())
420437

438+
tickOnConnect(client, () => {
439+
clock.tick(50)
440+
fastTimersTick(50)
441+
})
442+
421443
client.request({ path: '/', method: 'GET' }, (err, response) => {
422444
t.ok(err instanceof errors.HeadersTimeoutError)
423445
})
424-
425-
clock.tick(50)
426-
fastTimersTick(50)
427446
})
428447

429448
await t.completed
@@ -473,20 +492,18 @@ test('client.close should wait for the timeout', async (t) => {
473492
})
474493
after(() => client.destroy())
475494

495+
tickOnConnect(client, () => {
496+
clock.tick(100)
497+
fastTimersTick(100)
498+
})
499+
476500
client.request({ path: '/', method: 'GET' }, (err, response) => {
477501
t.ok(err instanceof errors.HeadersTimeoutError)
478502
})
479503

480504
client.close((err) => {
481505
t.ifError(err)
482506
})
483-
484-
client.on('connect', () => {
485-
process.nextTick(() => {
486-
clock.tick(100)
487-
fastTimersTick(100)
488-
})
489-
})
490507
})
491508

492509
await t.completed
@@ -826,21 +843,21 @@ test('client.close should not deadlock', async (t) => {
826843
})
827844
after(() => client.destroy())
828845

829-
client[kConnect](() => {
830-
client.request({
831-
path: '/',
832-
method: 'GET'
833-
}, (err, response) => {
834-
t.ok(err instanceof errors.HeadersTimeoutError)
835-
})
836-
837-
client.close((err) => {
838-
t.ifError(err)
839-
})
840-
846+
tickOnConnect(client, () => {
841847
clock.tick(100)
842848
fastTimersTick(100)
843849
})
850+
851+
client.request({
852+
path: '/',
853+
method: 'GET'
854+
}, (err, response) => {
855+
t.ok(err instanceof errors.HeadersTimeoutError)
856+
})
857+
858+
client.close((err) => {
859+
t.ifError(err)
860+
})
844861
})
845862
await t.completed
846863
})

0 commit comments

Comments
 (0)