Skip to content

Commit 8872840

Browse files
santigimenomcollina
authored andcommitted
test: refactor test-dgram-send-callback-recursive
Just send 10 messages recursively and check that the send calls are asynchronous by asserting that a `setImmediate` callback has been called in-between. It avoids a race condition in the test when the recursive limit is reached without having received at least 10 messages. Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> PR-URL: #5079
1 parent dff01d1 commit 8872840

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

test/parallel/test-dgram-send-callback-recursive.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,25 @@ const assert = require('assert');
55
const dgram = require('dgram');
66
const client = dgram.createSocket('udp4');
77
const chunk = 'abc';
8-
var recursiveCount = 0;
9-
var received = 0;
8+
let received = 0;
9+
let sent = 0;
1010
const limit = 10;
11-
const recursiveLimit = 100;
11+
let async = false;
1212

1313
function onsend() {
14-
if (recursiveCount > recursiveLimit) {
15-
throw new Error('infinite loop detected');
16-
}
17-
if (received < limit) {
14+
if (sent++ < limit) {
1815
client.send(
1916
chunk, 0, chunk.length, common.PORT, common.localhostIPv4, onsend);
17+
} else {
18+
assert.strictEqual(async, true, 'Send should be asynchronous.');
2019
}
21-
recursiveCount++;
2220
}
2321

2422
client.on('listening', function() {
23+
setImmediate(function() {
24+
async = true;
25+
});
26+
2527
onsend();
2628
});
2729

0 commit comments

Comments
 (0)