Skip to content

Commit 0b46a3d

Browse files
authored
test: wait for inflight-and-close body cleanup (#5261)
1 parent 397bc13 commit 0b46a3d

1 file changed

Lines changed: 29 additions & 21 deletions

File tree

test/inflight-and-close.js

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
'use strict'
22

33
const { tspl } = require('@matteo.collina/tspl')
4-
const { test } = require('node:test')
4+
const { test, after } = require('node:test')
55
const { request } = require('..')
66
const http = require('node:http')
7+
const { once } = require('node:events')
78

89
test('inflight and close', async (t) => {
910
t = tspl(t, { plan: 3 })
@@ -12,26 +13,33 @@ test('inflight and close', async (t) => {
1213
res.writeHead(200)
1314
res.end('Response body')
1415
res.socket.end() // Close the connection immediately with every response
15-
}).listen(0, '127.0.0.1', function () {
16-
const url = `http://127.0.0.1:${this.address().port}`
17-
request(url)
18-
.then(({ statusCode, headers, body }) => {
19-
t.ok(true, 'first response')
20-
body.resume()
21-
body.on('close', function () {
22-
t.ok(true, 'first body closed')
23-
})
24-
return request(url)
25-
.then(({ statusCode, headers, body }) => {
26-
t.ok(true, 'second response')
27-
body.resume()
28-
body.on('close', function () {
29-
server.close()
30-
})
31-
})
32-
}).catch((err) => {
33-
t.ifError(err)
34-
})
3516
})
17+
18+
after(() => server.close())
19+
20+
server.listen(0, '127.0.0.1')
21+
await once(server, 'listening')
22+
23+
const url = `http://127.0.0.1:${server.address().port}`
24+
25+
const first = await request(url)
26+
t.ok(true, 'first response')
27+
28+
const firstBodyClosed = once(first.body, 'close').then(() => {
29+
t.ok(true, 'first body closed')
30+
})
31+
first.body.resume()
32+
33+
const second = await request(url)
34+
t.ok(true, 'second response')
35+
36+
const secondBodyClosed = once(second.body, 'close')
37+
second.body.resume()
38+
39+
await Promise.all([
40+
firstBodyClosed,
41+
secondBodyClosed
42+
])
43+
3644
await t.completed
3745
})

0 commit comments

Comments
 (0)