-
-
Notifications
You must be signed in to change notification settings - Fork 209
Expand file tree
/
Copy pathmaxAttempts.ts
More file actions
74 lines (63 loc) · 1.88 KB
/
Copy pathmaxAttempts.ts
File metadata and controls
74 lines (63 loc) · 1.88 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
import { HocuspocusProviderWebsocket } from "@hocuspocus/provider";
import test from "ava";
import { sleep } from "../utils/index.ts";
const exhaustedRetryOptions = {
url: "ws://127.0.0.1:1",
maxAttempts: 1,
delay: 1,
minDelay: 1,
initialDelay: 0,
jitter: false,
};
test("does not produce an unhandled rejection when maxAttempts are exhausted", async (t) => {
const unhandled: unknown[] = [];
const onUnhandledRejection = (reason: unknown) => {
unhandled.push(reason);
};
process.on("unhandledRejection", onUnhandledRejection);
t.teardown(() => {
process.off("unhandledRejection", onUnhandledRejection);
});
const ws = new HocuspocusProviderWebsocket(exhaustedRetryOptions);
t.teardown(() => ws.destroy());
await sleep(300);
await new Promise((resolve) => setImmediate(resolve));
await new Promise((resolve) => setImmediate(resolve));
t.is(unhandled.length, 0);
});
test("onMaxAttemptsFailed is executed when maxAttempts are exhausted", async (t) => {
await new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(new Error("onMaxAttemptsFailed was not called"));
}, 2000);
const ws = new HocuspocusProviderWebsocket({
...exhaustedRetryOptions,
onMaxAttemptsFailed({ error }) {
clearTimeout(timeout);
t.truthy(error);
ws.destroy();
resolve("done");
},
});
t.teardown(() => ws.destroy());
});
});
test("on('maxAttemptsFailed') is executed when maxAttempts are exhausted", async (t) => {
await new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(new Error("maxAttemptsFailed was not emitted"));
}, 2000);
const ws = new HocuspocusProviderWebsocket({
...exhaustedRetryOptions,
autoConnect: false,
});
t.teardown(() => ws.destroy());
ws.on("maxAttemptsFailed", ({ error }) => {
clearTimeout(timeout);
t.truthy(error);
ws.destroy();
resolve("done");
});
ws.connect();
});
});