Skip to content

Commit 90659bc

Browse files
Trottevanlucas
authored andcommitted
test: fix flaky test-dgram-exclusive-implicit-bind
test-dgram-exclusive-implicit-bind is written assuming that dgram messages are received with 100% reliability. While missing a dgram message sent to localhost is rare, we do see it as evidenced by CI failures from time to time. The test has been rewritten to send dgram messages over and over until the test requirements have been met. Additional incidental refactoring includes: * var -> const * use of common.mustCall() instead of exit listener + boolean PR-URL: #10212 Reviewed-By: Santiago Gimeno <[email protected]> Reviewed-By: Italo A. Casas <[email protected]>
1 parent a4f3080 commit 90659bc

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

test/parallel/test-dgram-exclusive-implicit-bind.js

+24-18
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2121
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2222

23-
var common = require('../common');
24-
var assert = require('assert');
25-
var cluster = require('cluster');
26-
var dgram = require('dgram');
23+
const common = require('../common');
24+
const assert = require('assert');
25+
const cluster = require('cluster');
26+
const dgram = require('dgram');
2727

2828
// Without an explicit bind, send() causes an implicit bind, which always
2929
// generate a unique per-socket ephemeral port. An explicit bind to a port
@@ -40,17 +40,21 @@ var dgram = require('dgram');
4040
// with ENOTSUP.
4141

4242
if (cluster.isMaster) {
43-
var pass;
4443
var messages = 0;
45-
var ports = {};
46-
47-
process.on('exit', function() {
48-
assert.strictEqual(pass, true);
49-
});
44+
const ports = {};
45+
const pids = [];
5046

5147
var target = dgram.createSocket('udp4');
5248

49+
const done = common.mustCall(function() {
50+
cluster.disconnect();
51+
target.close();
52+
});
53+
5354
target.on('message', function(buf, rinfo) {
55+
if (pids.includes(buf.toString()))
56+
return;
57+
pids.push(buf.toString());
5458
messages++;
5559
ports[rinfo.port] = true;
5660

@@ -63,12 +67,6 @@ if (cluster.isMaster) {
6367
assert.strictEqual(Object.keys(ports).length, 3);
6468
done();
6569
}
66-
67-
function done() {
68-
pass = true;
69-
cluster.disconnect();
70-
target.close();
71-
}
7270
});
7371

7472
target.on('listening', function() {
@@ -85,7 +83,12 @@ if (cluster.isMaster) {
8583
return;
8684
}
8785

88-
var source = dgram.createSocket('udp4');
86+
const source = dgram.createSocket('udp4');
87+
var interval;
88+
89+
source.on('close', function() {
90+
clearInterval(interval);
91+
});
8992

9093
if (process.env.BOUND === 'y') {
9194
source.bind(0);
@@ -96,4 +99,7 @@ if (process.env.BOUND === 'y') {
9699
source.unref();
97100
}
98101

99-
source.send(Buffer.from('abc'), 0, 3, common.PORT, '127.0.0.1');
102+
const buf = Buffer.from(process.pid.toString());
103+
interval = setInterval(() => {
104+
source.send(buf, common.PORT, '127.0.0.1');
105+
}, 1).unref();

0 commit comments

Comments
 (0)