Skip to content

Commit c5a8bd5

Browse files
committed
test: allow out-of-order replies in dgram tests
Allow out of order replies in the flaky `test-dgram{-upd6,}-send-default-host.js` files by sorting the incoming replies after receiving them. Fixes: #6577 PR-URL: #6607 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 7f1111b commit c5a8bd5

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

test/parallel/test-dgram-send-default-host.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,26 @@ const toSend = [Buffer.alloc(256, 'x'),
1111
Buffer.alloc(256, 'z'),
1212
'hello'];
1313

14-
client.on('listening', function() {
14+
const received = [];
15+
16+
client.on('listening', common.mustCall(() => {
1517
client.send(toSend[0], 0, toSend[0].length, common.PORT);
1618
client.send(toSend[1], common.PORT);
1719
client.send([toSend[2]], common.PORT);
1820
client.send(toSend[3], 0, toSend[3].length, common.PORT);
19-
});
21+
}));
22+
23+
client.on('message', common.mustCall((buf, info) => {
24+
received.push(buf.toString());
2025

21-
client.on('message', function(buf, info) {
22-
const expected = toSend.shift().toString();
23-
assert.ok(buf.toString() === expected, 'message was received correctly');
26+
if (received.length === toSend.length) {
27+
// The replies may arrive out of order -> sort them before checking.
28+
received.sort();
2429

25-
if (toSend.length === 0) {
30+
const expected = toSend.map(String).sort();
31+
assert.deepStrictEqual(received, expected);
2632
client.close();
2733
}
28-
});
34+
}, toSend.length));
2935

3036
client.bind(common.PORT);

test/parallel/test-dgram-udp6-send-default-host.js

+16-11
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,31 @@ if (!common.hasIPv6) {
1111

1212
const client = dgram.createSocket('udp6');
1313

14-
const toSend = [new Buffer(256), new Buffer(256), new Buffer(256), 'hello'];
14+
const toSend = [Buffer.alloc(256, 'x'),
15+
Buffer.alloc(256, 'y'),
16+
Buffer.alloc(256, 'z'),
17+
'hello'];
1518

16-
toSend[0].fill('x');
17-
toSend[1].fill('y');
18-
toSend[2].fill('z');
19+
const received = [];
1920

20-
client.on('listening', function() {
21+
client.on('listening', common.mustCall(() => {
2122
client.send(toSend[0], 0, toSend[0].length, common.PORT);
2223
client.send(toSend[1], common.PORT);
2324
client.send([toSend[2]], common.PORT);
2425
client.send(toSend[3], 0, toSend[3].length, common.PORT);
25-
});
26+
}));
2627

27-
client.on('message', function(buf, info) {
28-
const expected = toSend.shift().toString();
29-
assert.ok(buf.toString() === expected, 'message was received correctly');
28+
client.on('message', common.mustCall((buf, info) => {
29+
received.push(buf.toString());
3030

31-
if (toSend.length === 0) {
31+
if (received.length === toSend.length) {
32+
// The replies may arrive out of order -> sort them before checking.
33+
received.sort();
34+
35+
const expected = toSend.map(String).sort();
36+
assert.deepStrictEqual(received, expected);
3237
client.close();
3338
}
34-
});
39+
}, toSend.length));
3540

3641
client.bind(common.PORT);

0 commit comments

Comments
 (0)