Skip to content

test: improve test-cluster-net-send.js #9570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions test/parallel/test-cluster-net-send.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,22 @@ if (process.argv[2] !== 'child') {
console.error('[%d] master', process.pid);

const worker = fork(__filename, ['child']);
let called = false;
let result = '';

worker.once('message', common.mustCall(function(msg, handle) {
worker.once('message', common.mustCall((msg, handle) => {
assert.strictEqual(msg, 'handle');
assert.ok(handle);
worker.send('got');

handle.on('data', function(data) {
called = true;
assert.strictEqual(data.toString(), 'hello');
handle.on('data', (data) => {
result += data.toString();
});

handle.on('end', function() {
handle.on('end', () => {
assert.strictEqual(result, 'hello');
worker.kill();
});
}));

process.once('exit', function() {
assert.ok(called);
});
} else {
console.error('[%d] worker', process.pid);

Expand All @@ -38,20 +34,20 @@ if (process.argv[2] !== 'child') {
process.send('handle', socket);
}

const server = net.createServer(function(c) {
process.once('message', common.mustCall(function(msg) {
const server = net.createServer((c) => {
process.once('message', common.mustCall((msg) => {
assert.strictEqual(msg, 'got');
c.end('hello');
}));
socketConnected();
});

server.listen(common.PORT, function() {
socket = net.connect(common.PORT, '127.0.0.1', socketConnected);
server.listen(common.PORT, () => {
socket = net.connect(server.address().port, server.address().address,
Copy link
Member

@gibfahn gibfahn Feb 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the same issue as the one I saw in #10854

If so, the problem is that you can't use server.address().address here. You should be listening on 0 (i.e. all IPv6 and IPv4 addresses), in which case server.address().address resolves to ::. On linux this sometimes resolves to 127.0.0.1 if there is no IPv6 support on the machine, but this isn't the case on all platforms.

Changing to:

-  server.listen(common.PORT, () => {
-    socket = net.connect(server.address().port, server.address().address,
+  server.listen(0, () => {
+    socket = net.connect(server.address().port, 'localhost',

should fix the issue on all platforms. localhost should always resolve to either 127.0.0.1 or :: (either of which is fine).

cc/ @cjihrig

socketConnected);
});

process.on('disconnect', function() {
process.exit();
process.on('disconnect', () => {
server.close();
});
}