Skip to content

Commit 5b2cd44

Browse files
Trottcodebytere
authored andcommitted
test: fix test-net-throttle
Repeat writes until data is queued in memory, rather than assuming that it will happen by a certain point. Fixes: #33135 PR-URL: #33329 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Juan José Arboleda <[email protected]>
1 parent 0d77eec commit 5b2cd44

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

test/pummel/test-net-throttle.js

+25-16
Original file line numberDiff line numberDiff line change
@@ -23,44 +23,53 @@
2323
require('../common');
2424
const assert = require('assert');
2525
const net = require('net');
26+
const debuglog = require('util').debuglog('test');
2627

27-
const N = 1024 * 1024;
28-
const part_N = N / 3;
2928
let chars_recved = 0;
3029
let npauses = 0;
31-
32-
console.log('build big string');
33-
const body = 'C'.repeat(N);
30+
let totalLength = 0;
3431

3532
const server = net.createServer((connection) => {
36-
connection.write(body.slice(0, part_N));
37-
connection.write(body.slice(part_N, 2 * part_N));
38-
assert.strictEqual(connection.write(body.slice(2 * part_N, N)), false);
39-
console.log(`bufferSize: ${connection.bufferSize}`, 'expecting', N);
40-
assert.ok(connection.bufferSize >= 0 &&
41-
connection.writableLength <= N);
33+
const body = 'C'.repeat(1024);
34+
let n = 1;
35+
debuglog('starting write loop');
36+
while (connection.write(body)) {
37+
n++;
38+
}
39+
debuglog('ended write loop');
40+
// Now that we're throttled, do some more writes to make sure the data isn't
41+
// lost.
42+
connection.write(body);
43+
connection.write(body);
44+
n += 2;
45+
totalLength = n * body.length;
46+
assert.ok(connection.bufferSize >= 0, `bufferSize: ${connection.bufferSize}`);
47+
assert.ok(
48+
connection.writableLength <= totalLength,
49+
`writableLength: ${connection.writableLength}, totalLength: ${totalLength}`
50+
);
4251
connection.end();
4352
});
4453

4554
server.listen(0, () => {
4655
const port = server.address().port;
47-
console.log(`server started on port ${port}`);
56+
debuglog(`server started on port ${port}`);
4857
let paused = false;
4958
const client = net.createConnection(port);
5059
client.setEncoding('ascii');
5160
client.on('data', (d) => {
5261
chars_recved += d.length;
53-
console.log(`got ${chars_recved}`);
62+
debuglog(`got ${chars_recved}`);
5463
if (!paused) {
5564
client.pause();
5665
npauses += 1;
5766
paused = true;
58-
console.log('pause');
67+
debuglog('pause');
5968
const x = chars_recved;
6069
setTimeout(() => {
6170
assert.strictEqual(chars_recved, x);
6271
client.resume();
63-
console.log('resume');
72+
debuglog('resume');
6473
paused = false;
6574
}, 100);
6675
}
@@ -74,6 +83,6 @@ server.listen(0, () => {
7483

7584

7685
process.on('exit', () => {
77-
assert.strictEqual(chars_recved, N);
86+
assert.strictEqual(chars_recved, totalLength);
7887
assert.strictEqual(npauses > 2, true);
7988
});

0 commit comments

Comments
 (0)