@niwa-labs/host 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.
- package/LICENSE +21 -0
- package/README.md +2 -2
- package/package.json +16 -4
- package/src/access-actions.ts +193 -0
- package/src/access-control.ts +333 -0
- package/src/call-operations.ts +176 -0
- package/src/cockpit-catalog.ts +99 -0
- package/src/describe.ts +55 -0
- package/src/doors.ts +272 -0
- package/src/host-types.ts +43 -0
- package/src/host.ts +3346 -0
- package/src/index.ts +10 -0
- package/src/plugin-install.ts +249 -0
- package/src/state.ts +261 -0
- package/src/stores.ts +131 -0
- package/test/abandon.test.ts +58 -0
- package/test/action-refusal-restart.test.ts +46 -0
- package/test/action-step.test.ts +363 -0
- package/test/action-wait-restart.test.ts +96 -0
- package/test/builtin-actions.test.ts +21 -0
- package/test/cancel-steps.test.ts +150 -0
- package/test/catch-up.test.ts +378 -0
- package/test/causes.test.ts +138 -0
- package/test/client.test.ts +190 -0
- package/test/cockpit-catalog.test.ts +118 -0
- package/test/crash-between-steps.test.ts +88 -0
- package/test/declared-values.test.ts +175 -0
- package/test/deep-nested-pause.test.ts +147 -0
- package/test/fixtures/action-step-host.ts +166 -0
- package/test/fixtures/action-wait-restart-host.ts +62 -0
- package/test/fixtures/catch-up-host.ts +91 -0
- package/test/fixtures/causes-host.ts +78 -0
- package/test/fixtures/client-host.ts +52 -0
- package/test/fixtures/crash-between-steps-host.ts +71 -0
- package/test/fixtures/declared-values-host.ts +110 -0
- package/test/fixtures/intercept-order-host.ts +112 -0
- package/test/fixtures/intercept-step-host.ts +106 -0
- package/test/fixtures/live-frames-host.ts +81 -0
- package/test/fixtures/long-wait-host.ts +78 -0
- package/test/fixtures/nested-wait-host.ts +120 -0
- package/test/fixtures/no-repeat-crash-host.ts +79 -0
- package/test/fixtures/plugin-replace-host.ts +176 -0
- package/test/fixtures/record-refusal-crash.ts +50 -0
- package/test/fixtures/recovery-contracts-host.ts +79 -0
- package/test/fixtures/repeated-write-host.ts +39 -0
- package/test/fixtures/replay-mismatch-host.ts +66 -0
- package/test/fixtures/single-writer-host.ts +113 -0
- package/test/fixtures/state-host.ts +118 -0
- package/test/fixtures/wait-restart-host.ts +82 -0
- package/test/guards.test.ts +448 -0
- package/test/handler-emit.test.ts +116 -0
- package/test/host.test.ts +114 -0
- package/test/intercept-adjust.test.ts +318 -0
- package/test/intercept-order.test.ts +276 -0
- package/test/intercept-step.test.ts +144 -0
- package/test/large-outcome.test.ts +60 -0
- package/test/live-frames.test.ts +150 -0
- package/test/live-ready.test.ts +61 -0
- package/test/long-wait.test.ts +168 -0
- package/test/nested-outcome.test.ts +75 -0
- package/test/nested-wait.test.ts +111 -0
- package/test/no-repeat-crash.test.ts +132 -0
- package/test/offer-replace.test.ts +39 -0
- package/test/plugin-async-setup.test.ts +22 -0
- package/test/plugin-copies.test.ts +87 -0
- package/test/plugin-emit.test.ts +43 -0
- package/test/plugin-install-failure.test.ts +121 -0
- package/test/plugin-install-hash.test.ts +57 -0
- package/test/plugin-needs.test.ts +80 -0
- package/test/plugin-replace.test.ts +225 -0
- package/test/plugin-uninstall.test.ts +70 -0
- package/test/plugin-version-copies.test.ts +53 -0
- package/test/plugin-version.test.ts +180 -0
- package/test/read-filter.test.ts +136 -0
- package/test/read-query.test.ts +92 -0
- package/test/record-admission.test.ts +294 -0
- package/test/recovery-contracts.test.ts +107 -0
- package/test/repeated-write.test.ts +119 -0
- package/test/replay-mismatch.test.ts +79 -0
- package/test/replay.test.ts +135 -0
- package/test/single-writer.test.ts +267 -0
- package/test/state.test.ts +246 -0
- package/test/step-by-name.test.ts +122 -0
- package/test/stores.test.ts +102 -0
- package/test/stream.test.ts +154 -0
- package/test/subscriber-stream.test.ts +71 -0
- package/test/subscribers.test.ts +101 -0
- package/test/subscriptions.test.ts +632 -0
- package/test/trace-call.test.ts +124 -0
- package/test/trigger-filter.test.ts +42 -0
- package/test/use-many.test.ts +151 -0
- package/test/wait-query.test.ts +117 -0
- package/test/wait-restart.test.ts +187 -0
- package/test/wire-read-access.test.ts +85 -0
- package/tsconfig.json +6 -0
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// The chain of a call made outside an invocation.
|
|
2
|
+
//
|
|
3
|
+
// A call outside an invocation starts a chain of its own: the first record it writes has no
|
|
4
|
+
// cause and is the root of the chain. Every record after it belongs to that chain, so the
|
|
5
|
+
// record before it is its cause and the trace down from the first record reaches all of them.
|
|
6
|
+
//
|
|
7
|
+
// The scenario runs in one process, against a real host on a temporary home:
|
|
8
|
+
//
|
|
9
|
+
// 1. an action called directly writes three records
|
|
10
|
+
// 2. the first record has no cause and is the root of the chain
|
|
11
|
+
// 3. the second record has the first as its cause, the third has the second
|
|
12
|
+
// 4. the trace down from the first record lists all three, in order
|
|
13
|
+
// 5. the causes of the third record walk back to the first one
|
|
14
|
+
|
|
15
|
+
import { expect, test } from "bun:test";
|
|
16
|
+
|
|
17
|
+
import { action, record, seq } from "@niwa-labs/core";
|
|
18
|
+
import { createNiwa } from "@niwa-labs/host";
|
|
19
|
+
import { plugin } from "@niwa-labs/plugin";
|
|
20
|
+
import { tempHome } from "@niwa-labs/testing";
|
|
21
|
+
import { z } from "zod";
|
|
22
|
+
|
|
23
|
+
const Fact = record("trace-call.fact", z.object({ n: z.number() }));
|
|
24
|
+
|
|
25
|
+
const three = action("trace-call.three", {
|
|
26
|
+
description: "Writes three records in one call.",
|
|
27
|
+
input: z.object({}),
|
|
28
|
+
output: z.object({ ids: z.array(z.string()) }),
|
|
29
|
+
recovery: "repeat",
|
|
30
|
+
effect: "write",
|
|
31
|
+
run: async (_input, ctx) => {
|
|
32
|
+
const ids: string[] = [];
|
|
33
|
+
for (const n of [1, 2, 3]) {
|
|
34
|
+
const written = await ctx.emit(Fact, { n });
|
|
35
|
+
ids.push(written.id);
|
|
36
|
+
}
|
|
37
|
+
return { ids };
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const facts = plugin("trace-call", (api) => {
|
|
42
|
+
api.declare(Fact);
|
|
43
|
+
api.action(three);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("a call outside an invocation: every record it writes hangs on the record before it", async () => {
|
|
47
|
+
const home = await tempHome();
|
|
48
|
+
const niwa = createNiwa({ home: home.path });
|
|
49
|
+
niwa.use(facts);
|
|
50
|
+
await niwa.serve();
|
|
51
|
+
|
|
52
|
+
try {
|
|
53
|
+
// 1. The call is made directly: outside any invocation.
|
|
54
|
+
const written = await niwa.call(three, {});
|
|
55
|
+
const records = await niwa.records.read({ types: [Fact] });
|
|
56
|
+
expect(records.map((record) => record.data)).toEqual([{ n: 1 }, { n: 2 }, { n: 3 }]);
|
|
57
|
+
|
|
58
|
+
const first = records[0];
|
|
59
|
+
const second = records[1];
|
|
60
|
+
const third = records[2];
|
|
61
|
+
expect(first).toBeDefined();
|
|
62
|
+
expect(second).toBeDefined();
|
|
63
|
+
expect(third).toBeDefined();
|
|
64
|
+
// The ids the call returned are the ids the journal holds.
|
|
65
|
+
expect(written.ids).toEqual([first!.id, second!.id, third!.id]);
|
|
66
|
+
|
|
67
|
+
// 2. The first record starts the chain: no cause, and it is its own root.
|
|
68
|
+
expect(first!.cause).toBeNull();
|
|
69
|
+
expect(first!.root).toBe(first!.id);
|
|
70
|
+
|
|
71
|
+
// 3. Every record after it has the record before it as its cause, and the root
|
|
72
|
+
// of the chain stays the first record.
|
|
73
|
+
expect(second!.cause).toBe(first!.id);
|
|
74
|
+
expect(second!.root).toBe(first!.id);
|
|
75
|
+
expect(third!.cause).toBe(second!.id);
|
|
76
|
+
expect(third!.root).toBe(first!.id);
|
|
77
|
+
|
|
78
|
+
// 4. The trace down from the first record lists all three, in order, each once.
|
|
79
|
+
const traced = await niwa.trace(first!.id);
|
|
80
|
+
expect(traced.map((record) => record.id)).toEqual([first!.id, second!.id, third!.id]);
|
|
81
|
+
expect(traced.map((record) => record.depth)).toEqual([0, 1, 2]);
|
|
82
|
+
|
|
83
|
+
// 5. The causes of the third record walk back to the first one.
|
|
84
|
+
const up = await niwa.causes(third!.id);
|
|
85
|
+
expect(up.map((record) => record.id)).toEqual([second!.id, first!.id]);
|
|
86
|
+
} finally {
|
|
87
|
+
await niwa.close();
|
|
88
|
+
await home.cleanup();
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test("two calls outside an invocation are two chains: the second call starts its own", async () => {
|
|
93
|
+
const home = await tempHome();
|
|
94
|
+
const niwa = createNiwa({ home: home.path });
|
|
95
|
+
niwa.use(facts);
|
|
96
|
+
await niwa.serve();
|
|
97
|
+
|
|
98
|
+
try {
|
|
99
|
+
await niwa.call(three, {});
|
|
100
|
+
await niwa.call(three, {});
|
|
101
|
+
|
|
102
|
+
const records = await niwa.records.read({ types: [Fact] });
|
|
103
|
+
expect(records).toHaveLength(6);
|
|
104
|
+
|
|
105
|
+
// Each call has its own root, and the trace down from one root does not reach the
|
|
106
|
+
// records of the other call.
|
|
107
|
+
const firstRoot = records[0]!;
|
|
108
|
+
const secondRoot = records[3]!;
|
|
109
|
+
expect(firstRoot.cause).toBeNull();
|
|
110
|
+
expect(secondRoot.cause).toBeNull();
|
|
111
|
+
expect(secondRoot.root).toBe(secondRoot.id);
|
|
112
|
+
expect(secondRoot.root).not.toBe(firstRoot.root);
|
|
113
|
+
|
|
114
|
+
const traced = await niwa.trace(firstRoot.id);
|
|
115
|
+
expect(traced.map((record) => record.id)).toEqual([
|
|
116
|
+
firstRoot.id,
|
|
117
|
+
records[1]!.id,
|
|
118
|
+
records[2]!.id,
|
|
119
|
+
]);
|
|
120
|
+
} finally {
|
|
121
|
+
await niwa.close();
|
|
122
|
+
await home.cleanup();
|
|
123
|
+
}
|
|
124
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// The filter of a trigger and the filter of the handler that subscribes to it are one condition.
|
|
2
|
+
|
|
3
|
+
import { expect, test } from "bun:test";
|
|
4
|
+
|
|
5
|
+
import { record } from "@niwa-labs/core";
|
|
6
|
+
import { createNiwa } from "@niwa-labs/host";
|
|
7
|
+
import { plugin, trigger } from "@niwa-labs/plugin";
|
|
8
|
+
import { eventually, tempHome } from "@niwa-labs/testing";
|
|
9
|
+
import { z } from "zod";
|
|
10
|
+
|
|
11
|
+
const Fired = record("filter.fired", z.object({ timer: z.string(), n: z.number() }), { open: true });
|
|
12
|
+
|
|
13
|
+
const declares = plugin("filter-types", (api) => api.declare(Fired));
|
|
14
|
+
const timerA = trigger(Fired, { name: "fired:a", filter: { timer: "a" } });
|
|
15
|
+
|
|
16
|
+
test("a handler of a trigger is woken by the records the trigger and its own filter both hold", async () => {
|
|
17
|
+
const home = await tempHome();
|
|
18
|
+
const niwa = createNiwa({ home: home.path });
|
|
19
|
+
const seen: number[] = [];
|
|
20
|
+
try {
|
|
21
|
+
niwa.use(declares, plugin("both", (api) => api.on(timerA, { filter: { n: 2 } }, (data) => void seen.push(data.n))));
|
|
22
|
+
await niwa.serve();
|
|
23
|
+
await niwa.emit(Fired, { timer: "a", n: 1 });
|
|
24
|
+
await niwa.emit(Fired, { timer: "b", n: 2 });
|
|
25
|
+
await niwa.emit(Fired, { timer: "a", n: 2 });
|
|
26
|
+
await eventually(() => seen.length >= 1);
|
|
27
|
+
await Bun.sleep(100);
|
|
28
|
+
expect(seen).toEqual([2]);
|
|
29
|
+
} finally {
|
|
30
|
+
await niwa.close();
|
|
31
|
+
await home.cleanup();
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test("a handler whose filter gives a path another value than its trigger is refused and names the path", () => {
|
|
36
|
+
const host = createNiwa({ home: "/nonexistent" });
|
|
37
|
+
expect(() => host.use(declares, plugin("conflict", (api) => api.on(timerA, { filter: { timer: "b" } }, () => undefined)))).toThrow(
|
|
38
|
+
/handler "fired:a".*timer = "b".*"fired:a" holds timer = "a"/,
|
|
39
|
+
);
|
|
40
|
+
// The same value is no conflict.
|
|
41
|
+
expect(() => host.use(plugin("agree", (api) => api.on(timerA, { filter: { timer: "a" } }, () => undefined)))).not.toThrow();
|
|
42
|
+
});
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// `use` takes several plugins and installs them in order; it stops at the first one that fails.
|
|
2
|
+
|
|
3
|
+
import { expect, test } from "bun:test";
|
|
4
|
+
|
|
5
|
+
import { createNiwa } from "@niwa-labs/host";
|
|
6
|
+
import { plugin, pluginName } from "@niwa-labs/plugin";
|
|
7
|
+
import { record, PluginInstallFailed } from "@niwa-labs/core";
|
|
8
|
+
import { tempHome } from "@niwa-labs/testing";
|
|
9
|
+
import { z } from "zod";
|
|
10
|
+
|
|
11
|
+
const One = record("many.one", z.object({}));
|
|
12
|
+
const Two = record("many.two", z.object({}));
|
|
13
|
+
const Three = record("many.three", z.object({}));
|
|
14
|
+
|
|
15
|
+
test("several plugins in one call are all installed, in order", async () => {
|
|
16
|
+
const home = await tempHome();
|
|
17
|
+
const niwa = createNiwa({ home: home.path });
|
|
18
|
+
try {
|
|
19
|
+
niwa.use(
|
|
20
|
+
plugin("many-one", (a) => a.declare(One)),
|
|
21
|
+
plugin("many-two", (a) => a.declare(Two)),
|
|
22
|
+
plugin("many-three", (a) => a.declare(Three)),
|
|
23
|
+
);
|
|
24
|
+
const d = await niwa.describe();
|
|
25
|
+
expect(d.types.map((t) => t.name)).toEqual([One.name, Two.name, Three.name]);
|
|
26
|
+
expect(niwa.installed(pluginName("many-three"))).toBeDefined();
|
|
27
|
+
} finally {
|
|
28
|
+
await niwa.close();
|
|
29
|
+
await home.cleanup();
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test("a failing plugin stops the call: earlier ones stay, later ones are not tried, the error names it", async () => {
|
|
34
|
+
const home = await tempHome();
|
|
35
|
+
const niwa = createNiwa({ home: home.path });
|
|
36
|
+
try {
|
|
37
|
+
let error: unknown;
|
|
38
|
+
try {
|
|
39
|
+
niwa.use(
|
|
40
|
+
plugin("many-one", (a) => a.declare(One)),
|
|
41
|
+
plugin("many-bad", () => {
|
|
42
|
+
throw new Error("boom");
|
|
43
|
+
}),
|
|
44
|
+
plugin("many-three", (a) => a.declare(Three)),
|
|
45
|
+
);
|
|
46
|
+
} catch (caught) {
|
|
47
|
+
error = caught;
|
|
48
|
+
}
|
|
49
|
+
expect(error).toBeInstanceOf(PluginInstallFailed);
|
|
50
|
+
const failed = error as PluginInstallFailed;
|
|
51
|
+
expect(failed.plugin).toBe("many-bad");
|
|
52
|
+
expect(failed.installed).toEqual(["many-one"]);
|
|
53
|
+
expect(failed.skipped).toEqual(["many-three"]);
|
|
54
|
+
expect(failed.message).toContain("boom");
|
|
55
|
+
expect(niwa.installed(pluginName("many-one"))).toBeDefined();
|
|
56
|
+
expect(niwa.installed(pluginName("many-bad"))).toBeUndefined();
|
|
57
|
+
expect(niwa.installed(pluginName("many-three"))).toBeUndefined();
|
|
58
|
+
expect((await niwa.describe()).types.map((t) => t.name)).toEqual([One.name]);
|
|
59
|
+
} finally {
|
|
60
|
+
await niwa.close();
|
|
61
|
+
await home.cleanup();
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test("no plugins is a no-op", async () => {
|
|
66
|
+
const home = await tempHome();
|
|
67
|
+
const niwa = createNiwa({ home: home.path });
|
|
68
|
+
try {
|
|
69
|
+
niwa.use();
|
|
70
|
+
expect((await niwa.describe()).types).toEqual([]);
|
|
71
|
+
} finally {
|
|
72
|
+
await niwa.close();
|
|
73
|
+
await home.cleanup();
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("a single failing plugin is wrapped too: it keeps the original error as its cause and installs nothing", async () => {
|
|
78
|
+
const home = await tempHome();
|
|
79
|
+
const niwa = createNiwa({ home: home.path });
|
|
80
|
+
try {
|
|
81
|
+
const original = new TypeError("bad setup");
|
|
82
|
+
let error: unknown;
|
|
83
|
+
try {
|
|
84
|
+
niwa.use(
|
|
85
|
+
plugin("many-bad", () => {
|
|
86
|
+
throw original;
|
|
87
|
+
}),
|
|
88
|
+
);
|
|
89
|
+
} catch (caught) {
|
|
90
|
+
error = caught;
|
|
91
|
+
}
|
|
92
|
+
expect(error).toBeInstanceOf(PluginInstallFailed);
|
|
93
|
+
const failed = error as PluginInstallFailed;
|
|
94
|
+
expect(failed.plugin).toBe("many-bad");
|
|
95
|
+
expect(failed.installed).toEqual([]);
|
|
96
|
+
expect(failed.skipped).toEqual([]);
|
|
97
|
+
expect(failed.cause).toBe(original);
|
|
98
|
+
expect(failed.message).toContain("bad setup");
|
|
99
|
+
} finally {
|
|
100
|
+
await niwa.close();
|
|
101
|
+
await home.cleanup();
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("a failing first plugin leaves the later ones untried and not installed", async () => {
|
|
106
|
+
const home = await tempHome();
|
|
107
|
+
const niwa = createNiwa({ home: home.path });
|
|
108
|
+
try {
|
|
109
|
+
let error: unknown;
|
|
110
|
+
try {
|
|
111
|
+
niwa.use(
|
|
112
|
+
plugin("many-bad", () => {
|
|
113
|
+
throw new Error("first");
|
|
114
|
+
}),
|
|
115
|
+
plugin("many-one", (a) => a.declare(One)),
|
|
116
|
+
);
|
|
117
|
+
} catch (caught) {
|
|
118
|
+
error = caught;
|
|
119
|
+
}
|
|
120
|
+
const failed = error as PluginInstallFailed;
|
|
121
|
+
expect(failed.installed).toEqual([]);
|
|
122
|
+
expect(failed.skipped).toEqual(["many-one"]);
|
|
123
|
+
expect(niwa.installed(pluginName("many-one"))).toBeUndefined();
|
|
124
|
+
} finally {
|
|
125
|
+
await niwa.close();
|
|
126
|
+
await home.cleanup();
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test("a replacement that fails leaves the plugin it was to replace in place, also while serving", async () => {
|
|
131
|
+
const home = await tempHome();
|
|
132
|
+
const niwa = createNiwa({ home: home.path });
|
|
133
|
+
try {
|
|
134
|
+
const original = plugin("many-one", (a) => a.declare(One));
|
|
135
|
+
niwa.use(original);
|
|
136
|
+
await niwa.serve();
|
|
137
|
+
expect(() =>
|
|
138
|
+
niwa.use(
|
|
139
|
+
plugin("many-one", (a) => {
|
|
140
|
+
a.declare(Two);
|
|
141
|
+
throw new Error("late failure");
|
|
142
|
+
}),
|
|
143
|
+
),
|
|
144
|
+
).toThrow(PluginInstallFailed);
|
|
145
|
+
expect(niwa.installed(pluginName("many-one"))).toBe(original);
|
|
146
|
+
expect((await niwa.describe()).types.map((t) => t.name)).toEqual([One.name]);
|
|
147
|
+
} finally {
|
|
148
|
+
await niwa.close();
|
|
149
|
+
await home.cleanup();
|
|
150
|
+
}
|
|
151
|
+
});
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// A wait takes the same query as a read: a type, equality on paths of the data, and a position to
|
|
2
|
+
// look after. It answers with the record's data and its position, so the next wait goes on from
|
|
3
|
+
// there and misses nothing in between.
|
|
4
|
+
|
|
5
|
+
import { expect, test } from "bun:test";
|
|
6
|
+
|
|
7
|
+
import { record, seq } from "@niwa-labs/core";
|
|
8
|
+
import type { Seq, StoredRecord } from "@niwa-labs/core";
|
|
9
|
+
import { createNiwa } from "@niwa-labs/host";
|
|
10
|
+
import { plugin } from "@niwa-labs/plugin";
|
|
11
|
+
import { eventually, tempHome } from "@niwa-labs/testing";
|
|
12
|
+
import { z } from "zod";
|
|
13
|
+
|
|
14
|
+
const Start = record("waitq.start", z.object({ n: z.number() }));
|
|
15
|
+
const Answer = record("waitq.answer", z.object({ to: z.object({ n: z.number() }), text: z.string() }));
|
|
16
|
+
const AnswerV2 = record("waitq.answer", z.object({ to: z.object({ n: z.number() }), text: z.string() }), { version: 2 });
|
|
17
|
+
|
|
18
|
+
test("a wait answers with the data and the position, and the next wait from that position misses nothing", async () => {
|
|
19
|
+
const home = await tempHome();
|
|
20
|
+
const got: Array<{ text: string; at: Seq }> = [];
|
|
21
|
+
const niwa = createNiwa({ home: home.path });
|
|
22
|
+
niwa.use(
|
|
23
|
+
plugin("waitq", (api) => {
|
|
24
|
+
api.declare(Start, Answer);
|
|
25
|
+
api.on(Start, async (data, step) => {
|
|
26
|
+
const first = await step.wait(Answer, { filter: { "to.n": data.n } });
|
|
27
|
+
const second = await step.wait(Answer, { filter: { "to.n": data.n }, from: first.at });
|
|
28
|
+
for (const one of [first, second]) if (!got.some((seen) => seen.at === one.at)) got.push({ text: one.data.text, at: one.at });
|
|
29
|
+
});
|
|
30
|
+
}),
|
|
31
|
+
);
|
|
32
|
+
try {
|
|
33
|
+
await niwa.serve();
|
|
34
|
+
await niwa.emit(Start, { n: 1 });
|
|
35
|
+
await eventually(async () => (await niwa.records.read()).some((row) => row.type === "niwa.invocation.state" && (row.data as { state: string }).state === "waiting"));
|
|
36
|
+
// Two answers in a row, and one for another start between them: the second wait finds the
|
|
37
|
+
// second answer, which was committed before it began.
|
|
38
|
+
const a = await niwa.emit(Answer, { to: { n: 1 }, text: "a" });
|
|
39
|
+
await niwa.emit(Answer, { to: { n: 2 }, text: "other" });
|
|
40
|
+
const b = await niwa.emit(Answer, { to: { n: 1 }, text: "b" });
|
|
41
|
+
await eventually(() => got.length === 2);
|
|
42
|
+
expect(got).toEqual([
|
|
43
|
+
{ text: "a", at: a.seq },
|
|
44
|
+
{ text: "b", at: b.seq },
|
|
45
|
+
]);
|
|
46
|
+
} finally {
|
|
47
|
+
await niwa.close();
|
|
48
|
+
await home.cleanup();
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("a wait from a position is answered by a record committed after it and before the wait began", async () => {
|
|
53
|
+
const home = await tempHome();
|
|
54
|
+
const answers: string[] = [];
|
|
55
|
+
let mark: StoredRecord | undefined;
|
|
56
|
+
const niwa = createNiwa({ home: home.path });
|
|
57
|
+
niwa.use(
|
|
58
|
+
plugin("waitq", (api) => {
|
|
59
|
+
api.declare(Start, Answer);
|
|
60
|
+
api.on(Start, async (data, step) => {
|
|
61
|
+
const found = await step.wait(Answer, { filter: { "to.n": data.n }, from: mark?.seq ?? seq(0) });
|
|
62
|
+
answers.push(found.data.text);
|
|
63
|
+
});
|
|
64
|
+
}),
|
|
65
|
+
);
|
|
66
|
+
try {
|
|
67
|
+
await niwa.serve();
|
|
68
|
+
mark = await niwa.emit(Answer, { to: { n: 9 }, text: "before the mark" });
|
|
69
|
+
await niwa.emit(Answer, { to: { n: 1 }, text: "early" });
|
|
70
|
+
await niwa.emit(Start, { n: 1 });
|
|
71
|
+
await eventually(() => answers.length === 1);
|
|
72
|
+
expect(answers).toEqual(["early"]);
|
|
73
|
+
} finally {
|
|
74
|
+
await niwa.close();
|
|
75
|
+
await home.cleanup();
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("a record of another schema version does not answer a wait, and the answer survives a restart with its position", async () => {
|
|
80
|
+
const home = await tempHome();
|
|
81
|
+
const seen: Array<{ text: string; at: number }> = [];
|
|
82
|
+
const waiter = (types: { answer: typeof Answer | typeof AnswerV2; declare: boolean }) =>
|
|
83
|
+
plugin("waitq", (api) => {
|
|
84
|
+
api.declare(Start, ...(types.declare ? [types.answer] : []));
|
|
85
|
+
api.on(Start, async (data, step) => {
|
|
86
|
+
const found = await step.wait(Answer, { filter: { "to.n": data.n } });
|
|
87
|
+
seen.push({ text: found.data.text, at: found.at });
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
try {
|
|
91
|
+
const first = createNiwa({ home: home.path });
|
|
92
|
+
first.use(waiter({ answer: Answer, declare: true }));
|
|
93
|
+
await first.serve();
|
|
94
|
+
await first.emit(Start, { n: 1 });
|
|
95
|
+
await eventually(async () => (await first.records.read()).some((row) => row.type === "niwa.invocation.state" && (row.data as { state: string }).state === "waiting"));
|
|
96
|
+
await first.close();
|
|
97
|
+
|
|
98
|
+
const newer = createNiwa({ home: home.path });
|
|
99
|
+
// The same plugin, with the type at its next version: the waiting invocation goes on on it.
|
|
100
|
+
newer.use(waiter({ answer: AnswerV2, declare: true }));
|
|
101
|
+
await newer.serve();
|
|
102
|
+
await newer.emit(AnswerV2, { to: { n: 1 }, text: "version two" });
|
|
103
|
+
await Bun.sleep(150);
|
|
104
|
+
expect(seen).toEqual([]);
|
|
105
|
+
await newer.close();
|
|
106
|
+
|
|
107
|
+
const again = createNiwa({ home: home.path });
|
|
108
|
+
again.use(waiter({ answer: Answer, declare: true }));
|
|
109
|
+
await again.serve();
|
|
110
|
+
const answer = await again.emit(Answer, { to: { n: 1 }, text: "version one" });
|
|
111
|
+
await eventually(() => seen.length === 1);
|
|
112
|
+
expect(seen).toEqual([{ text: "version one", at: answer.seq }]);
|
|
113
|
+
await again.close();
|
|
114
|
+
} finally {
|
|
115
|
+
await home.cleanup();
|
|
116
|
+
}
|
|
117
|
+
});
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
// Waiting survives a restart.
|
|
2
|
+
//
|
|
3
|
+
// The scenario runs against a real host process (fixtures/wait-restart-host.ts):
|
|
4
|
+
//
|
|
5
|
+
// 1. spawn the host, connect a client, emit Start { q }
|
|
6
|
+
// 2. the handler calls ask (counted) and then waits for an Answer record;
|
|
7
|
+
// the invocation's state becomes "waiting"
|
|
8
|
+
// 3. kill the host with SIGKILL — a real crash, while the invocation waits
|
|
9
|
+
// 4. spawn the host again and wait for it to serve; the invocation is picked
|
|
10
|
+
// up again — the journal grows by a state record — and it waits again:
|
|
11
|
+
// the question is not asked a second time
|
|
12
|
+
// 5. emit Answer { q, text }; the wait is satisfied, the handler continues
|
|
13
|
+
// from where it waited, calls after (counted), and the invocation ends
|
|
14
|
+
// as done
|
|
15
|
+
// 6. ask was called once in total, after once
|
|
16
|
+
//
|
|
17
|
+
// Only the public API is used: the entries of the packages and "@niwa-labs/testing".
|
|
18
|
+
|
|
19
|
+
import { expect, test } from "bun:test";
|
|
20
|
+
import { fileURLToPath } from "node:url";
|
|
21
|
+
|
|
22
|
+
import { connect } from "@niwa-labs/client";
|
|
23
|
+
import { seq } from "@niwa-labs/core";
|
|
24
|
+
import type { StoredRecord } from "@niwa-labs/core";
|
|
25
|
+
import { counter, eventually, spawnHost, tempHome, connectAsOwner } from "@niwa-labs/testing";
|
|
26
|
+
import { z } from "zod";
|
|
27
|
+
|
|
28
|
+
import { Answer, Start } from "./fixtures/wait-restart-host.ts";
|
|
29
|
+
|
|
30
|
+
const HOST_SCRIPT = fileURLToPath(new URL("./fixtures/wait-restart-host.ts", import.meta.url));
|
|
31
|
+
|
|
32
|
+
/** The question the handler asks and then waits about. */
|
|
33
|
+
const QUESTION = "what is the answer";
|
|
34
|
+
|
|
35
|
+
/** The type the core writes for every change of an invocation's state. */
|
|
36
|
+
const STATE_TYPE = "niwa.invocation.state";
|
|
37
|
+
|
|
38
|
+
/** From the beginning of the journal. */
|
|
39
|
+
const FROM_START = seq(0);
|
|
40
|
+
|
|
41
|
+
/** The data of a CORE.invocationState record: { invocation, state, reason? }. */
|
|
42
|
+
const InvocationState = z.object({
|
|
43
|
+
invocation: z.string(),
|
|
44
|
+
state: z.string(),
|
|
45
|
+
reason: z.string().optional(),
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The states of the invocation, in the order the journal holds them.
|
|
50
|
+
*
|
|
51
|
+
* There is one invocation in this check: only the Start record has a handler,
|
|
52
|
+
* and the Answer record is written by a client, not by a handler.
|
|
53
|
+
*/
|
|
54
|
+
function statesOf(records: StoredRecord[]): string[] {
|
|
55
|
+
const states: string[] = [];
|
|
56
|
+
for (const record of records) {
|
|
57
|
+
if (record.type !== STATE_TYPE) continue;
|
|
58
|
+
const parsed = InvocationState.safeParse(record.data);
|
|
59
|
+
if (parsed.success) states.push(parsed.data.state);
|
|
60
|
+
}
|
|
61
|
+
return states;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The state of the last state record the journal holds, if there is one. */
|
|
65
|
+
function lastState(records: StoredRecord[]): string | undefined {
|
|
66
|
+
const states = statesOf(records);
|
|
67
|
+
return states[states.length - 1];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** True for a record "niwa.invocation.state" whose state is "done". */
|
|
71
|
+
function isDoneState(record: StoredRecord): boolean {
|
|
72
|
+
if (record.type !== STATE_TYPE) return false;
|
|
73
|
+
const parsed = InvocationState.safeParse(record.data);
|
|
74
|
+
return parsed.success && parsed.data.state === "done";
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** The highest journal position in these records. */
|
|
78
|
+
function highestSeq(records: StoredRecord[]): number {
|
|
79
|
+
return records.reduce((highest, record) => Math.max(highest, record.seq), 0);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
test("waiting survives a restart: the question is asked once and the answer still arrives", async () => {
|
|
83
|
+
const home = await tempHome();
|
|
84
|
+
const homePath = home.path;
|
|
85
|
+
const clients: Array<{ close(): Promise<void> }> = [];
|
|
86
|
+
let first: ReturnType<typeof spawnHost> | undefined;
|
|
87
|
+
let second: ReturnType<typeof spawnHost> | undefined;
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
// 1. The first host. It is killed while the invocation waits.
|
|
91
|
+
first = spawnHost(HOST_SCRIPT, homePath);
|
|
92
|
+
await first.ready();
|
|
93
|
+
|
|
94
|
+
const client = await connectAsOwner(homePath);
|
|
95
|
+
const watcher = await connectAsOwner(homePath);
|
|
96
|
+
clients.push(client, watcher);
|
|
97
|
+
const seen: StoredRecord[] = [];
|
|
98
|
+
const watch = (async () => {
|
|
99
|
+
for await (const record of watcher.records.since()) {
|
|
100
|
+
seen.push(record);
|
|
101
|
+
}
|
|
102
|
+
})().catch(() => {});
|
|
103
|
+
|
|
104
|
+
await client.emit(Start, { q: QUESTION });
|
|
105
|
+
|
|
106
|
+
// 2. The question was asked, and the invocation is waiting for the answer.
|
|
107
|
+
// Both are observed, neither is inferred: the counter is the count of
|
|
108
|
+
// real calls, the state is a record the host wrote.
|
|
109
|
+
await eventually(
|
|
110
|
+
() =>
|
|
111
|
+
counter(homePath, "ask").count() === 1 && statesOf(seen).includes("waiting"),
|
|
112
|
+
10_000,
|
|
113
|
+
"the question should be asked once and the invocation should be waiting",
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
expect(counter(homePath, "ask").count()).toBe(1);
|
|
117
|
+
expect(counter(homePath, "after").count()).toBe(0);
|
|
118
|
+
|
|
119
|
+
// Where the journal stands before the crash. Everything the second host
|
|
120
|
+
// writes is after this position.
|
|
121
|
+
const beforeCrash = highestSeq(seen);
|
|
122
|
+
|
|
123
|
+
// 3. A real crash: SIGKILL, no clean shutdown.
|
|
124
|
+
await first.kill();
|
|
125
|
+
first = undefined;
|
|
126
|
+
await watcher.close().catch(() => {});
|
|
127
|
+
await watch;
|
|
128
|
+
|
|
129
|
+
// 4. The second host takes the waiting invocation up again.
|
|
130
|
+
second = spawnHost(HOST_SCRIPT, homePath);
|
|
131
|
+
await second.ready();
|
|
132
|
+
|
|
133
|
+
// The observer subscribes from the beginning of the journal, so it reads
|
|
134
|
+
// the states of the first host back from the journal as well.
|
|
135
|
+
const observer = await connectAsOwner(homePath);
|
|
136
|
+
clients.push(observer);
|
|
137
|
+
const history: StoredRecord[] = [];
|
|
138
|
+
const observe = (async () => {
|
|
139
|
+
for await (const record of observer.records.since()) {
|
|
140
|
+
history.push(record);
|
|
141
|
+
}
|
|
142
|
+
})().catch(() => {});
|
|
143
|
+
|
|
144
|
+
// The restarted host took the invocation up again: the journal grew by a
|
|
145
|
+
// state record after the crash, and the invocation is waiting — it neither
|
|
146
|
+
// ended nor broke.
|
|
147
|
+
await eventually(
|
|
148
|
+
() => {
|
|
149
|
+
const fresh = history.filter((record) => record.seq > beforeCrash);
|
|
150
|
+
return (
|
|
151
|
+
fresh.some((record) => record.type === STATE_TYPE) &&
|
|
152
|
+
lastState(history) === "waiting" &&
|
|
153
|
+
!statesOf(history).includes("done") &&
|
|
154
|
+
!statesOf(history).includes("paused")
|
|
155
|
+
);
|
|
156
|
+
},
|
|
157
|
+
15_000,
|
|
158
|
+
"the restarted host should resume the waiting invocation",
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
// The question was not asked again, and nothing happened after it.
|
|
162
|
+
expect(counter(homePath, "ask").count()).toBe(1);
|
|
163
|
+
expect(counter(homePath, "after").count()).toBe(0);
|
|
164
|
+
|
|
165
|
+
// 5. The answer arrives; the handler continues from where it waited.
|
|
166
|
+
await client.close().catch(() => {});
|
|
167
|
+
const answerer = await connectAsOwner(homePath);
|
|
168
|
+
clients.push(answerer);
|
|
169
|
+
await answerer.emit(Answer, { q: QUESTION, text: "42" });
|
|
170
|
+
|
|
171
|
+
await eventually(
|
|
172
|
+
() => counter(homePath, "after").count() === 1 && history.some(isDoneState),
|
|
173
|
+
15_000,
|
|
174
|
+
"the handler should continue after the answer and the invocation should be done",
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
// 6. The question was still asked only once, in total.
|
|
178
|
+
expect(counter(homePath, "ask").count()).toBe(1);
|
|
179
|
+
} finally {
|
|
180
|
+
for (const client of clients) {
|
|
181
|
+
await client.close().catch(() => {});
|
|
182
|
+
}
|
|
183
|
+
await first?.kill().catch(() => {});
|
|
184
|
+
await second?.kill().catch(() => {});
|
|
185
|
+
await home.cleanup();
|
|
186
|
+
}
|
|
187
|
+
}, 30_000);
|