Skip to content

Commit 06b613e

Browse files
Sebastian Plesciucgibfahn
Sebastian Plesciuc
authored andcommitted
test: dynamic port in dgram tests
Removed common.PORT from test-dgram-close-in-listening, test-dgram-close-is-not-callback, test-dgram-close, test-dgram-exclusive-implicit-bind and test-dgram-oob-buffer in order to eliminate the possibility of port collision. Refs: #12376 PR-URL: #12623 Backport-PR-URL: #13792 Reviewed-By: Anna Henningsen <[email protected]>
1 parent e08113e commit 06b613e

5 files changed

+62
-32
lines changed

test/parallel/test-dgram-close-in-listening.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ socket.on('listening', function() {
1313
socket.close();
1414
});
1515

16-
// adds a listener to 'listening' to send the data when
17-
// the socket is available
18-
socket.send(buf, 0, buf.length, common.PORT, 'localhost');
16+
// get a random port for send
17+
const portGetter = dgram.createSocket('udp4')
18+
.bind(0, 'localhost', common.mustCall(() => {
19+
// adds a listener to 'listening' to send the data when
20+
// the socket is available
21+
socket.send(buf, 0, buf.length,
22+
portGetter.address().port,
23+
portGetter.address().address);
24+
25+
portGetter.close();
26+
}));

test/parallel/test-dgram-close-is-not-callback.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@ const buf = Buffer.alloc(1024, 42);
66

77
const socket = dgram.createSocket('udp4');
88

9-
socket.send(buf, 0, buf.length, common.PORT, 'localhost');
9+
// get a random port for send
10+
const portGetter = dgram.createSocket('udp4')
11+
.bind(0, 'localhost', common.mustCall(() => {
12+
socket.send(buf, 0, buf.length,
13+
portGetter.address().port,
14+
portGetter.address().address);
1015

11-
// if close callback is not function, ignore the argument.
12-
socket.close('bad argument');
16+
// if close callback is not function, ignore the argument.
17+
socket.close('bad argument');
18+
portGetter.close();
1319

14-
socket.on('close', common.mustCall(function() {}));
20+
socket.on('close', common.mustCall(function() {}));
21+
}));

test/parallel/test-dgram-close.js

+19-10
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,23 @@ const buf = Buffer.alloc(1024, 42);
1111
let socket = dgram.createSocket('udp4');
1212
const handle = socket._handle;
1313

14-
socket.send(buf, 0, buf.length, common.PORT, 'localhost');
15-
assert.strictEqual(socket.close(common.mustCall(function() {})), socket);
16-
socket.on('close', common.mustCall(function() {}));
17-
socket = null;
14+
// get a random port for send
15+
const portGetter = dgram.createSocket('udp4')
16+
.bind(0, 'localhost', common.mustCall(() => {
17+
socket.send(buf, 0, buf.length,
18+
portGetter.address().port,
19+
portGetter.address().address);
1820

19-
// Verify that accessing handle after closure doesn't throw
20-
setImmediate(function() {
21-
setImmediate(function() {
22-
console.log('Handle fd is: ', handle.fd);
23-
});
24-
});
21+
assert.strictEqual(socket.close(common.mustCall(function() {})), socket);
22+
socket.on('close', common.mustCall(function() {}));
23+
socket = null;
24+
25+
// Verify that accessing handle after closure doesn't throw
26+
setImmediate(function() {
27+
setImmediate(function() {
28+
console.log('Handle fd is: ', handle.fd);
29+
});
30+
});
31+
32+
portGetter.close();
33+
}));

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

+7-6
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ if (cluster.isMaster) {
7070
});
7171

7272
target.on('listening', function() {
73-
cluster.fork();
74-
cluster.fork();
73+
cluster.fork({PORT: target.address().port});
74+
cluster.fork({PORT: target.address().port});
7575
if (!common.isWindows) {
76-
cluster.fork({BOUND: 'y'});
77-
cluster.fork({BOUND: 'y'});
76+
cluster.fork({BOUND: 'y', PORT: target.address().port});
77+
cluster.fork({BOUND: 'y', PORT: target.address().port});
7878
}
7979
});
8080

81-
target.bind({port: common.PORT, exclusive: true});
81+
target.bind({port: 0, exclusive: true});
8282

8383
return;
8484
}
@@ -99,7 +99,8 @@ if (process.env.BOUND === 'y') {
9999
source.unref();
100100
}
101101

102+
assert(process.env.PORT);
102103
const buf = Buffer.from(process.pid.toString());
103104
interval = setInterval(() => {
104-
source.send(buf, common.PORT, '127.0.0.1');
105+
source.send(buf, process.env.PORT, '127.0.0.1');
105106
}, 1).unref();

test/parallel/test-dgram-oob-buffer.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@ const dgram = require('dgram');
88

99
const socket = dgram.createSocket('udp4');
1010
const buf = Buffer.from([1, 2, 3, 4]);
11+
const portGetter = dgram.createSocket('udp4')
12+
.bind(0, 'localhost', common.mustCall(() => {
13+
const address = portGetter.address();
14+
portGetter.close(common.mustCall(() => {
15+
function ok() {}
16+
socket.send(buf, 0, 0, address.port, address.address, ok);
17+
socket.send(buf, 0, 4, address.port, address.address, ok);
18+
socket.send(buf, 1, 3, address.port, address.address, ok);
19+
socket.send(buf, 3, 1, address.port, address.address, ok);
20+
// Since length of zero means nothing, don't error despite OOB.
21+
socket.send(buf, 4, 0, address.port, address.address, ok);
1122

12-
function ok() {}
13-
socket.send(buf, 0, 0, common.PORT, '127.0.0.1', ok); // useful? no
14-
socket.send(buf, 0, 4, common.PORT, '127.0.0.1', ok);
15-
socket.send(buf, 1, 3, common.PORT, '127.0.0.1', ok);
16-
socket.send(buf, 3, 1, common.PORT, '127.0.0.1', ok);
17-
// Since length of zero means nothing, don't error despite OOB.
18-
socket.send(buf, 4, 0, common.PORT, '127.0.0.1', ok);
19-
20-
socket.close();
23+
socket.close();
24+
}));
25+
}));

0 commit comments

Comments
 (0)