Skip to content

Commit 9a8acad

Browse files
mscdexMyles Borins
authored andcommitted
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

Lines changed: 7 additions & 6 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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);

0 commit comments

Comments
 (0)