Skip to content

Commit 33af09f

Browse files
Trottitaloacasas
authored andcommitted
test,net: add tests for server.connections
There were no tests confirming situations where server.connections should return `null`. Add a test for that situation. Expand existing server.connection test slightly to check value. Refactor (mostly spacing) code for server.connections setter. PR-URL: #10762 Reviewed-By: James M Snell <[email protected]>
1 parent d751afa commit 33af09f

File tree

3 files changed

+56
-7
lines changed

3 files changed

+56
-7
lines changed

lib/net.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1132,9 +1132,8 @@ function Server(options, connectionListener) {
11321132
return this._connections;
11331133
}, 'Server.connections property is deprecated. ' +
11341134
'Use Server.getConnections method instead.'),
1135-
set: internalUtil.deprecate((val) => {
1136-
return (this._connections = val);
1137-
}, 'Server.connections property is deprecated.'),
1135+
set: internalUtil.deprecate((val) => (this._connections = val),
1136+
'Server.connections property is deprecated.'),
11381137
configurable: true, enumerable: false
11391138
});
11401139

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const fork = require('child_process').fork;
5+
const net = require('net');
6+
7+
if (process.argv[2] === 'child') {
8+
9+
process.on('message', (msg, socket) => {
10+
socket.end('goodbye');
11+
});
12+
13+
process.send('hello');
14+
15+
} else {
16+
17+
const child = fork(process.argv[1], ['child']);
18+
19+
const runTest = common.mustCall(() => {
20+
21+
const server = net.createServer();
22+
23+
// server.connections should start as 0
24+
assert.strictEqual(server.connections, 0);
25+
server.on('connection', (socket) => {
26+
child.send({what: 'socket'}, socket);
27+
});
28+
server.on('close', () => {
29+
child.kill();
30+
});
31+
32+
server.listen(0, common.mustCall(() => {
33+
const connect = net.connect(server.address().port);
34+
35+
connect.on('close', common.mustCall(() => {
36+
// now server.connections should be null
37+
assert.strictEqual(server.connections, null);
38+
server.close();
39+
}));
40+
}));
41+
});
42+
43+
child.on('message', runTest);
44+
}
+10-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33
const assert = require('assert');
44

55
const net = require('net');
66

7-
// test that server.connections property is no longer enumerable now that it
8-
// has been marked as deprecated
9-
107
const server = new net.Server();
118

9+
const expectedWarning = 'Server.connections property is deprecated. ' +
10+
'Use Server.getConnections method instead.';
11+
12+
common.expectWarning('DeprecationWarning', expectedWarning);
13+
14+
// test that server.connections property is no longer enumerable now that it
15+
// has been marked as deprecated
1216
assert.strictEqual(Object.keys(server).indexOf('connections'), -1);
17+
18+
assert.strictEqual(server.connections, 0);

0 commit comments

Comments
 (0)