Fix port testing logic in tests by being explicit about which interface we're checking

This commit is contained in:
SteveSandersonMS
2016-12-15 22:53:12 +00:00
parent f722dcf98d
commit dcb819f644
2 changed files with 9 additions and 7 deletions

View File

@@ -1,20 +1,21 @@
import * as portastic from 'portastic';
const pollInterval = 500;
export function waitUntilPortState(port: number, isOpen: boolean, timeoutMs: number, callback: (err: any) => void) {
export function waitUntilPortState(port: number, iface: string, isListening: 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' }`);
throw new Error(`Timed out waiting for port ${ port } to become ${ isListening ? 'in use' : 'free' }`);
}
portastic.test(port).then(
actualIsOpenState => {
if (actualIsOpenState === isOpen) {
portastic.test(port, iface).then(
actuallyIsAvailable => {
const actuallyIsListening = !actuallyIsAvailable;
if (actuallyIsListening === isListening) {
// Desired state is reached
callback(null);
} else {
// Wait longer
setTimeout(() => {
waitUntilPortState(port, isOpen, timeoutMs - pollInterval, callback);
waitUntilPortState(port, iface, isListening, timeoutMs - pollInterval, callback);
}, pollInterval);
}
},