Skip to content

Commit 474e9d6

Browse files
committedMar 16, 2017
test: add more test cases of server.listen option
* Test listening with different handle and fd * Test listening without callback PR-URL: #11778 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Sam Roberts <[email protected]>
1 parent e296ffb commit 474e9d6

3 files changed

+206
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const net = require('net');
6+
const fs = require('fs');
7+
const uv = process.binding('uv');
8+
const TCP = process.binding('tcp_wrap').TCP;
9+
const Pipe = process.binding('pipe_wrap').Pipe;
10+
11+
common.refreshTmpDir();
12+
13+
function closeServer() {
14+
return common.mustCall(function() {
15+
this.close();
16+
});
17+
}
18+
19+
// server.listen(pipe) creates a new pipe wrap,
20+
// so server.close() doesn't actually unlink this existing pipe.
21+
// It needs to be unlinked separately via handle.close()
22+
function closePipeServer(handle) {
23+
return common.mustCall(function() {
24+
this.close();
25+
handle.close();
26+
});
27+
}
28+
29+
let counter = 0;
30+
31+
// Avoid conflict with listen-path
32+
function randomPipePath() {
33+
return common.PIPE + '-listen-handle-' + (counter++);
34+
}
35+
36+
function randomHandle(type) {
37+
let handle, errno, handleName;
38+
if (type === 'tcp') {
39+
handle = new TCP();
40+
errno = handle.bind('0.0.0.0', 0);
41+
handleName = 'arbitrary tcp port';
42+
} else {
43+
const path = randomPipePath();
44+
handle = new Pipe();
45+
errno = handle.bind(path);
46+
handleName = `pipe ${path}`;
47+
}
48+
49+
if (errno < 0) { // uv.errname requires err < 0
50+
assert(errno >= 0, `unable to bind ${handleName}: ${uv.errname(errno)}`);
51+
}
52+
if (!common.isWindows) { // fd doesn't work on windows
53+
// err >= 0 but fd = -1, should not happen
54+
assert.notStrictEqual(handle.fd, -1,
55+
`Bound ${handleName} has fd -1 and errno ${errno}`);
56+
}
57+
return handle;
58+
}
59+
60+
// Not a public API, used by child_process
61+
{
62+
// Test listen(tcp)
63+
net.createServer()
64+
.listen(randomHandle('tcp'))
65+
.on('listening', closeServer());
66+
// Test listen(tcp, cb)
67+
net.createServer()
68+
.listen(randomHandle('tcp'), closeServer());
69+
}
70+
71+
function randomPipes(number) {
72+
const arr = [];
73+
for (let i = 0; i < number; ++i) {
74+
arr.push(randomHandle('pipe'));
75+
}
76+
return arr;
77+
}
78+
79+
// Not a public API, used by child_process
80+
if (!common.isWindows) { // Windows doesn't support {fd: <n>}
81+
const handles = randomPipes(2); // generate pipes in advance
82+
// Test listen(pipe)
83+
net.createServer()
84+
.listen(handles[0])
85+
.on('listening', closePipeServer(handles[0]));
86+
// Test listen(pipe, cb)
87+
net.createServer()
88+
.listen(handles[1], closePipeServer(handles[1]));
89+
}
90+
91+
{
92+
// Test listen({handle: tcp}, cb)
93+
net.createServer()
94+
.listen({handle: randomHandle('tcp')}, closeServer());
95+
// Test listen({handle: tcp})
96+
net.createServer()
97+
.listen({handle: randomHandle('tcp')})
98+
.on('listening', closeServer());
99+
// Test listen({_handle: tcp}, cb)
100+
net.createServer()
101+
.listen({_handle: randomHandle('tcp')}, closeServer());
102+
// Test listen({_handle: tcp})
103+
net.createServer()
104+
.listen({_handle: randomHandle('tcp')})
105+
.on('listening', closeServer());
106+
}
107+
108+
if (!common.isWindows) { // Windows doesn't support {fd: <n>}
109+
// Test listen({fd: tcp.fd}, cb)
110+
net.createServer()
111+
.listen({fd: randomHandle('tcp').fd}, closeServer());
112+
// Test listen({fd: tcp.fd})
113+
net.createServer()
114+
.listen({fd: randomHandle('tcp').fd})
115+
.on('listening', closeServer());
116+
}
117+
118+
if (!common.isWindows) { // Windows doesn't support {fd: <n>}
119+
const handles = randomPipes(6); // generate pipes in advance
120+
// Test listen({handle: pipe}, cb)
121+
net.createServer()
122+
.listen({handle: handles[0]}, closePipeServer(handles[0]));
123+
// Test listen({handle: pipe})
124+
net.createServer()
125+
.listen({handle: handles[1]})
126+
.on('listening', closePipeServer(handles[1]));
127+
// Test listen({_handle: pipe}, cb)
128+
net.createServer()
129+
.listen({_handle: handles[2]}, closePipeServer(handles[2]));
130+
// Test listen({_handle: pipe})
131+
net.createServer()
132+
.listen({_handle: handles[3]})
133+
.on('listening', closePipeServer(handles[3]));
134+
// Test listen({fd: pipe.fd}, cb)
135+
net.createServer()
136+
.listen({fd: handles[4].fd}, closePipeServer(handles[4]));
137+
// Test listen({fd: pipe.fd})
138+
net.createServer()
139+
.listen({fd: handles[5].fd})
140+
.on('listening', closePipeServer(handles[5]));
141+
}
142+
143+
if (!common.isWindows) { // Windows doesn't support {fd: <n>}
144+
// Test invalid fd
145+
const fd = fs.openSync(__filename, 'r');
146+
net.createServer()
147+
.listen({fd: fd}, common.mustNotCall())
148+
.on('error', common.mustCall(function(err) {
149+
assert.strictEqual(err + '', 'Error: listen EINVAL');
150+
this.close();
151+
}));
152+
}

‎test/parallel/test-net-server-listen-options.js

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ function listenError(literals, ...values) {
2222
net.createServer().listen().on('listening', common.mustCall(close));
2323
// Test listen(cb)
2424
net.createServer().listen(common.mustCall(close));
25+
// Test listen(port)
26+
net.createServer().listen(0).on('listening', common.mustCall(close));
27+
// Test listen({port})
28+
net.createServer().listen({port: 0})
29+
.on('listening', common.mustCall(close));
2530
}
2631

2732
// Test listen(port, cb) and listen({port: port}, cb) combinations
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const net = require('net');
5+
6+
common.refreshTmpDir();
7+
8+
function closeServer() {
9+
return common.mustCall(function() {
10+
this.close();
11+
});
12+
}
13+
14+
let counter = 0;
15+
16+
// Avoid conflict with listen-handle
17+
function randomPipePath() {
18+
return common.PIPE + '-listen-path-' + (counter++);
19+
}
20+
21+
// Test listen(path)
22+
{
23+
const handlePath = randomPipePath();
24+
net.createServer()
25+
.listen(handlePath)
26+
.on('listening', closeServer());
27+
}
28+
29+
// Test listen({path})
30+
{
31+
const handlePath = randomPipePath();
32+
net.createServer()
33+
.listen({path: handlePath})
34+
.on('listening', closeServer());
35+
}
36+
37+
// Test listen(path, cb)
38+
{
39+
const handlePath = randomPipePath();
40+
net.createServer()
41+
.listen(handlePath, closeServer());
42+
}
43+
44+
// Test listen(path, cb)
45+
{
46+
const handlePath = randomPipePath();
47+
net.createServer()
48+
.listen({path: handlePath}, closeServer());
49+
}

0 commit comments

Comments
 (0)