forked from kraenhansen/mocha-remote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisconnecting.test.ts
112 lines (93 loc) · 3.76 KB
/
disconnecting.test.ts
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import cp from "child_process";
import { expect } from "chai";
import { resolve } from "path";
import { Server } from "mocha-remote-server";
const TEST_CLIENT_PATH = resolve(__dirname, "../fixtures/client.ts");
/**
* @returns a promise of a child process for a client resolving when the client connects to the server.
*/
function startClient(server: Server, timeout: number) {
const clientProcess = cp.spawn(
process.execPath,
["--import", "tsx", TEST_CLIENT_PATH, timeout.toString()],
{ stdio: "inherit", env: { ...process.env, FORCE_COLOR: "false" }, timeout: 5_000 },
);
return new Promise<cp.ChildProcess>((resolve) => {
server.once("connection", () => resolve(clientProcess));
});
}
/**
* Stop a client previously started with {@link startClient}.
* @returns a promise of the client process exiting.
*/
function stopClient(clientProcess: cp.ChildProcess) {
const result = new Promise((resolve) => clientProcess.once("exit", resolve));
clientProcess.kill();
return result;
}
describe("disconnecting a client", () => {
it("should be able to start again, when the server is stopped during a run", async function() {
this.timeout(10000);
// Create and start the server
const server = new Server({
port: 8090,
reporter: "base",
autoRun: false,
});
await server.start();
const running = new Promise<unknown>(resolve => server.once("running", resolve));
// Start a client waiting longer then the test timeout
const childClientProcess = await startClient(server, this.timeout());
// Starting a run, which will be stopped before it completes naturally
let completed = false;
server.run(() => {
completed = true;
});
// Wait for the tests to start running
await running;
// Stop and restart the server
await server.stop();
await server.start();
// Finally stop the server and client
await server.stop();
await stopClient(childClientProcess);
// Stopping the server will call the callback passed to run
expect(completed).to.equal(false);
});
it("should be able to start again, on client disconnection while running", async function() {
this.timeout(10000);
// Create and start the server
const server = new Server({
port: 8090,
reporter: "base",
autoRun: false,
});
await server.start();
{
// Start a client waiting longer then the test timeout
const childClientProcess = await startClient(server, this.timeout() * 2);
// Abort the run by disconnecting the client
let completed = false;
server.run(() => {
completed = true
});
// Wait for the server to start running
await new Promise(resolve => server.once("running", resolve));
// Disconnect the client while running
await stopClient(childClientProcess);
// Expect no completion
expect(completed).to.equal(false);
}
{
// Connect with a new client
// Start a client completing fast
const childClientProcess = await startClient(server, 0);
// Run the tests again to completion
const result = await new Promise<number>((resolve) => server.run(resolve));
expect(result).to.equal(0);
// Stop the server and the client
await stopClient(childClientProcess);
}
await server.stop();
});
});