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
1451 views
1
/*
2
Tests of persist client.
3
4
pnpm test ./persist-client.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
describe("restarting the network and/or persist server, but with no delay afterwards", () => {
22
let client, s1;
23
24
it("creates a client, stream and test data", async () => {
25
client = connect();
26
s1 = stream({
27
client,
28
user: { hub_id: "x" },
29
storage: { path: "hub/network" },
30
});
31
await s1.set({
32
key: "test",
33
messageData: messageData("data"),
34
});
35
const mesg = await s1.get({ key: "test" });
36
expect(mesg.data).toBe("data");
37
});
38
39
it("restart conat networking", async () => {
40
await restartServer();
41
});
42
43
it("it start working again after restart of socketio server only, though we expect some errors", async () => {
44
try {
45
await s1.get({ key: "test", timeout: 500 });
46
} catch (err) {
47
expect(`${err}`).toMatch(/timeout|subscribers|disconnected/);
48
}
49
await wait({
50
until: async () => {
51
try {
52
await s1.get({ key: "test", timeout: 500 });
53
return true;
54
} catch {
55
return false;
56
}
57
},
58
});
59
const mesg = await s1.get({ key: "test" });
60
expect(mesg.data).toBe("data");
61
});
62
63
it("restarts just persist server", () => {
64
restartPersistServer();
65
});
66
67
it("it starts working again after restart after persist server only, though we expect some errors", async () => {
68
await wait({
69
until: async () => {
70
try {
71
await s1.set({
72
key: "test-5",
73
messageData: messageData("data", { headers: { foo: "bar" } }),
74
timeout: 500,
75
});
76
return true;
77
} catch (err) {
78
return false;
79
}
80
},
81
});
82
const mesg = await s1.get({ key: "test-5" });
83
expect(mesg.data).toBe("data");
84
});
85
86
it("restarts BOTH the socketio server and the persist server", () => {
87
restartServer();
88
restartPersistServer();
89
});
90
91
it("it starts working again after restart of BOTH servers, though we expect some errors", async () => {
92
await wait({
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