Skip to content

Commit

Permalink
test: fix flaky test-dgram-exclusive-implicit-bind
Browse files Browse the repository at this point in the history
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 <santiago.gimeno@gmail.com>
Reviewed-By: Italo A. Casas <me@italoacasas.com>
  • Loading branch information
Trott authored and evanlucas committed Dec 14, 2016
1 parent a4f3080 commit 90659bc
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions test/parallel/test-dgram-exclusive-implicit-bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

var common = require('../common');
var assert = require('assert');
var cluster = require('cluster');
var dgram = require('dgram');
const common = require('../common');
const assert = require('assert');
const cluster = require('cluster');
const dgram = require('dgram');

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

if (cluster.isMaster) {
var pass;
var messages = 0;
var ports = {};

process.on('exit', function() {
assert.strictEqual(pass, true);
});
const ports = {};
const pids = [];

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

const done = common.mustCall(function() {
cluster.disconnect();
target.close();
});

target.on('message', function(buf, rinfo) {
if (pids.includes(buf.toString()))
return;
pids.push(buf.toString());
messages++;
ports[rinfo.port] = true;

Expand All @@ -63,12 +67,6 @@ if (cluster.isMaster) {
assert.strictEqual(Object.keys(ports).length, 3);
done();
}

function done() {
pass = true;
cluster.disconnect();
target.close();
}
});

target.on('listening', function() {
Expand All @@ -85,7 +83,12 @@ if (cluster.isMaster) {
return;
}

var source = dgram.createSocket('udp4');
const source = dgram.createSocket('udp4');
var interval;

source.on('close', function() {
clearInterval(interval);
});

if (process.env.BOUND === 'y') {
source.bind(0);
Expand All @@ -96,4 +99,7 @@ if (process.env.BOUND === 'y') {
source.unref();
}

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

0 comments on commit 90659bc

Please sign in to comment.