Skip to content

Commit

Permalink
dgram: use common.mustCall and common.platformTimeout in send tests
Browse files Browse the repository at this point in the history
Fixes situations were some events were not asserted to be emitted.
Removed some flakyness from tests.
  • Loading branch information
mcollina committed May 25, 2016
1 parent 0f5f917 commit e1a4eee
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 35 deletions.
26 changes: 12 additions & 14 deletions test/parallel/test-dgram-send-callback-buffer-length.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
'use strict';
var common = require('../common');
var assert = require('assert');

var dgram = require('dgram');
var client, timer, buf, len, offset;
const common = require('../common');
const assert = require('assert');

const dgram = require('dgram');
const client = dgram.createSocket('udp4');

client = dgram.createSocket('udp4');

buf = Buffer.allocUnsafe(256);
offset = 20;

len = buf.length - offset;
const timer = setTimeout(function() {
throw new Error('Timeout');
}, common.platformTimeout(200));

const buf = Buffer.allocUnsafe(256);
const offset = 20;
const len = buf.length - offset;

client.send(buf, offset, len, common.PORT, '127.0.0.1', function(err, bytes) {
const messageSent = common.mustCall(function messageSent(err, bytes) {
assert.notEqual(bytes, buf.length);
assert.equal(bytes, buf.length - offset);
clearTimeout(timer);
client.close();
});

timer = setTimeout(function() {
throw new Error('Timeout');
}, 200);
client.send(buf, offset, len, common.PORT, '127.0.0.1', messageSent);
9 changes: 4 additions & 5 deletions test/parallel/test-dgram-send-callback-multi-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,22 @@ const timer = setTimeout(function() {
throw new Error('Timeout');
}, common.platformTimeout(200));

const onMessage = common.mustCall(function(err, bytes) {
const messageSent = common.mustCall(function messageSent(err, bytes) {
assert.equal(bytes, buf1.length + buf2.length);
clearTimeout(timer);
client.close();
});

const buf1 = Buffer.alloc(256, 'x');
const buf2 = Buffer.alloc(256, 'y');

client.on('listening', function() {
client.send([buf1, buf2], common.PORT, common.localhostIPv4, onMessage);
client.send([buf1, buf2], common.PORT, common.localhostIPv4, messageSent);
});

client.on('message', function(buf, info) {
client.on('message', common.mustCall(function onMessage(buf, info) {
const expected = Buffer.concat([buf1, buf2]);
assert.ok(buf.equals(expected), 'message was received correctly');
client.close();
});
}));

client.bind(common.PORT);
16 changes: 5 additions & 11 deletions test/parallel/test-dgram-send-empty-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,15 @@ const timer = setTimeout(function() {
throw new Error('Timeout');
}, common.platformTimeout(200));

const onMessage = common.mustCall(function(err, bytes) {
assert.equal(bytes, 0);
client.on('message', common.mustCall(function onMessage(buf, info) {
const expected = Buffer.alloc(0);
assert.ok(buf.equals(expected), 'message was received correctly');
clearTimeout(timer);
client.close();
});
}));

client.on('listening', function() {
const toSend = [];
client.send(toSend, common.PORT, common.localhostIPv4, onMessage);
});

client.on('message', function(buf, info) {
const expected = Buffer.alloc(0);
assert.ok(buf.equals(expected), 'message was received correctly');
client.close();
client.send([], common.PORT, common.localhostIPv4);
});

client.bind(common.PORT);
4 changes: 2 additions & 2 deletions test/parallel/test-dgram-send-empty-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ const client = dgram.createSocket('udp4');

client.bind(common.PORT);

client.on('message', function(buffer, bytes) {
client.on('message', common.mustCall(function onMessage(buffer, bytes) {
clearTimeout(timer);
client.close();
});
}));

const buf = Buffer.alloc(0);
client.send(buf, 0, 0, common.PORT, '127.0.0.1', function(err, len) { });
Expand Down
5 changes: 2 additions & 3 deletions test/parallel/test-dgram-send-multi-buffer-copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const timer = setTimeout(function() {
const onMessage = common.mustCall(function(err, bytes) {
assert.equal(bytes, buf1.length + buf2.length);
clearTimeout(timer);
client.close();
});

const buf1 = Buffer.alloc(256, 'x');
Expand All @@ -25,10 +24,10 @@ client.on('listening', function() {
toSend.splice(0, 2);
});

client.on('message', function(buf, info) {
client.on('message', common.mustCall(function onMessage(buf, info) {
const expected = Buffer.concat([buf1, buf2]);
assert.ok(buf.equals(expected), 'message was received correctly');
client.close();
});
}));

client.bind(common.PORT);

0 comments on commit e1a4eee

Please sign in to comment.