-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path_smoke_test.js
More file actions
87 lines (73 loc) · 2.08 KB
/
_smoke_test.js
File metadata and controls
87 lines (73 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright Titanium I.T. LLC.
import assert from "util/assert.js";
import * as testHelper from "util/test_helper.js";
import { pathToFile } from "util/module_paths.js";
// dependency_analysis: ./serve.js
const STARTUP_TIMEOUT_IN_MS = 2000;
const STARTUP_FAILED_REGEX = /"emergency"/;
const SERVER_STARTED_REGEX = /"server started".*?\n.*?"server started"/; // look for both servers to start
const WWW_PORT = 5001;
const ROT13_PORT = 5002;
describe("Smoke test", function() {
this.timeout(STARTUP_TIMEOUT_IN_MS + 1000);
it("performs end-to-end ROT-13 transform", async () => {
await runServersAsync(async () => {
const { status, body } = await testHelper.requestAsync({
port: WWW_PORT,
url: "/",
method: "POST",
body: [ "text=hello" ],
});
assert.equal(status, 200, "status");
assert.includes(body, 'value="uryyb"', "body");
});
});
});
async function runServersAsync(fnAsync) {
const killFnAsync = await forkAsync();
try {
await fnAsync();
}
finally {
await killFnAsync();
}
}
async function forkAsync() {
return await new Promise((resolve, reject) => {
let stdout = "";
const process = testHelper.forkModule(
pathToFile(import.meta.url, "serve.js"),
{ args: [ WWW_PORT.toString(), ROT13_PORT.toString() ] },
);
const timeoutHandle = setTimeout(() => {
return fail(new Error("Startup timed out"));
}, STARTUP_TIMEOUT_IN_MS);
process.stdout.on("data", (chunk) => {
stdout += chunk;
if (STARTUP_FAILED_REGEX.test(stdout)) {
return fail(new Error("Startup logged emergency"));
}
if (SERVER_STARTED_REGEX.test(stdout)) {
return succeed();
}
});
process.stderr.on("data", (chunk) => {
return fail(new Error(`Unexpected stderr: ${chunk}`));
});
function succeed() {
clearTimeout(timeoutHandle);
const killFnAsync = () => new Promise((innerResolve) => kill(innerResolve));
return resolve(killFnAsync);
}
function fail(err) {
kill(() => {
err.message += `\nLogs:\n${stdout}`;
return reject(err);
});
}
function kill(thenFn) {
process.kill();
process.on("exit", () => thenFn());
}
});
}