Skip to content

Commit d289678

Browse files
Sebastian Plesciucaddaleax
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 Reviewed-By: Anna Henningsen <[email protected]>
1 parent 28f535a commit d289678

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());
20+
socket.on('close', common.mustCall());
21+
}));

test/parallel/test-dgram-close.js

+20-11
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,23 @@ const buf = Buffer.alloc(1024, 42);
3232
let socket = dgram.createSocket('udp4');
3333
const handle = socket._handle;
3434

35-
socket.send(buf, 0, buf.length, common.PORT, 'localhost');
36-
assert.strictEqual(socket.close(common.mustCall()), socket);
37-
socket.on('close', common.mustCall());
38-
socket = null;
39-
40-
// Verify that accessing handle after closure doesn't throw
41-
setImmediate(function() {
42-
setImmediate(function() {
43-
console.log('Handle fd is: ', handle.fd);
44-
});
45-
});
35+
// get a random port for send
36+
const portGetter = dgram.createSocket('udp4')
37+
.bind(0, 'localhost', common.mustCall(() => {
38+
socket.send(buf, 0, buf.length,
39+
portGetter.address().port,
40+
portGetter.address().address);
41+
42+
assert.strictEqual(socket.close(common.mustCall()), socket);
43+
socket.on('close', common.mustCall());
44+
socket = null;
45+
46+
// Verify that accessing handle after closure doesn't throw
47+
setImmediate(function() {
48+
setImmediate(function() {
49+
console.log('Handle fd is: ', handle.fd);
50+
});
51+
});
52+
53+
portGetter.close();
54+
}));

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
}
@@ -98,7 +98,8 @@ if (process.env.BOUND === 'y') {
9898
source.unref();
9999
}
100100

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

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

+13-8
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,17 @@ const dgram = require('dgram');
2929

3030
const socket = dgram.createSocket('udp4');
3131
const buf = Buffer.from([1, 2, 3, 4]);
32+
const portGetter = dgram.createSocket('udp4')
33+
.bind(0, 'localhost', common.mustCall(() => {
34+
const address = portGetter.address();
35+
portGetter.close(common.mustCall(() => {
36+
socket.send(buf, 0, 0, address.port, address.address, common.noop);
37+
socket.send(buf, 0, 4, address.port, address.address, common.noop);
38+
socket.send(buf, 1, 3, address.port, address.address, common.noop);
39+
socket.send(buf, 3, 1, address.port, address.address, common.noop);
40+
// Since length of zero means nothing, don't error despite OOB.
41+
socket.send(buf, 4, 0, address.port, address.address, common.noop);
3242

33-
socket.send(buf, 0, 0, common.PORT, '127.0.0.1', common.noop); // useful? no
34-
socket.send(buf, 0, 4, common.PORT, '127.0.0.1', common.noop);
35-
socket.send(buf, 1, 3, common.PORT, '127.0.0.1', common.noop);
36-
socket.send(buf, 3, 1, common.PORT, '127.0.0.1', common.noop);
37-
// Since length of zero means nothing, don't error despite OOB.
38-
socket.send(buf, 4, 0, common.PORT, '127.0.0.1', common.noop);
39-
40-
socket.close();
43+
socket.close();
44+
}));
45+
}));

0 commit comments

Comments
 (0)