-
Notifications
You must be signed in to change notification settings - Fork 0
/
MockServer.ts
43 lines (34 loc) · 1.08 KB
/
MockServer.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
import { MockPeer } from './MockPeer';
import { MockWebSocket } from './MockWebSocket';
export class MockServer extends MockPeer {
get client(): MockWebSocket {
return this.peerWebSocket;
}
public async use<T>(
clientPromise: Promise<T>,
serverImplementation?: () => Promise<void>,
): Promise<T> {
const [clientResult, serverResult] = await Promise.allSettled([
clientPromise,
new Promise(async (resolve) => {
this.peerWebSocket.emit('open');
// Give the client enough time to connect
await new Promise(setImmediate);
await serverImplementation?.();
// Allow more time for the client to process the latest actions from the server
await new Promise(setImmediate);
if (this.client.readyState === MockWebSocket.OPEN) {
this.close();
}
resolve(undefined);
}),
]);
if (clientResult.status === 'rejected') {
throw clientResult.reason;
}
if (serverResult.status === 'rejected') {
throw serverResult.reason;
}
return clientResult.value;
}
}