Skip to content

Commit b5fea3c

Browse files
committed
conat -- more service unit tests
1 parent 623f036 commit b5fea3c

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

src/packages/backend/conat/test/service.test.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@ pnpm test ./service.test.ts
77
*/
88

99
import { callConatService, createConatService } from "@cocalc/conat/service";
10+
import {
11+
createServiceClient,
12+
createServiceHandler,
13+
} from "@cocalc/conat/service/typed";
1014
import { once } from "@cocalc/util/async-utils";
1115
import { before, after } from "@cocalc/backend/conat/test/setup";
1216
import { wait } from "@cocalc/backend/conat/test/util";
17+
import { is_date as isDate } from "@cocalc/util/misc";
18+
import { delay } from "awaiting";
1319

1420
beforeAll(before);
1521

@@ -54,4 +60,62 @@ describe("verify that you can create a service AFTER calling it and things to st
5460
});
5561
});
5662

63+
describe("create and test a more complicated service", () => {
64+
let client, service;
65+
it("defines the service", async () => {
66+
interface Api {
67+
add: (a: number, b: number) => Promise<number>;
68+
concat: (a: Buffer, b: Buffer) => Promise<Buffer>;
69+
now: () => Promise<Date>;
70+
big: (n: number) => Promise<string>;
71+
len: (s: string) => Promise<number>;
72+
}
73+
74+
const name = "my-service";
75+
service = await createServiceHandler<Api>({
76+
service: name,
77+
subject: name,
78+
description: "My Service",
79+
impl: {
80+
// put any functions here that take/return MsgPack'able values
81+
add: async (a, b) => a + b,
82+
concat: async (a, b) => Buffer.concat([a, b]),
83+
now: async () => {
84+
await delay(5);
85+
return new Date();
86+
},
87+
big: async (n: number) => "x".repeat(n),
88+
len: async (s: string) => s.length,
89+
},
90+
});
91+
92+
client = createServiceClient<Api>({
93+
service: name,
94+
subject: name,
95+
});
96+
});
97+
98+
it("tests the service", async () => {
99+
// these calls are all type checked using typescript
100+
expect(await client.add(2, 3)).toBe(5);
101+
102+
const a = Buffer.from("hello");
103+
const b = Buffer.from(" conat");
104+
expect(await client.concat(a, b)).toEqual(Buffer.concat([a, b]));
105+
106+
const d = await client.now();
107+
expect(isDate(d)).toBe(true);
108+
expect(Math.abs(d.valueOf() - Date.now())).toBeLessThan(100);
109+
110+
const n = 10 * 1e6;
111+
expect((await client.big(n)).length).toBe(n);
112+
113+
expect(await client.len("x".repeat(n))).toBe(n);
114+
});
115+
116+
it("cleans up", () => {
117+
service.close();
118+
});
119+
});
120+
57121
afterAll(after);

0 commit comments

Comments
 (0)