Skip to content

Commit 94eed0f

Browse files
thelostone-mcjasnell
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 <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Yuta Hiroto <[email protected]> Reviewed-By: Santiago Gimeno <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]>
1 parent 824fb49 commit 94eed0f

10 files changed

+39
-24
lines changed

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

+3-2
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

+6-5
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@ const common = require('../common');
2424
const net = require('net');
2525
const assert = require('assert');
2626

27-
28-
// Hopefully nothing is running on common.PORT
29-
const c = net.createConnection(common.PORT);
27+
const server = net.createServer();
28+
server.listen(0);
29+
const port = server.address().port;
30+
const c = net.createConnection(port);
3031

3132
c.on('connect', common.mustNotCall());
3233

33-
c.on('error', common.mustCall(function(e) {
34-
console.error('couldn\'t connect.');
34+
c.on('error', common.mustCall((e) => {
3535
assert.strictEqual('ECONNREFUSED', e.code);
3636
}));
37+
server.close();

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

+5-2
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const net = require('net');
2626

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

29-
client.once('error', common.mustCall(function(err) {
29+
client.once('error', common.mustCall((err) => {
3030
assert(err);
3131
assert.strictEqual(err.code, err.errno);
3232
assert.strictEqual(err.code, 'ENOTFOUND');

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

+8-4
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

+5-4
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,25 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
const common = require('../common');
23+
require('../common');
2424
const assert = require('assert');
2525
const net = require('net');
2626

27+
// Using port 0 as localPort / localAddress is already invalid.
2728
connect({
2829
host: 'localhost',
29-
port: common.PORT,
30+
port: 0,
3031
localPort: 'foobar',
3132
}, /^TypeError: "localPort" option should be a number: foobar$/);
3233

3334
connect({
3435
host: 'localhost',
35-
port: common.PORT,
36+
port: 0,
3637
localAddress: 'foobar',
3738
}, /^TypeError: "localAddress" option must be a valid IP: foobar$/);
3839

3940
function connect(opts, msg) {
40-
assert.throws(function() {
41+
assert.throws(() => {
4142
net.connect(opts);
4243
}, msg);
4344
}

test/parallel/test-net-options-lookup.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ 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 @@ connectDoesNotThrow(common.noop);
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

+6-2
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@
2323
const common = require('../common');
2424
const net = require('net');
2525

26-
const conn = net.createConnection(common.PORT);
26+
const server = net.createServer();
27+
server.listen(0);
28+
const port = server.address().port;
29+
const conn = net.createConnection(port);
2730

28-
conn.on('error', common.mustCall(function() {
31+
conn.on('error', common.mustCall(() => {
2932
conn.destroy();
3033
}));
3134

3235
conn.on('close', common.mustCall());
36+
server.close();

0 commit comments

Comments
 (0)