Skip to content

Commit

Permalink
test: dynamic port in dgram tests
Browse files Browse the repository at this point in the history
Removed common.PORT from test-dgram-close-in-listening,
test-dgram-close-is-not-callback, test-dgram-close,
test-dgram-exclusive-implicit-bind and test-dgram-oob-buffer
in order to eliminate the possibility of port collision.

Refs: #12376
PR-URL: #12623
Backport-PR-URL: #13792
Reviewed-By: Anna Henningsen <[email protected]>
  • Loading branch information
Sebastian Plesciuc authored and MylesBorins committed Jul 11, 2017
1 parent 361bc84 commit 14e8358
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 32 deletions.
14 changes: 11 additions & 3 deletions test/parallel/test-dgram-close-in-listening.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ socket.on('listening', function() {
socket.close();
});

// adds a listener to 'listening' to send the data when
// the socket is available
socket.send(buf, 0, buf.length, common.PORT, 'localhost');
// get a random port for send
const portGetter = dgram.createSocket('udp4')
.bind(0, 'localhost', common.mustCall(() => {
// adds a listener to 'listening' to send the data when
// the socket is available
socket.send(buf, 0, buf.length,
portGetter.address().port,
portGetter.address().address);

portGetter.close();
}));
15 changes: 11 additions & 4 deletions test/parallel/test-dgram-close-is-not-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@ const buf = Buffer.alloc(1024, 42);

const socket = dgram.createSocket('udp4');

socket.send(buf, 0, buf.length, common.PORT, 'localhost');
// get a random port for send
const portGetter = dgram.createSocket('udp4')
.bind(0, 'localhost', common.mustCall(() => {
socket.send(buf, 0, buf.length,
portGetter.address().port,
portGetter.address().address);

// if close callback is not function, ignore the argument.
socket.close('bad argument');
// if close callback is not function, ignore the argument.
socket.close('bad argument');
portGetter.close();

socket.on('close', common.mustCall(function() {}));
socket.on('close', common.mustCall(function() {}));
}));
29 changes: 19 additions & 10 deletions test/parallel/test-dgram-close.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,23 @@ const buf = Buffer.alloc(1024, 42);
let socket = dgram.createSocket('udp4');
const handle = socket._handle;

socket.send(buf, 0, buf.length, common.PORT, 'localhost');
assert.strictEqual(socket.close(common.mustCall(function() {})), socket);
socket.on('close', common.mustCall(function() {}));
socket = null;
// get a random port for send
const portGetter = dgram.createSocket('udp4')
.bind(0, 'localhost', common.mustCall(() => {
socket.send(buf, 0, buf.length,
portGetter.address().port,
portGetter.address().address);

// Verify that accessing handle after closure doesn't throw
setImmediate(function() {
setImmediate(function() {
console.log('Handle fd is: ', handle.fd);
});
});
assert.strictEqual(socket.close(common.mustCall(function() {})), socket);
socket.on('close', common.mustCall(function() {}));
socket = null;

// Verify that accessing handle after closure doesn't throw
setImmediate(function() {
setImmediate(function() {
console.log('Handle fd is: ', handle.fd);
});
});

portGetter.close();
}));
13 changes: 7 additions & 6 deletions test/parallel/test-dgram-exclusive-implicit-bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ if (cluster.isMaster) {
});

target.on('listening', function() {
cluster.fork();
cluster.fork();
cluster.fork({PORT: target.address().port});
cluster.fork({PORT: target.address().port});
if (!common.isWindows) {
cluster.fork({BOUND: 'y'});
cluster.fork({BOUND: 'y'});
cluster.fork({BOUND: 'y', PORT: target.address().port});
cluster.fork({BOUND: 'y', PORT: target.address().port});
}
});

target.bind({port: common.PORT, exclusive: true});
target.bind({port: 0, exclusive: true});

return;
}
Expand All @@ -99,7 +99,8 @@ if (process.env.BOUND === 'y') {
source.unref();
}

assert(process.env.PORT);
const buf = Buffer.from(process.pid.toString());
interval = setInterval(() => {
source.send(buf, common.PORT, '127.0.0.1');
source.send(buf, process.env.PORT, '127.0.0.1');
}, 1).unref();
23 changes: 14 additions & 9 deletions test/parallel/test-dgram-oob-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ const dgram = require('dgram');

const socket = dgram.createSocket('udp4');
const buf = Buffer.from([1, 2, 3, 4]);
const portGetter = dgram.createSocket('udp4')
.bind(0, 'localhost', common.mustCall(() => {
const address = portGetter.address();
portGetter.close(common.mustCall(() => {
function ok() {}
socket.send(buf, 0, 0, address.port, address.address, ok);
socket.send(buf, 0, 4, address.port, address.address, ok);
socket.send(buf, 1, 3, address.port, address.address, ok);
socket.send(buf, 3, 1, address.port, address.address, ok);
// Since length of zero means nothing, don't error despite OOB.
socket.send(buf, 4, 0, address.port, address.address, ok);

function ok() {}
socket.send(buf, 0, 0, common.PORT, '127.0.0.1', ok); // useful? no
socket.send(buf, 0, 4, common.PORT, '127.0.0.1', ok);
socket.send(buf, 1, 3, common.PORT, '127.0.0.1', ok);
socket.send(buf, 3, 1, common.PORT, '127.0.0.1', ok);
// Since length of zero means nothing, don't error despite OOB.
socket.send(buf, 4, 0, common.PORT, '127.0.0.1', ok);

socket.close();
socket.close();
}));
}));

0 comments on commit 14e8358

Please sign in to comment.