Skip to content

Commit 0cb68ce

Browse files
committed
test: keep the body queued by corking the socket
Instead of writing a large body and hoping it gets buffered, simulate buffering by using `cork`, and temporarily stop `uncork` calls from having effect. This should make the test work on Windows. Signed-off-by: David Glasser <glasser@apollographql.com>
1 parent 41c4adf commit 0cb68ce

1 file changed

Lines changed: 24 additions & 8 deletions

File tree

‎test/parallel/test-http-server-close-idle-pending-response.js‎

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,38 @@ const common = require('../common');
1010
const assert = require('assert');
1111
const { createServer, get } = require('http');
1212

13-
// Larger than the socket buffers, so that part of the body is still queued in
14-
// Node when end() returns.
15-
const size = 32 * 1024 * 1024;
13+
const body = 'hello';
1614

1715
const server = createServer(common.mustCall((req, res) => {
1816
// Wait for the request to be fully received, so that the connection is
1917
// idle as far as the parser is concerned and the sweep considers it.
2018
req.resume();
2119
req.on('end', common.mustCall(() => {
22-
res.writeHead(200, { 'Content-Length': size });
23-
res.end(Buffer.alloc(size), common.mustCall());
20+
const socket = res.socket;
21+
res.writeHead(200, { 'Content-Length': body.length });
22+
res.flushHeaders();
23+
24+
// Keep the body queued in the socket's write buffer while server.close()
25+
// runs: cork the socket, and make the uncork() that end() performs a
26+
// no-op. (Writing a body larger than the socket buffers is not a reliable
27+
// way to leave data queued, because on some platforms, including Windows,
28+
// the kernel may accept all of it synchronously.)
29+
socket.cork();
30+
// uncork() is inherited, so it is restored below by deleting the override.
31+
assert.ok(!Object.hasOwn(socket, 'uncork'));
32+
socket.uncork = () => {};
33+
res.end(body, common.mustCall());
2434
assert.strictEqual(res.finished, true);
2535
assert.strictEqual(res.writableFinished, false);
36+
assert.ok(socket.writableLength >= body.length);
2637

2738
// The response is still being written, so this must leave the connection
2839
// alone and wait for it.
2940
server.close(common.mustCall());
41+
42+
// Now let the body be written.
43+
delete socket.uncork;
44+
socket.uncork();
3045
}));
3146
}));
3247

@@ -36,13 +51,14 @@ server.listen(0, common.mustCall(() => {
3651
// keep-alive connection to time out.
3752
get({ port: server.address().port, agent: false }, common.mustCall((res) => {
3853
assert.strictEqual(res.statusCode, 200);
39-
let received = 0;
54+
let received = '';
55+
res.setEncoding('utf8');
4056
res.on('data', (chunk) => {
41-
received += chunk.length;
57+
received += chunk;
4258
});
4359
res.on('error', common.mustNotCall());
4460
res.on('end', common.mustCall(() => {
45-
assert.strictEqual(received, size);
61+
assert.strictEqual(received, body);
4662
}));
4763
}));
4864
}));

0 commit comments

Comments
 (0)