Skip to content

Commit 288702d

Browse files
committed
conat: more service unit tests
1 parent 6295b00 commit 288702d

File tree

3 files changed

+46
-21
lines changed

3 files changed

+46
-21
lines changed

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

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import {
1111
createServiceClient,
1212
createServiceHandler,
1313
} from "@cocalc/conat/service/typed";
14-
import { once } from "@cocalc/util/async-utils";
15-
import { before, after, connect } from "@cocalc/backend/conat/test/setup";
14+
import { before, after } from "@cocalc/backend/conat/test/setup";
1615
import { wait } from "@cocalc/backend/conat/test/util";
1716
import { is_date as isDate } from "@cocalc/util/misc";
1817
import { delay } from "awaiting";
@@ -23,14 +22,11 @@ beforeAll(before);
2322

2423
describe("create a service and test it out", () => {
2524
let s;
26-
let subject;
2725
it("creates a service", async () => {
2826
s = createConatService({
2927
service: "echo",
3028
handler: (mesg) => mesg.repeat(2),
3129
});
32-
subject = s.subject;
33-
await once(s, "running");
3430
expect(await callConatService({ service: "echo", mesg: "hello" })).toBe(
3531
"hellohello",
3632
);
@@ -42,16 +38,6 @@ describe("create a service and test it out", () => {
4238
await callConatService({ service: "echo", mesg: "hi", timeout: 250 });
4339
}).rejects.toThrowError("timeout");
4440
});
45-
46-
// [ ] TODO: broken!
47-
it.skip("creates a listener on the same subject and try to call to verify timeout works", async () => {
48-
const client = connect();
49-
const sub = await client.subscribe(subject);
50-
await expect(async () => {
51-
await callConatService({ service: "echo", mesg: "hi", timeout: 250 });
52-
}).rejects.toThrowError("timeout");
53-
sub.close();
54-
});
5541
});
5642

5743
describe("verify that you can create a service AFTER calling it and things to still work fine", () => {
@@ -231,4 +217,42 @@ describe("create a service with specified client, stop and start the server, and
231217
});
232218
});
233219

220+
describe("create a slow service and check that the timeout parameter works", () => {
221+
let s;
222+
it("creates a slow service", async () => {
223+
s = createConatService({
224+
service: "slow",
225+
handler: async (d) => {
226+
await delay(d);
227+
return { delay: d };
228+
},
229+
});
230+
});
231+
232+
it("confirms it works", async () => {
233+
const t0 = Date.now();
234+
const r = await callConatService({
235+
service: s.name,
236+
mesg: 50,
237+
});
238+
expect(r).toEqual({ delay: 50 });
239+
expect(Date.now() - t0).toBeGreaterThan(45);
240+
expect(Date.now() - t0).toBeLessThan(500);
241+
});
242+
243+
it("confirms it throws a timeout error", async () => {
244+
await expect(async () => {
245+
await callConatService({
246+
service: s.name,
247+
mesg: 5000,
248+
timeout: 75,
249+
});
250+
}).rejects.toThrowError("imeout");
251+
});
252+
253+
it("clean up", async () => {
254+
s.close();
255+
});
256+
});
257+
234258
afterAll(after);

src/packages/conat/core/client.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,13 +619,12 @@ export class Client {
619619
code: 503,
620620
});
621621
}
622-
623622
for await (const resp of sub) {
624623
sub.stop();
625624
return resp;
626625
}
627626
sub.stop();
628-
throw Error("timeout");
627+
throw new ConatError("timeout", { code: 408 });
629628
};
630629

631630
async *requestMany(

src/packages/conat/service/service.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export async function callConatService(opts: ServiceCall): Promise<any> {
5959
const data = opts.mesg ?? null;
6060

6161
const doRequest = async () => {
62+
// console.log("doRequest...");
6263
resp = await cn.request(subject, data, {
6364
timeout,
6465
});
@@ -69,16 +70,15 @@ export async function callConatService(opts: ServiceCall): Promise<any> {
6970
return result;
7071
};
7172

72-
// we just try to call the service first
7373
try {
74+
// try to call the service:
7475
return await doRequest();
7576
} catch (err) {
7677
// console.log(`request to '${subject}' failed -- ${err}`);
7778
// it failed.
7879
if (opts.noRetry) {
7980
throw err;
8081
}
81-
// it's a nats problem
8282
const p = opts.path ? `${trunc_middle(opts.path, 64)}:` : "";
8383
if (err.code == 503) {
8484
// it's actually just not ready, so
@@ -92,10 +92,12 @@ export async function callConatService(opts: ServiceCall): Promise<any> {
9292
}
9393
throw err;
9494
}
95-
} else if (err.code == "TIMEOUT") {
95+
} else if (err.code == 408) {
9696
throw Error(
97-
`Timeout: service ${p}${opts.service} did not respond for ${Math.round(timeout / 1000)} seconds`,
97+
`Timeout: service '${p}${opts.service}' did not respond for ${Math.round(timeout / 1000)} seconds`,
9898
);
99+
} else {
100+
throw err;
99101
}
100102
}
101103
}

0 commit comments

Comments
 (0)