|
1 | 1 | import { expect } from "chai";
|
2 | 2 | import { WebSocket } from "ws";
|
3 | 3 | import flatted from "flatted";
|
| 4 | +import { ChildProcess, fork } from "child_process"; |
| 5 | +import { resolve } from "path"; |
4 | 6 |
|
5 | 7 | import { Server } from "./Server";
|
6 | 8 |
|
@@ -91,4 +93,85 @@ describe("Mocha Remote Server", () => {
|
91 | 93 | }
|
92 | 94 | }
|
93 | 95 | });
|
| 96 | + |
| 97 | + const startRemoteClient = (script: string, server: Server) => { |
| 98 | + return new Promise<ChildProcess>((resolve) => { |
| 99 | + const clientProcess = fork(script, { stdio: "inherit" }); |
| 100 | + const onConnection = () => { |
| 101 | + cleanup(); |
| 102 | + resolve(clientProcess); |
| 103 | + }; |
| 104 | + const cleanup = () => { |
| 105 | + server.off("connection", onConnection); |
| 106 | + } |
| 107 | + server.on("connection", onConnection); |
| 108 | + }); |
| 109 | + }; |
| 110 | + |
| 111 | + const stopRemoteClient = (childClientProcess: ChildProcess) => { |
| 112 | + return new Promise<void>((resolve, reject) => { |
| 113 | + childClientProcess.once("exit", () => { |
| 114 | + resolve(); |
| 115 | + }); |
| 116 | + childClientProcess.kill(); |
| 117 | + }); |
| 118 | + }; |
| 119 | + |
| 120 | + const runTests = (server: Server) => { |
| 121 | + return new Promise<number>((resolve, reject) => { |
| 122 | + server.run((failures) => { |
| 123 | + resolve(failures); |
| 124 | + }); |
| 125 | + }); |
| 126 | + }; |
| 127 | + |
| 128 | + it("should be able to start again, when the server is stopped during a run", async () => { |
| 129 | + // Create and start the server |
| 130 | + const server = new Server({ |
| 131 | + port: 8090, |
| 132 | + reporter: "base", |
| 133 | + autoRun: false, |
| 134 | + }); |
| 135 | + |
| 136 | + await server.start(); |
| 137 | + |
| 138 | + const clientThatHangsScriptPath = resolve(__dirname, "../fixtures/clientVeryLongRun.js"); |
| 139 | + const childClientProcess = await startRemoteClient(clientThatHangsScriptPath, server); |
| 140 | + |
| 141 | + server.run(() => { |
| 142 | + // |
| 143 | + }); |
| 144 | + |
| 145 | + await server.stop(); |
| 146 | + await server.start(); |
| 147 | + |
| 148 | + await server.stop(); |
| 149 | + await stopRemoteClient(childClientProcess); |
| 150 | + }); |
| 151 | + |
| 152 | + it("should be able to start again, on client disconnection while running", async () => { |
| 153 | + // Create and start the server |
| 154 | + const server = new Server({ |
| 155 | + port: 8090, |
| 156 | + reporter: "base", |
| 157 | + autoRun: false, |
| 158 | + }); |
| 159 | + |
| 160 | + await server.start(); |
| 161 | + |
| 162 | + const clientThatHangsScriptPath = resolve(__dirname, "../fixtures/clientVeryLongRun.js"); |
| 163 | + const childClientProcess = await startRemoteClient(clientThatHangsScriptPath, server); |
| 164 | + |
| 165 | + const results = await Promise.all([runTests(server), stopRemoteClient(childClientProcess)]); |
| 166 | + const failures = results[0]; |
| 167 | + expect(failures).to.equal(0); |
| 168 | + |
| 169 | + const clientScriptPath = resolve(__dirname, "../fixtures/client.js"); |
| 170 | + const remoteClientProcess = await startRemoteClient(clientScriptPath, server); |
| 171 | + |
| 172 | + const result = await runTests(server); |
| 173 | + expect(result).to.equal(1); |
| 174 | + await server.stop(); |
| 175 | + await stopRemoteClient(remoteClientProcess); |
| 176 | + }); |
94 | 177 | });
|
0 commit comments