Skip to content

Commit 57ac4de

Browse files
authored
test: synchronize the issue-3356 body timeout (#5834)
The test races its forced body timeout against both the first response chunk and a native response-completion timer. A byte-zero retry can succeed, while an early response completion can prevent the expected rejection. An open RetryAgent can also keep server teardown pending after the assertion completes. Signed-off-by: Ruben Bridgewater <[email protected]>
1 parent 6d44174 commit 57ac4de

1 file changed

Lines changed: 14 additions & 16 deletions

File tree

‎test/issue-3356.js‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const { tick: fastTimersTick } = require('../lib/util/timers')
88
const { fetch, Agent, RetryAgent } = require('..')
99

1010
test('https://github.com/nodejs/undici/issues/3356', async (t) => {
11-
t = tspl(t, { plan: 3 })
11+
t = tspl(t, { plan: 2 })
1212

1313
let shouldRetry = true
1414
const server = createServer()
@@ -19,7 +19,6 @@ test('https://github.com/nodejs/undici/issues/3356', async (t) => {
1919

2020
res.flushHeaders()
2121
res.write('h')
22-
setTimeout(() => { res.end('ello world!') }, 100)
2322
} else {
2423
res.end('hello world!')
2524
}
@@ -29,32 +28,31 @@ test('https://github.com/nodejs/undici/issues/3356', async (t) => {
2928

3029
await once(server, 'listening')
3130

31+
const agent = new RetryAgent(new Agent({ bodyTimeout: 50 }), {
32+
errorCodes: ['UND_ERR_BODY_TIMEOUT']
33+
})
34+
3235
after(async () => {
36+
await agent.close()
3337
server.close()
3438

3539
await once(server, 'close')
3640
})
3741

38-
const agent = new RetryAgent(new Agent({ bodyTimeout: 50 }), {
39-
errorCodes: ['UND_ERR_BODY_TIMEOUT']
40-
})
41-
4242
const response = await fetch(`http://localhost:${server.address().port}`, {
4343
dispatcher: agent
4444
})
45+
const reader = response.body.getReader()
46+
47+
await reader.read()
4548

4649
fastTimersTick()
4750

48-
setTimeout(async () => {
49-
try {
50-
t.equal(response.status, 200)
51-
// consume response
52-
await response.text()
53-
} catch (err) {
54-
t.equal(err.name, 'TypeError')
55-
t.equal(err.cause.code, 'UND_ERR_REQ_RETRY')
56-
}
57-
}, 200)
51+
t.equal(response.status, 200)
52+
await t.rejects(reader.read(), /** @param {Error & { cause: { code: string } }} error */ (error) => {
53+
return error.name === 'TypeError' &&
54+
error.cause.code === 'UND_ERR_REQ_RETRY'
55+
})
5856

5957
await t.completed
6058
})

0 commit comments

Comments
 (0)