Skip to content

Commit 29d35d0

Browse files
thelostone-mcMylesBorins
authored andcommitted
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: #12376 PR-URL: #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>
1 parent 186c075 commit 29d35d0

10 files changed

+40
-25
lines changed

test/parallel/test-net-better-error-messages-port-hostname.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ const common = require('../common');
33
const net = require('net');
44
const assert = require('assert');
55

6-
const c = net.createConnection(common.PORT, '***');
6+
// Using port 0 as hostname used is already invalid.
7+
const c = net.createConnection(0, '***');
78

89
c.on('connect', common.mustNotCall());
910

1011
c.on('error', common.mustCall(function(e) {
1112
assert.strictEqual(e.code, 'ENOTFOUND');
12-
assert.strictEqual(e.port, common.PORT);
13+
assert.strictEqual(e.port, 0);
1314
assert.strictEqual(e.hostname, '***');
1415
}));

test/parallel/test-net-connect-handle-econnrefused.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ const common = require('../common');
33
const net = require('net');
44
const assert = require('assert');
55

6-
7-
// Hopefully nothing is running on common.PORT
8-
const c = net.createConnection(common.PORT);
6+
const server = net.createServer();
7+
server.listen(0);
8+
const port = server.address().port;
9+
const c = net.createConnection(port);
910

1011
c.on('connect', common.mustNotCall());
1112

12-
c.on('error', common.mustCall(function(e) {
13-
console.error('couldn\'t connect.');
13+
c.on('error', common.mustCall((e) => {
1414
assert.strictEqual('ECONNREFUSED', e.code);
1515
}));
16+
server.close();

test/parallel/test-net-connect-immediate-destroy.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
const common = require('../common');
33
const net = require('net');
44

5-
const socket = net.connect(common.PORT, common.localhostIPv4,
6-
common.mustNotCall());
5+
const server = net.createServer();
6+
server.listen(0);
7+
const port = server.address().port;
8+
const socket = net.connect(port, common.localhostIPv4, common.mustNotCall());
79
socket.on('error', common.mustNotCall());
10+
server.close();
811
socket.destroy();

test/parallel/test-net-connect-immediate-finish.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const net = require('net');
55

66
const client = net.connect({host: '***', port: common.PORT});
77

8-
client.once('error', common.mustCall(function(err) {
8+
client.once('error', common.mustCall((err) => {
99
assert(err);
1010
assert.strictEqual(err.code, err.errno);
1111
assert.strictEqual(err.code, 'ENOTFOUND');

test/parallel/test-net-connect-local-error.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,25 @@ const common = require('../common');
33
const assert = require('assert');
44
const net = require('net');
55

6+
const server = net.createServer();
7+
server.listen(0);
8+
const port = server.address().port;
69
const client = net.connect({
7-
port: common.PORT + 1,
8-
localPort: common.PORT,
10+
port: port + 1,
11+
localPort: port,
912
localAddress: common.localhostIPv4
1013
});
1114

1215
client.on('error', common.mustCall(function onError(err) {
1316
assert.strictEqual(
1417
err.localPort,
15-
common.PORT,
16-
`${err.localPort} !== ${common.PORT} in ${err}`
18+
port,
19+
`${err.localPort} !== ${port} in ${err}`
1720
);
1821
assert.strictEqual(
1922
err.localAddress,
2023
common.localhostIPv4,
2124
`${err.localAddress} !== ${common.localhostIPv4} in ${err}`
2225
);
2326
}));
27+
server.close();

test/parallel/test-net-localerror.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
'use strict';
2-
const common = require('../common');
2+
require('../common');
33
const assert = require('assert');
44
const net = require('net');
55

6+
// Using port 0 as localPort / localAddress is already invalid.
67
connect({
78
host: 'localhost',
8-
port: common.PORT,
9+
port: 0,
910
localPort: 'foobar',
1011
}, /^TypeError: "localPort" option should be a number: foobar$/);
1112

1213
connect({
1314
host: 'localhost',
14-
port: common.PORT,
15+
port: 0,
1516
localAddress: 'foobar',
1617
}, /^TypeError: "localAddress" option must be a valid IP: foobar$/);
1718

1819
function connect(opts, msg) {
19-
assert.throws(function() {
20+
assert.throws(() => {
2021
net.connect(opts);
2122
}, msg);
2223
}
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
'use strict';
2-
const common = require('../common');
2+
require('../common');
33
const assert = require('assert');
44
const net = require('net');
55

66
const expectedError = /^TypeError: "lookup" option should be a function$/;
77

88
['foobar', 1, {}, []].forEach((input) => connectThrows(input));
99

10+
// Using port 0 as lookup is emitted before connecting.
1011
function connectThrows(input) {
1112
const opts = {
1213
host: 'localhost',
13-
port: common.PORT,
14+
port: 0,
1415
lookup: input
1516
};
1617

17-
assert.throws(function() {
18+
assert.throws(() => {
1819
net.connect(opts);
1920
}, expectedError);
2021
}
@@ -24,11 +25,11 @@ function connectThrows(input) {
2425
function connectDoesNotThrow(input) {
2526
const opts = {
2627
host: 'localhost',
27-
port: common.PORT,
28+
port: 0,
2829
lookup: input
2930
};
3031

31-
assert.doesNotThrow(function() {
32+
assert.doesNotThrow(() => {
3233
net.connect(opts);
3334
});
3435
}

test/parallel/test-net-socket-destroy-twice.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
const common = require('../common');
33
const net = require('net');
44

5-
const conn = net.createConnection(common.PORT);
5+
const server = net.createServer();
6+
server.listen(0);
7+
const port = server.address().port;
8+
const conn = net.createConnection(port);
69

7-
conn.on('error', common.mustCall(function() {
10+
conn.on('error', common.mustCall(() => {
811
conn.destroy();
912
}));
1013

1114
conn.on('close', common.mustCall(function() {}));
15+
server.close();

0 commit comments

Comments
 (0)