test: use dynamic port instead of common.PORT

Remove common.PORT from, test-net-connect-immediate-destroy,
test-net-options-lookup, test-net-connect-local-error,
test-net-connect-handle-econnrefused, test-net-socket-destroy-twice,
test-net-better-error-messages-port-hostname, test-net-localerror,
to reduce possibility that a dynamic port used in another test will
collide with common.PORT.

Moved test-net-listen-shared-ports, test-net-better-error-messages-port
from tests/parallel to test/sequential

Refs: https://github.com/nodejs/node/issues/12376
PR-URL: https://github.com/nodejs/node/pull/12473
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
This commit is contained in:
Aditya Anand 2017-04-17 17:20:17 +05:30 committed by James M Snell
parent 824fb49a70
commit 94eed0fb11
10 changed files with 39 additions and 24 deletions

View File

@ -3,12 +3,13 @@ const common = require('../common');
const net = require('net'); const net = require('net');
const assert = require('assert'); const assert = require('assert');
const c = net.createConnection(common.PORT, '***'); // Using port 0 as hostname used is already invalid.
const c = net.createConnection(0, '***');
c.on('connect', common.mustNotCall()); c.on('connect', common.mustNotCall());
c.on('error', common.mustCall(function(e) { c.on('error', common.mustCall(function(e) {
assert.strictEqual(e.code, 'ENOTFOUND'); assert.strictEqual(e.code, 'ENOTFOUND');
assert.strictEqual(e.port, common.PORT); assert.strictEqual(e.port, 0);
assert.strictEqual(e.hostname, '***'); assert.strictEqual(e.hostname, '***');
})); }));

View File

@ -24,13 +24,14 @@ const common = require('../common');
const net = require('net'); const net = require('net');
const assert = require('assert'); const assert = require('assert');
const server = net.createServer();
// Hopefully nothing is running on common.PORT server.listen(0);
const c = net.createConnection(common.PORT); const port = server.address().port;
const c = net.createConnection(port);
c.on('connect', common.mustNotCall()); c.on('connect', common.mustNotCall());
c.on('error', common.mustCall(function(e) { c.on('error', common.mustCall((e) => {
console.error('couldn\'t connect.');
assert.strictEqual('ECONNREFUSED', e.code); assert.strictEqual('ECONNREFUSED', e.code);
})); }));
server.close();

View File

@ -2,7 +2,10 @@
const common = require('../common'); const common = require('../common');
const net = require('net'); const net = require('net');
const socket = net.connect(common.PORT, common.localhostIPv4, const server = net.createServer();
common.mustNotCall()); server.listen(0);
const port = server.address().port;
const socket = net.connect(port, common.localhostIPv4, common.mustNotCall());
socket.on('error', common.mustNotCall()); socket.on('error', common.mustNotCall());
server.close();
socket.destroy(); socket.destroy();

View File

@ -26,7 +26,7 @@ const net = require('net');
const client = net.connect({host: '***', port: common.PORT}); const client = net.connect({host: '***', port: common.PORT});
client.once('error', common.mustCall(function(err) { client.once('error', common.mustCall((err) => {
assert(err); assert(err);
assert.strictEqual(err.code, err.errno); assert.strictEqual(err.code, err.errno);
assert.strictEqual(err.code, 'ENOTFOUND'); assert.strictEqual(err.code, 'ENOTFOUND');

View File

@ -3,17 +3,20 @@ const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const net = require('net'); const net = require('net');
const server = net.createServer();
server.listen(0);
const port = server.address().port;
const client = net.connect({ const client = net.connect({
port: common.PORT + 1, port: port + 1,
localPort: common.PORT, localPort: port,
localAddress: common.localhostIPv4 localAddress: common.localhostIPv4
}); });
client.on('error', common.mustCall(function onError(err) { client.on('error', common.mustCall(function onError(err) {
assert.strictEqual( assert.strictEqual(
err.localPort, err.localPort,
common.PORT, port,
`${err.localPort} !== ${common.PORT} in ${err}` `${err.localPort} !== ${port} in ${err}`
); );
assert.strictEqual( assert.strictEqual(
err.localAddress, err.localAddress,
@ -21,3 +24,4 @@ client.on('error', common.mustCall(function onError(err) {
`${err.localAddress} !== ${common.localhostIPv4} in ${err}` `${err.localAddress} !== ${common.localhostIPv4} in ${err}`
); );
})); }));
server.close();

View File

@ -20,24 +20,25 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE. // USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict'; 'use strict';
const common = require('../common'); require('../common');
const assert = require('assert'); const assert = require('assert');
const net = require('net'); const net = require('net');
// Using port 0 as localPort / localAddress is already invalid.
connect({ connect({
host: 'localhost', host: 'localhost',
port: common.PORT, port: 0,
localPort: 'foobar', localPort: 'foobar',
}, /^TypeError: "localPort" option should be a number: foobar$/); }, /^TypeError: "localPort" option should be a number: foobar$/);
connect({ connect({
host: 'localhost', host: 'localhost',
port: common.PORT, port: 0,
localAddress: 'foobar', localAddress: 'foobar',
}, /^TypeError: "localAddress" option must be a valid IP: foobar$/); }, /^TypeError: "localAddress" option must be a valid IP: foobar$/);
function connect(opts, msg) { function connect(opts, msg) {
assert.throws(function() { assert.throws(() => {
net.connect(opts); net.connect(opts);
}, msg); }, msg);
} }

View File

@ -7,14 +7,15 @@ const expectedError = /^TypeError: "lookup" option should be a function$/;
['foobar', 1, {}, []].forEach((input) => connectThrows(input)); ['foobar', 1, {}, []].forEach((input) => connectThrows(input));
// Using port 0 as lookup is emitted before connecting.
function connectThrows(input) { function connectThrows(input) {
const opts = { const opts = {
host: 'localhost', host: 'localhost',
port: common.PORT, port: 0,
lookup: input lookup: input
}; };
assert.throws(function() { assert.throws(() => {
net.connect(opts); net.connect(opts);
}, expectedError); }, expectedError);
} }
@ -24,11 +25,11 @@ connectDoesNotThrow(common.noop);
function connectDoesNotThrow(input) { function connectDoesNotThrow(input) {
const opts = { const opts = {
host: 'localhost', host: 'localhost',
port: common.PORT, port: 0,
lookup: input lookup: input
}; };
assert.doesNotThrow(function() { assert.doesNotThrow(() => {
net.connect(opts); net.connect(opts);
}); });
} }

View File

@ -23,10 +23,14 @@
const common = require('../common'); const common = require('../common');
const net = require('net'); const net = require('net');
const conn = net.createConnection(common.PORT); const server = net.createServer();
server.listen(0);
const port = server.address().port;
const conn = net.createConnection(port);
conn.on('error', common.mustCall(function() { conn.on('error', common.mustCall(() => {
conn.destroy(); conn.destroy();
})); }));
conn.on('close', common.mustCall()); conn.on('close', common.mustCall());
server.close();