Skip to content

Commit bc57514

Browse files
fix: retry finding port when port is null and get ports in sequence (#1993)
1 parent 2029211 commit bc57514

5 files changed

Lines changed: 103 additions & 14 deletions

File tree

‎lib/utils/findPort.js‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
'use strict';
22

3-
const{ getPortPromise }=require('portfinder');
3+
constpRetry=require('p-retry');
4+
constportfinder=require('portfinder');
45
constdefaultPort=require('./defaultPort');
56
constdefaultTo=require('./defaultTo');
67
consttryParseInt=require('./tryParseInt');
78

9+
functionrunPortFinder(){
10+
returnnewPromise((resolve,reject)=>{
11+
portfinder.basePort=defaultPort;
12+
portfinder.getPort((error,port)=>{
13+
if(error){
14+
returnreject(error);
15+
}
16+
17+
returnresolve(port);
18+
});
19+
});
20+
}
21+
822
functionfindPort(port){
9-
if(typeofport!=='undefined'){
23+
if(port){
1024
returnPromise.resolve(port);
1125
}
1226

@@ -19,10 +33,7 @@ function findPort(port) {
1933
3
2034
);
2135

22-
returngetPortPromise({
23-
port: defaultPort,
24-
stopPort: defaultPort+defaultPortRetry,
25-
});
36+
returnpRetry(runPortFinder,{retries: defaultPortRetry});
2637
}
2738

2839
module.exports=findPort;

‎package-lock.json‎

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"killable": "^1.0.1",
5252
"loglevel": "^1.6.2",
5353
"opn": "^5.5.0",
54+
"p-retry": "^3.0.1",
5455
"portfinder": "^1.0.20",
5556
"schema-utils": "^1.0.0",
5657
"selfsigned": "^1.10.4",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`findPort util should throws the error when the port isn't found 1`] =`"No open ports found in between 8080 and 8085"`;
3+
exports[`findPort util should throws the error when the port isn't found 1`] =`"busy"`;

‎test/server/utils/findPort.test.js‎

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
consthttp=require('http');
4+
constportfinder=require('portfinder');
45
constfindPort=require('../../../lib/utils/findPort');
56

67
describe('findPort util',()=>{
@@ -23,7 +24,7 @@ describe('findPort util', () => {
2324
});
2425

2526
functioncreateDummyServers(n){
26-
return[...newArray(n)].reduce((p,_,i)=>{
27+
return(Array.isArray(n) ? n : [...newArray(n)]).reduce((p,_,i)=>{
2728
returnp.then(()=>{
2829
returnnewPromise((resolve)=>{
2930
constserver=http.createServer();
@@ -42,25 +43,88 @@ describe('findPort util', () => {
4243
});
4344
});
4445

45-
it('should retry finding the port for up to defaultPortRetry times',()=>{
46-
constretryCount=5;
46+
it.only('should returns the port when the port is null',()=>{
47+
constretryCount=2;
48+
49+
process.env.DEFAULT_PORT_RETRY=2;
50+
51+
returncreateDummyServers(retryCount)
52+
.then(()=>findPort(null))
53+
.then((port)=>{
54+
expect(port).toEqual(8080+retryCount);
55+
});
56+
});
57+
58+
it('should returns the port when the port is undefined',()=>{
59+
constretryCount=2;
60+
61+
process.env.DEFAULT_PORT_RETRY=2;
62+
63+
return(
64+
createDummyServers(retryCount)
65+
// eslint-disable-next-line no-undefined
66+
.then(()=>findPort(undefined))
67+
.then((port)=>{
68+
expect(port).toEqual(8080+retryCount);
69+
})
70+
);
71+
});
72+
73+
it('should retry finding the port for up to defaultPortRetry times (number)',()=>{
74+
constretryCount=3;
4775

4876
process.env.DEFAULT_PORT_RETRY=retryCount;
4977

5078
returncreateDummyServers(retryCount)
51-
.then(findPort)
79+
.then(()=>findPort())
5280
.then((port)=>{
5381
expect(port).toEqual(8080+retryCount);
5482
});
5583
});
5684

85+
it('should retry finding the port for up to defaultPortRetry times (string)',()=>{
86+
constretryCount=3;
87+
88+
process.env.DEFAULT_PORT_RETRY=`${retryCount}`;
89+
90+
returncreateDummyServers(retryCount)
91+
.then(()=>findPort())
92+
.then((port)=>{
93+
expect(port).toEqual(8080+retryCount);
94+
});
95+
});
96+
97+
it('should retry finding the port when serial ports are busy',()=>{
98+
constbusyPorts=[8080,8081,8082,8083];
99+
100+
process.env.DEFAULT_PORT_RETRY=3;
101+
102+
returncreateDummyServers(busyPorts)
103+
.then(()=>findPort())
104+
.then((port)=>{
105+
expect(port).toEqual(8080+busyPorts.length);
106+
});
107+
});
108+
57109
it("should throws the error when the port isn't found",()=>{
58-
process.env.DEFAULT_PORT_RETRY=5;
110+
expect.assertions(1);
111+
112+
constspy=jest
113+
.spyOn(portfinder,'getPort')
114+
.mockImplementation((callback)=>{
115+
returncallback(newError('busy'));
116+
});
59117

60-
returncreateDummyServers(10)
61-
.then(findPort)
118+
constretryCount=1;
119+
120+
process.env.DEFAULT_PORT_RETRY=0;
121+
122+
returncreateDummyServers(retryCount)
123+
.then(()=>findPort())
62124
.catch((err)=>{
63125
expect(err.message).toMatchSnapshot();
126+
127+
spy.mockRestore();
64128
});
65129
});
66130
});

0 commit comments

Comments
 (0)