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: #11649Fixes: #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‎

Lines changed: 90 additions & 68 deletions
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-
constserver=https.createServer(options,(req,res)=>{
22-
res.end('hello world\n');
23-
});
24-
2521
constexpectedHeader=/^HTTP\/1.1200OK/;
2622
constexpectedBody=/helloworld\n/;
2723
constexpectCertError=/^Error:unabletoverifythefirstcertificate$/;
@@ -42,83 +38,109 @@ const checkRequest = (socket, server) =>{
4238
}));
4339
};
4440

41+
functioncreateServer(){
42+
returnhttps.createServer(options,(req,res)=>{
43+
res.end('hello world\n');
44+
});
45+
}
46+
4547
// use option connect
46-
server.listen(0,common.mustCall(()=>{
47-
constport=server.address().port;
48-
consthost='localhost';
49-
constoptions={
50-
port: port,
51-
host: host,
52-
rejectUnauthorized: false,
53-
_agentKey: agent.getName({
48+
{
49+
constserver=createServer();
50+
server.listen(0,common.mustCall(()=>{
51+
constport=server.address().port;
52+
consthost='localhost';
53+
constoptions={
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-
constsocket=agent.createConnection(options);
60-
checkRequest(socket,server);
61-
}));
63+
constsocket=agent.createConnection(options);
64+
checkRequest(socket,server);
65+
}));
66+
}
6267

6368
// use port and option connect
64-
server.listen(0,common.mustCall(()=>{
65-
constport=server.address().port;
66-
consthost='localhost';
67-
constoptions={
68-
rejectUnauthorized: false,
69-
_agentKey: agent.getName({
70-
port: port,
71-
host: host,
72-
}),
73-
};
74-
constsocket=agent.createConnection(port,options);
75-
checkRequest(socket,server);
76-
}));
69+
{
70+
constserver=createServer();
71+
server.listen(0,common.mustCall(()=>{
72+
constport=server.address().port;
73+
consthost='localhost';
74+
constoptions={
75+
rejectUnauthorized: false,
76+
_agentKey: agent.getName({
77+
port: port,
78+
host: host,
79+
}),
80+
};
81+
constsocket=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-
constport=server.address().port;
81-
consthost='localhost';
82-
constoptions={
83-
rejectUnauthorized: false,
84-
_agentKey: agent.getName({
85-
port: port,
86-
host: host,
87-
}),
88-
};
89-
constsocket=agent.createConnection(port,host,options);
90-
checkRequest(socket,server);
91-
}));
87+
{
88+
constserver=createServer();
89+
server.listen(0,common.mustCall(()=>{
90+
constport=server.address().port;
91+
consthost='localhost';
92+
constoptions={
93+
rejectUnauthorized: false,
94+
_agentKey: agent.getName({
95+
port: port,
96+
host: host,
97+
}),
98+
};
99+
constsocket=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-
constport=server.address().port;
96-
consthost='localhost';
97-
constoptions={
98-
rejectUnauthorized: false,
99-
};
100-
constsocket=agent.createConnection(port,host,options);
101-
checkRequest(socket,server);
102-
}));
105+
{
106+
constserver=createServer();
107+
server.listen(0,common.mustCall(()=>{
108+
constport=server.address().port;
109+
consthost='localhost';
110+
constoptions={
111+
rejectUnauthorized: false,
112+
};
113+
constsocket=agent.createConnection(port,host,options);
114+
checkRequest(socket,server);
115+
}));
116+
}
103117

104118
// options is null
105-
server.listen(0,common.mustCall(()=>{
106-
constport=server.address().port;
107-
consthost='localhost';
108-
constoptions=null;
109-
constsocket=agent.createConnection(port,host,options);
110-
socket.on('error',common.mustCall((e)=>{
111-
assert(expectCertError.test(e.toString()));
119+
{
120+
constserver=createServer();
121+
server.listen(0,common.mustCall(()=>{
122+
constport=server.address().port;
123+
consthost='localhost';
124+
constoptions=null;
125+
constsocket=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-
constport=server.address().port;
118-
consthost='localhost';
119-
constoptions=undefined;
120-
constsocket=agent.createConnection(port,host,options);
121-
socket.on('error',common.mustCall((e)=>{
122-
assert(expectCertError.test(e.toString()));
134+
{
135+
constserver=createServer();
136+
server.listen(0,common.mustCall(()=>{
137+
constport=server.address().port;
138+
consthost='localhost';
139+
constoptions=undefined;
140+
constsocket=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)