File tree Expand file tree Collapse file tree 1 file changed +8
-2
lines changed
packages/mongodb-memory-server-core/src/util/getport Expand file tree Collapse file tree 1 file changed +8
-2
lines changed Original file line number Diff line number Diff line change @@ -56,13 +56,15 @@ export async function getFreePort(
5656 PORTS_CACHE . timeSet = Date . now ( ) ;
5757 }
5858
59+ const exp_net0listen = envToBool ( resolveConfig ( ResolveConfigVariables . EXP_NET0LISTEN ) ) ;
60+
5961 let tries = 0 ;
6062 while ( tries <= max_tries ) {
6163 tries += 1 ;
6264
6365 let nextPort : number ;
6466
65- if ( envToBool ( resolveConfig ( ResolveConfigVariables . EXP_NET0LISTEN ) ) ) {
67+ if ( exp_net0listen ) {
6668 // "0" means to use ".listen" random port
6769 nextPort = tries === 1 ? firstPort : 0 ;
6870 } else {
@@ -71,7 +73,8 @@ export async function getFreePort(
7173 }
7274
7375 // try next port, because it is already in the cache
74- if ( PORTS_CACHE . ports . has ( nextPort ) ) {
76+ // unless port is "0" which will use "net.listen(0)"
77+ if ( PORTS_CACHE . ports . has ( nextPort ) && nextPort !== 0 ) {
7578 continue ;
7679 }
7780
@@ -81,6 +84,9 @@ export async function getFreePort(
8184
8285 const triedPort = await tryPort ( nextPort ) ;
8386
87+ // returned port can be different than the "nextPort" (if EXP_NET0LISTEN)
88+ PORTS_CACHE . ports . add ( nextPort ) ;
89+
8490 if ( triedPort > 0 ) {
8591 return triedPort ;
8692 }
You can’t perform that action at this time.
0 commit comments