Skip to content

Commit

Permalink
Merge pull request #1 from joheredi/simple-test
Browse files Browse the repository at this point in the history
Test against autorest-testserver
  • Loading branch information
joheredi authored Nov 15, 2019
2 parents b2ec3d7 + 845daf4 commit 4e721e7
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 1 deletion.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"scripts": {
"build": "tsc -p .",
"start": "node ./dist/src/main.js",
"test": "ts-node test/utils/start-server.ts & mocha -r ts-node/register './test/**/*spec.ts' & stop-autorest-testserver",
"debug": "node --inspect-brk ./dist/src/main.js"
},
"dependencies": {
Expand All @@ -19,6 +20,7 @@
"source-map-support": "^0.5.16"
},
"devDependencies": {
"@autorest/test-server": "^3.0.26",
"@types/js-yaml": "3.12.1",
"@types/mocha": "5.2.7",
"@types/node": "12.7.12",
Expand All @@ -28,6 +30,8 @@
"@types/xmlbuilder": "0.0.34",
"@typescript-eslint/eslint-plugin": "~2.0.0",
"@typescript-eslint/parser": "~2.0.0",
"eslint": "~6.2.2"
"eslint": "~6.2.2",
"node-cmd": "^3.0.0",
"wait-port": "^0.2.6"
}
}
17 changes: 17 additions & 0 deletions test/testClient.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ping } from "./testClient";
import { equal } from "assert";

/**
* Basic test suite that verifies that the Test Server is up and running
*/
describe("TestServer", () => {
it("should get true as response", async () => {
const result = await ping(true);
equal(result, true);
});

it("should get false as response", async () => {
const result = await ping(false);
equal(result, false);
});
});
31 changes: 31 additions & 0 deletions test/testClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { get } from "http";

export const ping = (expect: boolean): Promise<boolean> => {
const options = {
host: "localhost",
port: 3000,
path: `bool/${expect}`,
json: true,
headers: {
"content-type": "application/json",
accept: "application/json"
}
};
return new Promise((resolve, reject) => {
get(options, response => {
let data = "";
response.on("data", (chunk: Buffer) => {
data += chunk;
});

response.on("end", () => {
resolve(/true/i.test(data));
});

response.on("error", (error: Error) => {
console.error(`Error while fetching ${error}`);
reject(error);
});
});
});
};
91 changes: 91 additions & 0 deletions test/utils/start-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const cmd = require("node-cmd");
const waitPort = require("wait-port");
type OutputType = "silent" | "dots";

/**
* Function that starts the test server with retries
*/
const startTestServer = () => retry(startServer, 3);

/**
* Function that starts the tests server and verifies it is ready to receive requests
*/
const startServer = async (): Promise<boolean> => {
const isStarted = await signalServer(true);

if (!isStarted) {
return Promise.reject(new Error("Could not start server"));
}

const output: OutputType = "silent";
const timeout = 5000;
const params = {
host: "localhost",
port: 3000,
output,
timeout
};

const isReady = await waitPort(params);

if (!isReady) {
return Promise.reject(
new Error(
`Couldn't get server ready for testing, not responding on port ${params.port}`
)
);
}

return Promise.resolve(true);
};

/**
* Signals the test server to start or stop
* @param isStart whether the signal is to start
*/
const signalServer = (isStart: boolean): Promise<boolean> => {
return new Promise((resolve, reject) => {
const command = isStart ? "start" : "stop";
cmd.get(`${command}-autorest-testserver`, (err: Error) => {
if (err) {
const error = `Error starting server: ${err}`;
console.error(error);
reject(new Error(error));
} else {
resolve(true);
}
});
});
};

/**
* Function to pause the retries and allow for exponential backoff
* @param timeInMs time to pause
*/
const pause = (timeInMs: number) =>
new Promise(resolve => setTimeout(resolve, timeInMs));

/**
* Helper for retrying a given function with exponential backoff
* @param fn Function to retry
* @param maxTries Max number of attempts
* @param delay Time in ms to wait before retrying
*/
const retry = (
fn: () => Promise<boolean>,
maxTries: number,
delay = 1000
): Promise<Boolean> =>
fn().catch((error: Error) => {
return maxTries > 1
? pause(delay).then(() => retry(fn, maxTries - 1, delay * 2))
: Promise.reject(error);
});

startTestServer()
.then(result => {
console.log(`Server ready: ${result}`);
})
.catch(error => {
console.error(`Couldn't start server ${error}`);
});

0 comments on commit 4e721e7

Please sign in to comment.