Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions test/parallel/test-dgram-send-default-host.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', common.mustCall((buf, info) => {
received.push(buf.toString());

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

if (toSend.length === 0) {
const expected = toSend.map(String).sort();
assert.deepStrictEqual(received, expected);
client.close();
}
});
}, toSend.length));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you passing toSend.length here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making sure that the message event is invoked for each message – otherwise the test would accept messages just being dropped.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies, I missed it was an argument to mustCall.


client.bind(common.PORT);
27 changes: 16 additions & 11 deletions test/parallel/test-dgram-udp6-send-default-host.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question.


client.bind(common.PORT);