Skip to content

Commit 9a8acad

Browse files
mscdexMyles Borins
authored and
Myles Borins
committed
test: use random ports where possible
This helps to prevent issues where a failed test can keep a bound socket open long enough to cause other tests to fail with EADDRINUSE because the same port number is used. PR-URL: #7045 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Rod Vagg <[email protected]>
1 parent af4940d commit 9a8acad

File tree

336 files changed

+1514
-1478
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

336 files changed

+1514
-1478
lines changed

test/parallel/test-async-wrap-check-providers.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ net.createServer(function(c) {
7676
net.createServer(function(c) {
7777
c.end();
7878
this.close(checkTLS);
79-
}).listen(common.PORT, function() {
80-
net.connect(common.PORT, noop);
79+
}).listen(0, function() {
80+
net.connect(this.address().port, noop);
8181
});
8282

83-
dgram.createSocket('udp4').bind(common.PORT, function() {
84-
this.send(new Buffer(2), 0, 2, common.PORT, '::', () => {
83+
dgram.createSocket('udp4').bind(0, function() {
84+
this.send(new Buffer(2), 0, 2, this.address().port, '::', () => {
8585
this.close();
8686
});
8787
});
@@ -95,8 +95,9 @@ function checkTLS() {
9595
cert: fs.readFileSync(common.fixturesDir + '/keys/ec-cert.pem')
9696
};
9797
const server = tls.createServer(options, noop)
98-
.listen(common.PORT, function() {
99-
tls.connect(common.PORT, { rejectUnauthorized: false }, function() {
98+
.listen(0, function() {
99+
const connectOpts = { rejectUnauthorized: false };
100+
tls.connect(this.address().port, connectOpts, function() {
100101
this.destroy();
101102
server.close();
102103
});

test/parallel/test-async-wrap-disabled-propagate-parent.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const common = require('../common');
3+
require('../common');
44
const assert = require('assert');
55
const net = require('net');
66
const async_wrap = process.binding('async_wrap');
@@ -40,8 +40,8 @@ const server = net.createServer(function(c) {
4040
c.end();
4141
this.close();
4242
});
43-
}).listen(common.PORT, function() {
44-
net.connect(common.PORT, noop);
43+
}).listen(0, function() {
44+
net.connect(this.address().port, noop);
4545
});
4646

4747
async_wrap.disable();

test/parallel/test-async-wrap-propagate-parent.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const common = require('../common');
3+
require('../common');
44
const assert = require('assert');
55
const net = require('net');
66
const async_wrap = process.binding('async_wrap');
@@ -40,8 +40,8 @@ const server = net.createServer(function(c) {
4040
c.end();
4141
this.close();
4242
});
43-
}).listen(common.PORT, function() {
44-
net.connect(common.PORT, noop);
43+
}).listen(0, function() {
44+
net.connect(this.address().port, noop);
4545
});
4646

4747

test/parallel/test-beforeexit-event.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
2+
require('../common');
23
var assert = require('assert');
34
var net = require('net');
4-
var common = require('../common');
55
var revivals = 0;
66
var deaths = 0;
77

@@ -29,7 +29,7 @@ function tryTimer() {
2929
function tryListen() {
3030
console.log('create a server');
3131
net.createServer()
32-
.listen(common.PORT)
32+
.listen(0)
3333
.on('listening', function() {
3434
revivals++;
3535
this.close();

test/parallel/test-child-process-disconnect.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ if (process.argv[2] === 'child') {
3838

3939
// when the server is ready tell parent
4040
server.on('listening', function() {
41-
process.send('ready');
41+
process.send({ msg: 'ready', port: server.address().port });
4242
});
4343

44-
server.listen(common.PORT);
44+
server.listen(0);
4545

4646
} else {
4747
// testcase
@@ -65,11 +65,11 @@ if (process.argv[2] === 'child') {
6565
});
6666

6767
// when child is listening
68-
child.on('message', function(msg) {
69-
if (msg === 'ready') {
68+
child.on('message', function(obj) {
69+
if (obj && obj.msg === 'ready') {
7070

7171
// connect to child using TCP to know if disconnect was emitted
72-
var socket = net.connect(common.PORT);
72+
var socket = net.connect(obj.port);
7373

7474
socket.on('data', function(data) {
7575
data = data.toString();

test/parallel/test-child-process-fork-dgram.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ if (process.argv[2] === 'child') {
7272
msg,
7373
0,
7474
msg.length,
75-
common.PORT,
75+
server.address().port,
7676
'127.0.0.1',
7777
function(err) {
7878
if (err) throw err;
@@ -98,7 +98,7 @@ if (process.argv[2] === 'child') {
9898
client.close();
9999
};
100100

101-
server.bind(common.PORT, '127.0.0.1');
101+
server.bind(0, '127.0.0.1');
102102

103103
process.once('exit', function() {
104104
assert(parentGotMessage);

test/parallel/test-child-process-fork-net.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
2-
const assert = require('assert');
32
require('../common');
3+
const assert = require('assert');
44
const fork = require('child_process').fork;
55
const net = require('net');
66

test/parallel/test-child-process-fork-net2.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ if (process.argv[2] === 'child') {
9999

100100
var j = count, client;
101101
while (j--) {
102-
client = net.connect(common.PORT, '127.0.0.1');
102+
client = net.connect(this.address().port, '127.0.0.1');
103103
client.on('error', function() {
104104
// This can happen if we kill the child too early.
105105
// The client should still get a close event afterwards.
@@ -125,7 +125,7 @@ if (process.argv[2] === 'child') {
125125
child3.kill();
126126
}));
127127

128-
server.listen(common.PORT, '127.0.0.1');
128+
server.listen(0, '127.0.0.1');
129129

130130
var closeServer = function() {
131131
server.close();

test/parallel/test-child-process-fork-regr-gh-2847.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ var server = net.createServer(function(s) {
2525
setTimeout(function() {
2626
s.destroy();
2727
}, 100);
28-
}).listen(common.PORT, function() {
28+
}).listen(0, function() {
2929
var worker = cluster.fork();
3030

3131
function send(callback) {
32-
var s = net.connect(common.PORT, function() {
32+
var s = net.connect(server.address().port, function() {
3333
worker.send({}, s, callback);
3434
});
3535

test/parallel/test-child-process-recv-handle.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function master() {
2424
});
2525
proc.stdout.on('data', function(data) {
2626
assert.equal(data, 'ok\r\n');
27-
net.createServer(common.fail).listen(common.PORT, function() {
27+
net.createServer(common.fail).listen(0, function() {
2828
handle = this._handle;
2929
proc.send('one');
3030
proc.send('two', handle);

test/parallel/test-crypto-verify-failure.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ function verify() {
3636
.verify(certPem, 'asdfasdfas', 'base64');
3737
}
3838

39-
server.listen(common.PORT, function() {
39+
server.listen(0, function() {
4040
tls.connect({
41-
port: common.PORT,
41+
port: this.address().port,
4242
rejectUnauthorized: false
4343
}, function() {
4444
verify();

test/parallel/test-dgram-address.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ var family_ipv4 = 'IPv4';
1010
socket_ipv4.on('listening', function() {
1111
var address_ipv4 = socket_ipv4.address();
1212
assert.strictEqual(address_ipv4.address, common.localhostIPv4);
13-
assert.strictEqual(address_ipv4.port, common.PORT);
13+
assert.strictEqual(typeof address_ipv4.port, 'number');
14+
assert.ok(isFinite(address_ipv4.port));
15+
assert.ok(address_ipv4.port > 0);
1416
assert.strictEqual(address_ipv4.family, family_ipv4);
1517
socket_ipv4.close();
1618
});
@@ -20,7 +22,7 @@ socket_ipv4.on('error', function(e) {
2022
socket_ipv4.close();
2123
});
2224

23-
socket_ipv4.bind(common.PORT, common.localhostIPv4);
25+
socket_ipv4.bind(0, common.localhostIPv4);
2426

2527
// IPv6 Test
2628
var localhost_ipv6 = '::1';
@@ -30,7 +32,9 @@ var family_ipv6 = 'IPv6';
3032
socket_ipv6.on('listening', function() {
3133
var address_ipv6 = socket_ipv6.address();
3234
assert.strictEqual(address_ipv6.address, localhost_ipv6);
33-
assert.strictEqual(address_ipv6.port, common.PORT);
35+
assert.strictEqual(typeof address_ipv6.port, 'number');
36+
assert.ok(isFinite(address_ipv6.port));
37+
assert.ok(address_ipv6.port > 0);
3438
assert.strictEqual(address_ipv6.family, family_ipv6);
3539
socket_ipv6.close();
3640
});
@@ -40,4 +44,4 @@ socket_ipv6.on('error', function(e) {
4044
socket_ipv6.close();
4145
});
4246

43-
socket_ipv6.bind(common.PORT, localhost_ipv6);
47+
socket_ipv6.bind(0, localhost_ipv6);

test/parallel/test-dgram-bind-default-address.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ if (common.inFreeBSDJail) {
99
return;
1010
}
1111

12-
dgram.createSocket('udp4').bind(common.PORT + 0, common.mustCall(function() {
13-
assert.equal(this.address().port, common.PORT + 0);
12+
dgram.createSocket('udp4').bind(0, common.mustCall(function() {
13+
assert.strictEqual(typeof this.address().port, 'number');
14+
assert.ok(isFinite(this.address().port));
15+
assert.ok(this.address().port > 0);
1416
assert.equal(this.address().address, '0.0.0.0');
1517
this.close();
1618
}));
@@ -20,8 +22,10 @@ if (!common.hasIPv6) {
2022
return;
2123
}
2224

23-
dgram.createSocket('udp6').bind(common.PORT + 1, common.mustCall(function() {
24-
assert.equal(this.address().port, common.PORT + 1);
25+
dgram.createSocket('udp6').bind(0, common.mustCall(function() {
26+
assert.strictEqual(typeof this.address().port, 'number');
27+
assert.ok(isFinite(this.address().port));
28+
assert.ok(this.address().port > 0);
2529
var address = this.address().address;
2630
if (address === '::ffff:0.0.0.0')
2731
address = '::';

test/parallel/test-dgram-empty-packet.js

+20-19
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,27 @@ if (process.platform === 'darwin') {
1313

1414
client = dgram.createSocket('udp4');
1515

16-
client.bind(common.PORT);
17-
18-
function callback() {
19-
callbacks++;
20-
if (callbacks == 2) {
21-
clearTimeout(timer);
22-
client.close();
23-
} else if (callbacks > 2) {
24-
throw new Error('the callbacks should be called only two times');
16+
client.bind(0, function() {
17+
function callback() {
18+
callbacks++;
19+
if (callbacks == 2) {
20+
clearTimeout(timer);
21+
client.close();
22+
} else if (callbacks > 2) {
23+
throw new Error('the callbacks should be called only two times');
24+
}
2525
}
26-
}
2726

28-
client.on('message', function(buffer, bytes) {
29-
callback();
30-
});
27+
client.on('message', function(buffer, bytes) {
28+
callback();
29+
});
3130

32-
client.send(new Buffer(1), 0, 0, common.PORT, '127.0.0.1', function(err, len) {
33-
callback();
34-
});
31+
const port = this.address().port;
32+
client.send(new Buffer(1), 0, 0, port, '127.0.0.1', function(err, len) {
33+
callback();
34+
});
3535

36-
timer = setTimeout(function() {
37-
throw new Error('Timeout');
38-
}, 200);
36+
timer = setTimeout(function() {
37+
throw new Error('Timeout');
38+
}, 200);
39+
});

test/parallel/test-dgram-error-message-address.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ var socket_ipv4 = dgram.createSocket('udp4');
99
socket_ipv4.on('listening', common.fail);
1010

1111
socket_ipv4.on('error', common.mustCall(function(e) {
12-
assert.equal(e.message, 'bind EADDRNOTAVAIL 1.1.1.1:' + common.PORT);
12+
assert.strictEqual(e.port, undefined);
13+
assert.equal(e.message, 'bind EADDRNOTAVAIL 1.1.1.1');
1314
assert.equal(e.address, '1.1.1.1');
14-
assert.equal(e.port, common.PORT);
1515
assert.equal(e.code, 'EADDRNOTAVAIL');
1616
socket_ipv4.close();
1717
}));
1818

19-
socket_ipv4.bind(common.PORT, '1.1.1.1');
19+
socket_ipv4.bind(0, '1.1.1.1');
2020

2121
// IPv6 Test
2222
var socket_ipv6 = dgram.createSocket('udp6');
@@ -27,10 +27,10 @@ socket_ipv6.on('error', common.mustCall(function(e) {
2727
// EAFNOSUPPORT or EPROTONOSUPPORT means IPv6 is disabled on this system.
2828
var allowed = ['EADDRNOTAVAIL', 'EAFNOSUPPORT', 'EPROTONOSUPPORT'];
2929
assert.notEqual(allowed.indexOf(e.code), -1);
30-
assert.equal(e.message, 'bind ' + e.code + ' 111::1:' + common.PORT);
30+
assert.strictEqual(e.port, undefined);
31+
assert.equal(e.message, 'bind ' + e.code + ' 111::1');
3132
assert.equal(e.address, '111::1');
32-
assert.equal(e.port, common.PORT);
3333
socket_ipv6.close();
3434
}));
3535

36-
socket_ipv6.bind(common.PORT, '111::1');
36+
socket_ipv6.bind(0, '111::1');

test/parallel/test-dgram-implicit-bind.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
var common = require('../common');
2+
require('../common');
33
var assert = require('assert');
44
var dgram = require('dgram');
55

@@ -22,8 +22,9 @@ target.on('message', function(buf) {
2222

2323
target.on('listening', function() {
2424
// Second .send() call should not throw a bind error.
25-
source.send(Buffer('abc'), 0, 3, common.PORT, '127.0.0.1');
26-
source.send(Buffer('def'), 0, 3, common.PORT, '127.0.0.1');
25+
const port = this.address().port;
26+
source.send(Buffer('abc'), 0, 3, port, '127.0.0.1');
27+
source.send(Buffer('def'), 0, 3, port, '127.0.0.1');
2728
});
2829

29-
target.bind(common.PORT);
30+
target.bind(0);

test/parallel/test-dgram-multicast-setTTL.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use strict';
2-
const common = require('../common');
2+
require('../common');
33
const assert = require('assert');
44
const dgram = require('dgram');
55
const socket = dgram.createSocket('udp4');
66
let thrown = false;
77

8-
socket.bind(common.PORT);
8+
socket.bind(0);
99
socket.on('listening', function() {
1010
socket.setMulticastTTL(16);
1111

test/parallel/test-dgram-send-callback-recursive.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@ let received = 0;
99
let sent = 0;
1010
const limit = 10;
1111
let async = false;
12+
let port;
1213

1314
function onsend() {
1415
if (sent++ < limit) {
15-
client.send(
16-
chunk, 0, chunk.length, common.PORT, common.localhostIPv4, onsend);
16+
client.send(chunk, 0, chunk.length, port, common.localhostIPv4, onsend);
1717
} else {
1818
assert.strictEqual(async, true, 'Send should be asynchronous.');
1919
}
2020
}
2121

2222
client.on('listening', function() {
23+
port = this.address().port;
24+
2325
setImmediate(function() {
2426
async = true;
2527
});
@@ -38,4 +40,4 @@ client.on('close', common.mustCall(function() {
3840
assert.equal(received, limit);
3941
}));
4042

41-
client.bind(common.PORT);
43+
client.bind(0);

0 commit comments

Comments
 (0)