From aa34b439095770d0935439718a683b43fbbbf1ad Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Tue, 10 May 2016 07:56:52 +0200 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/6607 Fixes: https://github.com/nodejs/node/issues/6577 Reviewed-By: Ben Noordhuis Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Santiago Gimeno Reviewed-By: Matteo Collina --- test/parallel/test-dgram-send-default-host.js | 20 +++++++++----- .../test-dgram-udp6-send-default-host.js | 27 +++++++++++-------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/test/parallel/test-dgram-send-default-host.js b/test/parallel/test-dgram-send-default-host.js index e6b6d7c9b45..692c088746b 100644 --- a/test/parallel/test-dgram-send-default-host.js +++ b/test/parallel/test-dgram-send-default-host.js @@ -11,20 +11,26 @@ const toSend = [Buffer.alloc(256, 'x'), Buffer.alloc(256, 'z'), 'hello']; -client.on('listening', function() { +const received = []; + +client.on('listening', common.mustCall(() => { client.send(toSend[0], 0, toSend[0].length, common.PORT); client.send(toSend[1], common.PORT); client.send([toSend[2]], common.PORT); client.send(toSend[3], 0, toSend[3].length, common.PORT); -}); +})); -client.on('message', function(buf, info) { - const expected = toSend.shift().toString(); - assert.ok(buf.toString() === expected, 'message was received correctly'); +client.on('message', common.mustCall((buf, info) => { + received.push(buf.toString()); - if (toSend.length === 0) { + if (received.length === toSend.length) { + // The replies may arrive out of order -> sort them before checking. + received.sort(); + + const expected = toSend.map(String).sort(); + assert.deepStrictEqual(received, expected); client.close(); } -}); +}, toSend.length)); client.bind(common.PORT); diff --git a/test/parallel/test-dgram-udp6-send-default-host.js b/test/parallel/test-dgram-udp6-send-default-host.js index f482f8b428b..7e72ea57018 100644 --- a/test/parallel/test-dgram-udp6-send-default-host.js +++ b/test/parallel/test-dgram-udp6-send-default-host.js @@ -11,26 +11,31 @@ if (!common.hasIPv6) { const client = dgram.createSocket('udp6'); -const toSend = [new Buffer(256), new Buffer(256), new Buffer(256), 'hello']; +const toSend = [Buffer.alloc(256, 'x'), + Buffer.alloc(256, 'y'), + Buffer.alloc(256, 'z'), + 'hello']; -toSend[0].fill('x'); -toSend[1].fill('y'); -toSend[2].fill('z'); +const received = []; -client.on('listening', function() { +client.on('listening', common.mustCall(() => { client.send(toSend[0], 0, toSend[0].length, common.PORT); client.send(toSend[1], common.PORT); client.send([toSend[2]], common.PORT); client.send(toSend[3], 0, toSend[3].length, common.PORT); -}); +})); -client.on('message', function(buf, info) { - const expected = toSend.shift().toString(); - assert.ok(buf.toString() === expected, 'message was received correctly'); +client.on('message', common.mustCall((buf, info) => { + received.push(buf.toString()); - if (toSend.length === 0) { + if (received.length === toSend.length) { + // The replies may arrive out of order -> sort them before checking. + received.sort(); + + const expected = toSend.map(String).sort(); + assert.deepStrictEqual(received, expected); client.close(); } -}); +}, toSend.length)); client.bind(common.PORT);