Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/backend/conat/test/persist/persist-client-restarts.test.ts
1712 views
1
/*
2
Tests of persist client.
3
4
pnpm test ./persist-client-restarts.test.ts
5
6
*/
7
8
import {
9
before,
10
after,
11
connect,
12
restartServer,
13
restartPersistServer,
14
wait,
15
} from "@cocalc/backend/conat/test/setup";
16
import { stream } from "@cocalc/conat/persist/client";
17
import { messageData } from "@cocalc/conat/core/client";
18
19
beforeAll(before);
20
21
jest.setTimeout(15000);
22
describe("restarting the network and/or persist server, but with no delay afterwards", () => {
23
let client, s1;
24
25
it("creates a client, stream and test data", async () => {
26
client = connect();
27
s1 = stream({
28
client,
29
user: { hub_id: "x" },
30
storage: { path: "hub/network" },
31
});
32
await s1.set({
33
key: "test",
34
messageData: messageData("data"),
35
});
36
const mesg = await s1.get({ key: "test" });
37
expect(mesg.data).toBe("data");
38
});
39
40
it("restart conat networking", async () => {
41
await restartServer();
42
});
43
44
it("it start working again after restart of socketio server only, though we expect some errors", async () => {
45
try {
46
await s1.get({ key: "test", timeout: 500 });
47
} catch {}
48
await wait({
49
until: async () => {
50
try {
51
await s1.get({ key: "test", timeout: 500 });
52
return true;
53
} catch {
54
return false;
55
}
56
},
57
});
58
const mesg = await s1.get({ key: "test" });
59
expect(mesg.data).toBe("data");
60
});
61
62
it("restarts just persist server", () => {
63
restartPersistServer();
64
});
65
66
it("it starts working again after restart after persist server only, though we expect some errors", async () => {
67
await wait({
68
until: async () => {
69
try {
70
await s1.set({
71
key: "test-5",
72
messageData: messageData("data", { headers: { foo: "bar" } }),
73
timeout: 500,
74
});
75
return true;
76
} catch (err) {
77
return false;
78
}
79
},
80
});
81
const mesg = await s1.get({ key: "test-5" });
82
expect(mesg.data).toBe("data");
83
});
84
85
it("restarts BOTH the socketio server and the persist server", () => {
86
restartServer();
87
restartPersistServer();
88
});
89
90
it("it starts working again after restart of BOTH servers, though we expect some errors", async () => {
91
await wait({
92
timeout: 15000,
93
until: async () => {
94
try {
95
await s1.set({
96
key: "test-10",
97
messageData: messageData("data", { headers: { foo: "bar" } }),
98
timeout: 500,
99
});
100
return true;
101
} catch (err) {
102
return false;
103
}
104
},
105
});
106
const mesg = await s1.get({ key: "test-10" });
107
expect(mesg.data).toBe("data");
108
});
109
});
110
111
afterAll(after);
112
113