Skip to content

Commit e1a4eee

Browse files
committed
dgram: use common.mustCall and common.platformTimeout in send tests
Fixes situations were some events were not asserted to be emitted. Removed some flakyness from tests.
1 parent 0f5f917 commit e1a4eee

5 files changed

+25
-35
lines changed
Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
'use strict';
2-
var common = require('../common');
3-
var assert = require('assert');
42

5-
var dgram = require('dgram');
6-
var client, timer, buf, len, offset;
3+
const common = require('../common');
4+
const assert = require('assert');
75

6+
const dgram = require('dgram');
7+
const client = dgram.createSocket('udp4');
88

9-
client = dgram.createSocket('udp4');
10-
11-
buf = Buffer.allocUnsafe(256);
12-
offset = 20;
13-
14-
len = buf.length - offset;
9+
const timer = setTimeout(function() {
10+
throw new Error('Timeout');
11+
}, common.platformTimeout(200));
1512

13+
const buf = Buffer.allocUnsafe(256);
14+
const offset = 20;
15+
const len = buf.length - offset;
1616

17-
client.send(buf, offset, len, common.PORT, '127.0.0.1', function(err, bytes) {
17+
const messageSent = common.mustCall(function messageSent(err, bytes) {
1818
assert.notEqual(bytes, buf.length);
1919
assert.equal(bytes, buf.length - offset);
2020
clearTimeout(timer);
2121
client.close();
2222
});
2323

24-
timer = setTimeout(function() {
25-
throw new Error('Timeout');
26-
}, 200);
24+
client.send(buf, offset, len, common.PORT, '127.0.0.1', messageSent);

test/parallel/test-dgram-send-callback-multi-buffer.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,22 @@ const timer = setTimeout(function() {
1010
throw new Error('Timeout');
1111
}, common.platformTimeout(200));
1212

13-
const onMessage = common.mustCall(function(err, bytes) {
13+
const messageSent = common.mustCall(function messageSent(err, bytes) {
1414
assert.equal(bytes, buf1.length + buf2.length);
1515
clearTimeout(timer);
16-
client.close();
1716
});
1817

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

2221
client.on('listening', function() {
23-
client.send([buf1, buf2], common.PORT, common.localhostIPv4, onMessage);
22+
client.send([buf1, buf2], common.PORT, common.localhostIPv4, messageSent);
2423
});
2524

26-
client.on('message', function(buf, info) {
25+
client.on('message', common.mustCall(function onMessage(buf, info) {
2726
const expected = Buffer.concat([buf1, buf2]);
2827
assert.ok(buf.equals(expected), 'message was received correctly');
2928
client.close();
30-
});
29+
}));
3130

3231
client.bind(common.PORT);

test/parallel/test-dgram-send-empty-array.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,15 @@ const timer = setTimeout(function() {
1515
throw new Error('Timeout');
1616
}, common.platformTimeout(200));
1717

18-
const onMessage = common.mustCall(function(err, bytes) {
19-
assert.equal(bytes, 0);
18+
client.on('message', common.mustCall(function onMessage(buf, info) {
19+
const expected = Buffer.alloc(0);
20+
assert.ok(buf.equals(expected), 'message was received correctly');
2021
clearTimeout(timer);
2122
client.close();
22-
});
23+
}));
2324

2425
client.on('listening', function() {
25-
const toSend = [];
26-
client.send(toSend, common.PORT, common.localhostIPv4, onMessage);
27-
});
28-
29-
client.on('message', function(buf, info) {
30-
const expected = Buffer.alloc(0);
31-
assert.ok(buf.equals(expected), 'message was received correctly');
32-
client.close();
26+
client.send([], common.PORT, common.localhostIPv4);
3327
});
3428

3529
client.bind(common.PORT);

test/parallel/test-dgram-send-empty-buffer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ const client = dgram.createSocket('udp4');
1111

1212
client.bind(common.PORT);
1313

14-
client.on('message', function(buffer, bytes) {
14+
client.on('message', common.mustCall(function onMessage(buffer, bytes) {
1515
clearTimeout(timer);
1616
client.close();
17-
});
17+
}));
1818

1919
const buf = Buffer.alloc(0);
2020
client.send(buf, 0, 0, common.PORT, '127.0.0.1', function(err, len) { });

test/parallel/test-dgram-send-multi-buffer-copy.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const timer = setTimeout(function() {
1313
const onMessage = common.mustCall(function(err, bytes) {
1414
assert.equal(bytes, buf1.length + buf2.length);
1515
clearTimeout(timer);
16-
client.close();
1716
});
1817

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

28-
client.on('message', function(buf, info) {
27+
client.on('message', common.mustCall(function onMessage(buf, info) {
2928
const expected = Buffer.concat([buf1, buf2]);
3029
assert.ok(buf.equals(expected), 'message was received correctly');
3130
client.close();
32-
});
31+
}));
3332

3433
client.bind(common.PORT);

0 commit comments

Comments
 (0)