Skip to content

Commit 960368c

Browse files
authored
Merge pull request #1729 from dynst/socket-test
Modernize child_process import in reconnecting socket test
2 parents 1e7014c + 0230fec commit 960368c

File tree

1 file changed

+43
-35
lines changed

1 file changed

+43
-35
lines changed

packages/socket/src/reconnectingsocket.spec.ts

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,16 @@
1-
import assert from "assert";
2-
31
import { ReconnectingSocket } from "./reconnectingsocket";
42

53
/** @see https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback */
6-
type Exec = (command: string, callback: (error: null | (Error & { readonly code: number })) => void) => void;
7-
8-
let exec: Exec | undefined;
9-
let childProcessAvailable: boolean;
10-
11-
try {
12-
// eslint-disable-next-line @typescript-eslint/no-var-requires
13-
exec = require("child_process").exec;
14-
assert.strict(typeof exec === "function");
15-
childProcessAvailable = true;
16-
} catch {
17-
childProcessAvailable = false;
18-
}
4+
type Exec = (command: string, callback: (error: null | (Error & { readonly code?: number })) => void) => void;
5+
6+
const getExec = async (): Promise<Exec | undefined> => (await import("child_process")).exec;
197

208
function pendingWithoutSocketServer(): void {
219
if (!process.env.SOCKETSERVER_ENABLED) {
2210
pending("Set SOCKETSERVER_ENABLED to enable socket tests");
2311
}
2412
}
2513

26-
function pendingWithoutChildProcess(): void {
27-
if (!childProcessAvailable) {
28-
pending("Run test in an environment which supports child processes to enable socket tests");
29-
}
30-
}
31-
3214
describe("ReconnectingSocket", () => {
3315
const socketServerUrl = "ws://localhost:4444/websocket";
3416

@@ -97,13 +79,24 @@ describe("ReconnectingSocket", () => {
9779
const startServerCmd = `${dirPath}/start.sh`;
9880
const stopServerCmd = `${dirPath}/stop.sh`;
9981

100-
it("automatically reconnects if no connection can be established at init", (done) => {
101-
pendingWithoutChildProcess();
82+
it("automatically reconnects if no connection can be established at init", async () => {
83+
let pass!: () => void, fail!: (reason?: any) => void;
84+
const ret = new Promise<void>((resolve, reject) => {
85+
pass = resolve;
86+
fail = reject;
87+
});
88+
89+
const exec = await getExec();
90+
if (exec === undefined) {
91+
pending("Run test in an environment which supports child processes to enable socket tests");
92+
return;
93+
}
94+
10295
pendingWithoutSocketServer();
10396

104-
exec!(stopServerCmd, (stopError) => {
97+
exec(stopServerCmd, (stopError) => {
10598
if (stopError && stopError.code !== codePkillNoProcessesMatched) {
106-
done.fail(stopError);
99+
fail(stopError);
107100
}
108101

109102
const socket = new ReconnectingSocket(socketServerUrl);
@@ -119,7 +112,7 @@ describe("ReconnectingSocket", () => {
119112
complete: () => {
120113
// Make sure we don't get a completion unexpectedly
121114
expect(eventsSeen).toEqual(requests.length);
122-
done();
115+
pass();
123116
},
124117
});
125118

@@ -128,18 +121,31 @@ describe("ReconnectingSocket", () => {
128121

129122
setTimeout(
130123
() =>
131-
exec!(startServerCmd, (startError) => {
124+
exec(startServerCmd, (startError) => {
132125
if (startError) {
133-
done.fail(startError);
126+
fail(startError);
134127
}
135128
}),
136129
2000,
137130
);
138131
});
132+
133+
return ret;
139134
});
140135

141-
it("automatically reconnects if the connection is broken off", (done) => {
142-
pendingWithoutChildProcess();
136+
it("automatically reconnects if the connection is broken off", async () => {
137+
let pass!: () => void, fail!: (reason?: any) => void;
138+
const ret = new Promise<void>((resolve, reject) => {
139+
pass = resolve;
140+
fail = reject;
141+
});
142+
143+
const exec = await getExec();
144+
if (exec === undefined) {
145+
pending("Run test in an environment which supports child processes to enable socket tests");
146+
return;
147+
}
148+
143149
pendingWithoutSocketServer();
144150

145151
const socket = new ReconnectingSocket(socketServerUrl);
@@ -155,7 +161,7 @@ describe("ReconnectingSocket", () => {
155161
complete: () => {
156162
// Make sure we don't get a completion unexpectedly
157163
expect(eventsSeen).toEqual(requests.length);
158-
done();
164+
pass();
159165
},
160166
});
161167

@@ -164,9 +170,9 @@ describe("ReconnectingSocket", () => {
164170

165171
setTimeout(
166172
() =>
167-
exec!(stopServerCmd, (stopError) => {
173+
exec(stopServerCmd, (stopError) => {
168174
if (stopError && stopError.code !== codePkillNoProcessesMatched) {
169-
done.fail(stopError);
175+
fail(stopError);
170176
}
171177

172178
// TODO: This timeout is here to avoid an edge case where if a request
@@ -179,9 +185,9 @@ describe("ReconnectingSocket", () => {
179185

180186
setTimeout(
181187
() =>
182-
exec!(startServerCmd, (startError) => {
188+
exec(startServerCmd, (startError) => {
183189
if (startError) {
184-
done.fail(startError);
190+
fail(startError);
185191
}
186192
}),
187193
2000,
@@ -190,6 +196,8 @@ describe("ReconnectingSocket", () => {
190196
}),
191197
1000,
192198
);
199+
200+
return ret;
193201
});
194202
});
195203
});

0 commit comments

Comments
 (0)