forked from Azure/azure-sdk-for-js
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from joheredi/simple-test
Test against autorest-testserver
- Loading branch information
Showing
4 changed files
with
144 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
}); |