Skip to content

Commit 00dd20c

Browse files
santigimenoaddaleax
authored andcommitted
test: fix flaky test-https-agent-create-connection
Use a different server instance for every test to avoid that they reuse the same port. PR-URL: #11649 Fixes: #11644 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]>
1 parent 91a222d commit 00dd20c

File tree

1 file changed

+90
-68
lines changed

1 file changed

+90
-68
lines changed

test/parallel/test-https-agent-create-connection.js

+90-68
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ const options = {
1818
cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem'),
1919
};
2020

21-
const server = https.createServer(options, (req, res) => {
22-
res.end('hello world\n');
23-
});
24-
2521
const expectedHeader = /^HTTP\/1.1 200 OK/;
2622
const expectedBody = /hello world\n/;
2723
const expectCertError = /^Error: unable to verify the first certificate$/;
@@ -42,83 +38,109 @@ const checkRequest = (socket, server) => {
4238
}));
4339
};
4440

41+
function createServer() {
42+
return https.createServer(options, (req, res) => {
43+
res.end('hello world\n');
44+
});
45+
}
46+
4547
// use option connect
46-
server.listen(0, common.mustCall(() => {
47-
const port = server.address().port;
48-
const host = 'localhost';
49-
const options = {
50-
port: port,
51-
host: host,
52-
rejectUnauthorized: false,
53-
_agentKey: agent.getName({
48+
{
49+
const server = createServer();
50+
server.listen(0, common.mustCall(() => {
51+
const port = server.address().port;
52+
const host = 'localhost';
53+
const options = {
5454
port: port,
5555
host: host,
56-
}),
57-
};
56+
rejectUnauthorized: false,
57+
_agentKey: agent.getName({
58+
port: port,
59+
host: host,
60+
}),
61+
};
5862

59-
const socket = agent.createConnection(options);
60-
checkRequest(socket, server);
61-
}));
63+
const socket = agent.createConnection(options);
64+
checkRequest(socket, server);
65+
}));
66+
}
6267

6368
// use port and option connect
64-
server.listen(0, common.mustCall(() => {
65-
const port = server.address().port;
66-
const host = 'localhost';
67-
const options = {
68-
rejectUnauthorized: false,
69-
_agentKey: agent.getName({
70-
port: port,
71-
host: host,
72-
}),
73-
};
74-
const socket = agent.createConnection(port, options);
75-
checkRequest(socket, server);
76-
}));
69+
{
70+
const server = createServer();
71+
server.listen(0, common.mustCall(() => {
72+
const port = server.address().port;
73+
const host = 'localhost';
74+
const options = {
75+
rejectUnauthorized: false,
76+
_agentKey: agent.getName({
77+
port: port,
78+
host: host,
79+
}),
80+
};
81+
const socket = agent.createConnection(port, options);
82+
checkRequest(socket, server);
83+
}));
84+
}
7785

7886
// use port and host and option connect
79-
server.listen(0, common.mustCall(() => {
80-
const port = server.address().port;
81-
const host = 'localhost';
82-
const options = {
83-
rejectUnauthorized: false,
84-
_agentKey: agent.getName({
85-
port: port,
86-
host: host,
87-
}),
88-
};
89-
const socket = agent.createConnection(port, host, options);
90-
checkRequest(socket, server);
91-
}));
87+
{
88+
const server = createServer();
89+
server.listen(0, common.mustCall(() => {
90+
const port = server.address().port;
91+
const host = 'localhost';
92+
const options = {
93+
rejectUnauthorized: false,
94+
_agentKey: agent.getName({
95+
port: port,
96+
host: host,
97+
}),
98+
};
99+
const socket = agent.createConnection(port, host, options);
100+
checkRequest(socket, server);
101+
}));
102+
}
92103

93104
// use port and host and option does not have agentKey
94-
server.listen(0, common.mustCall(() => {
95-
const port = server.address().port;
96-
const host = 'localhost';
97-
const options = {
98-
rejectUnauthorized: false,
99-
};
100-
const socket = agent.createConnection(port, host, options);
101-
checkRequest(socket, server);
102-
}));
105+
{
106+
const server = createServer();
107+
server.listen(0, common.mustCall(() => {
108+
const port = server.address().port;
109+
const host = 'localhost';
110+
const options = {
111+
rejectUnauthorized: false,
112+
};
113+
const socket = agent.createConnection(port, host, options);
114+
checkRequest(socket, server);
115+
}));
116+
}
103117

104118
// options is null
105-
server.listen(0, common.mustCall(() => {
106-
const port = server.address().port;
107-
const host = 'localhost';
108-
const options = null;
109-
const socket = agent.createConnection(port, host, options);
110-
socket.on('error', common.mustCall((e) => {
111-
assert(expectCertError.test(e.toString()));
119+
{
120+
const server = createServer();
121+
server.listen(0, common.mustCall(() => {
122+
const port = server.address().port;
123+
const host = 'localhost';
124+
const options = null;
125+
const socket = agent.createConnection(port, host, options);
126+
socket.on('error', common.mustCall((e) => {
127+
assert(expectCertError.test(e.toString()));
128+
server.close();
129+
}));
112130
}));
113-
}));
131+
}
114132

115133
// options is undefined
116-
server.listen(0, common.mustCall(() => {
117-
const port = server.address().port;
118-
const host = 'localhost';
119-
const options = undefined;
120-
const socket = agent.createConnection(port, host, options);
121-
socket.on('error', common.mustCall((e) => {
122-
assert(expectCertError.test(e.toString()));
134+
{
135+
const server = createServer();
136+
server.listen(0, common.mustCall(() => {
137+
const port = server.address().port;
138+
const host = 'localhost';
139+
const options = undefined;
140+
const socket = agent.createConnection(port, host, options);
141+
socket.on('error', common.mustCall((e) => {
142+
assert(expectCertError.test(e.toString()));
143+
server.close();
144+
}));
123145
}));
124-
}));
146+
}

0 commit comments

Comments
 (0)