Skip to content
This repository has been archived by the owner on Apr 8, 2020. It is now read-only.

Commit

Permalink
Fix port testing logic in tests by being explicit about which interfa…
Browse files Browse the repository at this point in the history
…ce we're checking
  • Loading branch information
SteveSandersonMS committed Dec 15, 2016
1 parent f722dcf commit dcb819f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
3 changes: 2 additions & 1 deletion test/templates/util/aspnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { waitUntilPortState } from './ports';
const treeKill = require('tree-kill');
const crossSpawn: typeof childProcess.spawn = require('cross-spawn');
const defaultPort = 5000;
const defaultInterface = 'localhost';

export const defaultUrl = `http://localhost:${ defaultPort }`;

Expand Down Expand Up @@ -99,7 +100,7 @@ export class AspNetProcess {
// any outstanding requests. We have to wait for it really to be gone before continuing, otherwise
// the next test might be unable to start because of the port still being in use.
console.log(`Waiting until port ${ defaultPort } is closed...`);
waitUntilPortState(defaultPort, /* isOpen */ true, /* timeoutMs */ 15000, err => {
waitUntilPortState(defaultPort, defaultInterface, /* isListening */ false, /* timeoutMs */ 15000, err => {
if (err) {
callback(err);
} else {
Expand Down
13 changes: 7 additions & 6 deletions test/templates/util/ports.ts
Original file line number Diff line number Diff line change
@@ -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);
}
},
Expand Down

0 comments on commit dcb819f

Please sign in to comment.