Skip to content

Commit 01f010f

Browse files
addaleaxevanlucas
authored andcommitted
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. PR-URL: #6607Fixes: #6577 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Santiago Gimeno <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent be241c3 commit 01f010f

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

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

Lines changed: 13 additions & 7 deletions
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+
constreceived=[];
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-
constexpected=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+
constexpected=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‎

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,31 @@ if (!common.hasIPv6){
1111

1212
constclient=dgram.createSocket('udp6');
1313

14-
consttoSend=[newBuffer(256),newBuffer(256),newBuffer(256),'hello'];
14+
consttoSend=[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+
constreceived=[];
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-
constexpected=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+
constexpected=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)