mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-24 02:30:13 +00:00
Wait for port to be freed before continuing
This commit is contained in:
23
test/templates/util/ports.ts
Normal file
23
test/templates/util/ports.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import * as portastic from 'portastic';
|
||||
const pollInterval = 500;
|
||||
|
||||
export function waitUntilPortState(port: number, isOpen: boolean, timeoutMs: number, callback: (err: any) => void) {
|
||||
if (!(timeoutMs > 0)) {
|
||||
throw new Error(`Timed out after ${ timeoutMs }ms waiting for port ${ port } to become ${ isOpen ? 'free' : 'in use' }`);
|
||||
}
|
||||
|
||||
portastic.test(port).then(
|
||||
actualIsOpenState => {
|
||||
if (actualIsOpenState === isOpen) {
|
||||
// Desired state is reached
|
||||
callback(null);
|
||||
} else {
|
||||
// Wait longer
|
||||
setTimeout(() => {
|
||||
waitUntilPortState(port, isOpen, timeoutMs - pollInterval, callback);
|
||||
}, pollInterval);
|
||||
}
|
||||
},
|
||||
callback
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user