@niwa-labs/client 0.0.0-stage → 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,19 @@
1
+ // The wait between reconnect attempts leaves nothing behind.
2
+
3
+ import { expect, test } from "bun:test";
4
+
5
+ import { RetryPause } from "../src/pause.ts";
6
+
7
+ test("a cancel ends the wait at once", async () => {
8
+ const pause = new RetryPause();
9
+ const waiting = pause.wait(60_000);
10
+ pause.cancel();
11
+ expect(await Promise.race([waiting.then(() => "ended"), Bun.sleep(1_000).then(() => "stuck")])).toBe("ended");
12
+ });
13
+
14
+ test("a wait that ran out holds nothing: a later cancel is a no-op", async () => {
15
+ const pause = new RetryPause();
16
+ await pause.wait(5);
17
+ pause.cancel();
18
+ await pause.wait(5);
19
+ });
@@ -0,0 +1,54 @@
1
+ // A request whose bytes the socket refused is not left behind to be sent with the next one.
2
+
3
+ import { afterEach, expect, spyOn, test } from "bun:test";
4
+
5
+ import { connect } from "../src/index.ts";
6
+
7
+ interface StubSocket {
8
+ write(data: Uint8Array): number;
9
+ end(): void;
10
+ }
11
+
12
+ const restores: Array<() => void> = [];
13
+ afterEach(() => {
14
+ for (const restore of restores.splice(0)) restore();
15
+ });
16
+
17
+ /** Bun.connect that hands out sockets whose writes fail while `breaking` is set. */
18
+ function stubbedSockets() {
19
+ const written: string[] = [];
20
+ const state = { breaking: false, opened: 0 };
21
+ const spy = spyOn(Bun, "connect").mockImplementation((async () => {
22
+ state.opened += 1;
23
+ const socket: StubSocket = {
24
+ write(data) {
25
+ if (state.breaking) throw new Error("EPIPE");
26
+ written.push(new TextDecoder().decode(data));
27
+ return data.length;
28
+ },
29
+ end() {},
30
+ };
31
+ return socket;
32
+ }) as unknown as typeof Bun.connect);
33
+ restores.push(() => spy.mockRestore());
34
+ return { written, state };
35
+ }
36
+
37
+ test("a call whose write failed does not start with the next call on the same client", async () => {
38
+ const { written, state } = stubbedSockets();
39
+ const client = await connect({ home: "/nowhere" });
40
+ try {
41
+ state.breaking = true;
42
+ await expect(client.describe()).rejects.toThrow();
43
+ state.breaking = false;
44
+ const next = client.describe().catch(() => undefined);
45
+ await Bun.sleep(50);
46
+ // The next call opened a connection of its own and sent its request alone.
47
+ expect(state.opened).toBe(2);
48
+ expect(written.join("").split('"op":"describe"')).toHaveLength(2);
49
+ await client.close();
50
+ await next;
51
+ } finally {
52
+ await client.close();
53
+ }
54
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "include": [
4
+ "src"
5
+ ]
6
+ }