Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/backend/conat/test/sync/akv.test.ts
1451 views
1
/*
2
Testing basic ops with dkv
3
4
DEVELOPMENT:
5
6
pnpm test ./akv.test.ts
7
8
*/
9
10
import { dkv as createDkv, akv as createAkv } from "@cocalc/backend/conat/sync";
11
import { wait } from "@cocalc/backend/conat/test/util";
12
import { before, after, connect } from "@cocalc/backend/conat/test/setup";
13
14
beforeAll(before);
15
16
describe("test basics with an akv", () => {
17
let kv, client;
18
const name = `test-${Math.random()}`;
19
20
it("creates the akv, then set and read a value", async () => {
21
client = connect();
22
kv = createAkv({ name, client });
23
await kv.set("x", 10);
24
expect(await kv.get("x")).toBe(10);
25
});
26
27
it("reads a value that isn't there and gets undefined", async () => {
28
expect(await kv.get("y")).toBe(undefined);
29
});
30
31
it("writes and reads null and gets null", async () => {
32
await kv.set("y", null);
33
expect(await kv.get("y")).toBe(null);
34
});
35
36
it("gets all keys", async () => {
37
expect(await kv.keys()).toEqual(["x", "y"]);
38
});
39
40
it("check that deleting a value works", async () => {
41
await kv.delete("x");
42
expect(await kv.get("x")).toBe(undefined);
43
});
44
45
it("cleans up", async () => {
46
const k = await createDkv({ name, client });
47
k.clear();
48
await k.close();
49
});
50
});
51
52
describe("test interop with a dkv", () => {
53
let akv, dkv, client;
54
const name = `test-${Math.random()}`;
55
56
it("creates the akv and dkv", async () => {
57
client = connect();
58
akv = createAkv({ name, client });
59
dkv = await createDkv({ name, client });
60
});
61
62
it("sets value in the dkv and reads it using the akv", async () => {
63
dkv.set("x", 25);
64
await dkv.save();
65
expect(await akv.get("x")).toBe(25);
66
});
67
68
it("sets value in the akv and reads it using the dkv", async () => {
69
await akv.set("z", 389);
70
await wait({ until: () => dkv.has("z") });
71
expect(await dkv.get("z")).toBe(389);
72
});
73
74
it("check headers work", async () => {
75
dkv.set("h", 10, { headers: { foo: "bar" } });
76
await dkv.save();
77
expect(await akv.headers("h")).toEqual(
78
expect.objectContaining({ foo: "bar" }),
79
);
80
81
await akv.set("h2", 20, { headers: { foo: "baz" } });
82
expect(await akv.headers("h2")).toEqual(
83
expect.objectContaining({ foo: "baz" }),
84
);
85
await wait({ until: () => dkv.has("h2") });
86
expect(await dkv.headers("h2")).toEqual(
87
expect.objectContaining({ foo: "baz" }),
88
);
89
});
90
91
it("check sqlite query fails", async () => {
92
await expect(async () => {
93
await akv.sqlite("SELECT count(*) AS n FROM messages");
94
}).rejects.toThrowError("sqlite command not currently supported");
95
});
96
97
// it("check sqlite query works", async () => {
98
// const v = await akv.sqlite("SELECT count(*) AS n FROM messages");
99
// expect(v[0].n).toBe((await akv.keys()).length);
100
// });
101
102
it("cleans up", async () => {
103
dkv.clear();
104
await dkv.close();
105
});
106
});
107
108
describe("testing writing and reading chunked data", () => {
109
let maxPayload = 0;
110
let client;
111
112
it("sanity check on the max payload", async () => {
113
client = connect();
114
await wait({ until: () => client.info != null });
115
maxPayload = client.info?.max_payload ?? 0;
116
expect(maxPayload).toBeGreaterThan(500000);
117
});
118
119
let kv;
120
const name = `test-${Math.random()}`;
121
it("creates akv, then set and read a large value", async () => {
122
kv = createAkv({ name, client });
123
const val = "z".repeat(maxPayload * 1.5) + "cocalc";
124
await kv.set("x", val);
125
expect(await kv.get("x")).toBe(val);
126
});
127
128
it("cleans up", async () => {
129
const k = await createDkv({ name, client });
130
k.clear();
131
await k.close();
132
});
133
});
134
135
afterAll(after);
136
137