node/test/parallel/test-cluster-eaddrinuse.js
cjihrig f0eba7811d
test: add common.mustNotCall()
This commit adds a mustNotCall() helper for testing. This provides
an alternative to using common.fail() as a callback, or creating
a callback function for the sole purpose of calling common.fail().

PR-URL: https://github.com/nodejs/node/pull/11152
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
2017-02-14 11:52:23 -05:00

40 lines
1.3 KiB
JavaScript

'use strict';
// Check that having a worker bind to a port that's already taken doesn't
// leave the master process in a confused state. Releasing the port and
// trying again should Just Work[TM].
const common = require('../common');
const assert = require('assert');
const fork = require('child_process').fork;
const net = require('net');
const id = '' + process.argv[2];
if (id === 'undefined') {
const server = net.createServer(common.mustNotCall());
server.listen(common.PORT, function() {
const worker = fork(__filename, ['worker']);
worker.on('message', function(msg) {
if (msg !== 'stop-listening') return;
server.close(function() {
worker.send('stopped-listening');
});
});
});
} else if (id === 'worker') {
let server = net.createServer(common.mustNotCall());
server.listen(common.PORT, common.mustNotCall());
server.on('error', common.mustCall(function(e) {
assert(e.code, 'EADDRINUSE');
process.send('stop-listening');
process.once('message', function(msg) {
if (msg !== 'stopped-listening') return;
server = net.createServer(common.mustNotCall());
server.listen(common.PORT, common.mustCall(function() {
server.close();
}));
});
}));
} else {
assert(0); // Bad argument.
}