@niwa-labs/tasks 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 +4 -2
- package/package.json +42 -4
- package/src/actions.ts +109 -0
- package/src/cockpit/actions.ts +52 -0
- package/src/cockpit/index.tsx +32 -0
- package/src/cockpit/pages.tsx +103 -0
- package/src/executor.ts +214 -0
- package/src/index.ts +516 -0
- package/src/ledger.ts +88 -0
- package/src/owner.ts +178 -0
- package/src/records-entry.ts +38 -0
- package/src/records.ts +118 -0
- package/src/strategy.ts +60 -0
- package/test/asker.test.ts +67 -0
- package/test/cockpit-actions.test.ts +19 -0
- package/test/executor.test.ts +520 -0
- package/test/ledger.test.ts +172 -0
- package/test/notifications.test.ts +170 -0
- package/test/queries.test.ts +19 -0
- package/test/steps.test.ts +147 -0
- package/test/tasks.test.ts +536 -0
- package/tsconfig.json +15 -0
|
@@ -0,0 +1,536 @@
|
|
|
1
|
+
// The tasks module: a task is a contract and nothing else.
|
|
2
|
+
//
|
|
3
|
+
// The scenario runs in one process, against a real host on a temporary home:
|
|
4
|
+
//
|
|
5
|
+
// 1. the tasks plugin, the messages plugin and the fake strategy are installed
|
|
6
|
+
// 2. a task is created: it returns at once, with the id of the task, and the
|
|
7
|
+
// Assignment message goes to the responsible address
|
|
8
|
+
// 3. the fake strategy, which is the responsible party, accepts the task and
|
|
9
|
+
// answers for the result
|
|
10
|
+
// 4. the result of the task arrives: "task.result" gives it at once
|
|
11
|
+
// 5. finishing a task twice is refused, and the first end stands
|
|
12
|
+
// 6. the trace from the record that woke the strategy joins the task, the
|
|
13
|
+
// message and the result
|
|
14
|
+
//
|
|
15
|
+
// Only the public API is used: the entries of the packages.
|
|
16
|
+
|
|
17
|
+
import { expect, test } from "bun:test";
|
|
18
|
+
|
|
19
|
+
import { record, seq, strategy } from "@niwa-labs/core";
|
|
20
|
+
import type { Address, Caller, RecordTypeName, StoredRecord } from "@niwa-labs/core";
|
|
21
|
+
import { MessageSent } from "@niwa-labs/messages/records";
|
|
22
|
+
import { plugin } from "@niwa-labs/plugin";
|
|
23
|
+
import type { CallerBinding, Plugin } from "@niwa-labs/plugin";
|
|
24
|
+
import {
|
|
25
|
+
TaskCreated,
|
|
26
|
+
TaskDone,
|
|
27
|
+
TaskId,
|
|
28
|
+
taskActions,
|
|
29
|
+
taskCreate,
|
|
30
|
+
taskFail,
|
|
31
|
+
taskFinish,
|
|
32
|
+
taskIds,
|
|
33
|
+
taskLedger,
|
|
34
|
+
taskReport,
|
|
35
|
+
taskResult,
|
|
36
|
+
tasks,
|
|
37
|
+
} from "../src/index.ts";
|
|
38
|
+
import type { Strategy, TaskAnswer, TaskContract } from "../src/index.ts";
|
|
39
|
+
import { connect, connectAsOwner, createNiwa, messageSend, messages, tempHome } from "@niwa-labs/testing";
|
|
40
|
+
import type { Host } from "@niwa-labs/testing";
|
|
41
|
+
import { z } from "zod";
|
|
42
|
+
|
|
43
|
+
/** The record that wakes the strategy: it asks for the work to be done on one task. */
|
|
44
|
+
const Work = record("tasks.work", z.object({ text: z.string(), task: TaskId }), { open: true });
|
|
45
|
+
|
|
46
|
+
/** The text of the work. */
|
|
47
|
+
const TEXT = "Move argument parsing into its own module.";
|
|
48
|
+
|
|
49
|
+
/** The address of the strategy: the responsible party of the task. */
|
|
50
|
+
const STRATEGY = strategy("fake");
|
|
51
|
+
|
|
52
|
+
/** The address that asks for the work. */
|
|
53
|
+
const ASKER = { kind: "owner" } as const;
|
|
54
|
+
|
|
55
|
+
/** The address the result is reported to. */
|
|
56
|
+
const REPORTER = { kind: "owner" } as const;
|
|
57
|
+
|
|
58
|
+
/** What the fake strategy was asked to do, and what it answered. */
|
|
59
|
+
interface Asked {
|
|
60
|
+
readonly text: string;
|
|
61
|
+
readonly answer: TaskAnswer;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The fake strategy: the responsible party of the task.
|
|
66
|
+
*
|
|
67
|
+
* It accepts every task and answers with a fixed answer. It is a strategy, so the base does not
|
|
68
|
+
* tell it how to work: it hands the task over and waits for the answer.
|
|
69
|
+
*/
|
|
70
|
+
function fakeStrategy(asked: Asked[]): Strategy {
|
|
71
|
+
return {
|
|
72
|
+
address: STRATEGY,
|
|
73
|
+
accept: () => true,
|
|
74
|
+
answer: async (task) => {
|
|
75
|
+
const answer: TaskAnswer = { status: "result", result: `done: ${task.text}` };
|
|
76
|
+
asked.push({ text: task.text, answer });
|
|
77
|
+
return answer;
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The strategy's own plugin speaks as the strategy; a client speaks as the strategy too. */
|
|
83
|
+
const bind: CallerBinding = (caller: Caller) =>
|
|
84
|
+
caller.kind === "client" || (caller.kind === "handler" && caller.handler.startsWith("fake-strategy/"))
|
|
85
|
+
? STRATEGY
|
|
86
|
+
: undefined;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The plugin of the fake strategy.
|
|
90
|
+
*
|
|
91
|
+
* It reads the contract of the task out of the journal, accepts the task, and finishes it with
|
|
92
|
+
* the answer of the strategy.
|
|
93
|
+
*/
|
|
94
|
+
function strategyPlugin(fake: Strategy) {
|
|
95
|
+
return plugin("fake-strategy", (api) => {
|
|
96
|
+
api.declare(Work);
|
|
97
|
+
const ledger = taskLedger(() => api.journal?.());
|
|
98
|
+
const acts = taskActions(ledger, taskIds(() => api.journal?.()), bind);
|
|
99
|
+
api.on(Work, async (work, step) => {
|
|
100
|
+
const contract = ledger.state(work.task)?.contract;
|
|
101
|
+
if (contract === undefined || !(await fake.accept(contract))) return;
|
|
102
|
+
const answer = await fake.answer(contract);
|
|
103
|
+
if (answer.status === "result") await step(acts.finish, { task: work.task, result: answer.result });
|
|
104
|
+
else await step(acts.fail, { task: work.task, reason: answer.reason });
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
test("the tasks module: a task returns at once, the result arrives, finishing twice is refused, and the trace joins the task, the message and the result", async () => {
|
|
110
|
+
const home = await tempHome();
|
|
111
|
+
const asked: Asked[] = [];
|
|
112
|
+
const fake = fakeStrategy(asked);
|
|
113
|
+
|
|
114
|
+
const niwa = createNiwa({ home: home.path });
|
|
115
|
+
niwa.use(messages({ bind }));
|
|
116
|
+
niwa.use(tasks({ messages: { bind } }));
|
|
117
|
+
niwa.use(strategyPlugin(fake));
|
|
118
|
+
await niwa.serve();
|
|
119
|
+
|
|
120
|
+
try {
|
|
121
|
+
const client = await connectAsOwner(home.path);
|
|
122
|
+
|
|
123
|
+
// The task is created and returns at once, with the id niwa issued.
|
|
124
|
+
const created = await niwa.call(taskCreate, contractOf());
|
|
125
|
+
const TASK_ID = created.id;
|
|
126
|
+
expect(TASK_ID).toMatch(/^task-[0-9a-f]{6,8}$/);
|
|
127
|
+
|
|
128
|
+
// The contract of the task is in the journal: that is how a responsible party
|
|
129
|
+
// learns what it is responsible for.
|
|
130
|
+
const taskRecords = await recordsOf(client, TaskCreated.name);
|
|
131
|
+
expect(taskRecords).toHaveLength(1);
|
|
132
|
+
const created0 = taskRecords[0];
|
|
133
|
+
const contract = created0?.data as TaskContract;
|
|
134
|
+
expect(contract.id).toBe(TASK_ID);
|
|
135
|
+
expect(contract.kind).toBe("change");
|
|
136
|
+
expect<string>(contract.description).toBe(DESCRIPTION);
|
|
137
|
+
expect(contract.text).toBe(TEXT);
|
|
138
|
+
expect(contract.responsible).toEqual(STRATEGY);
|
|
139
|
+
expect(contract.askedBy).toEqual(ASKER);
|
|
140
|
+
expect(contract.reportTo).toEqual(REPORTER);
|
|
141
|
+
|
|
142
|
+
// The Assignment message went to the responsible address.
|
|
143
|
+
const sent = await recordsOf(client, MessageSent.name);
|
|
144
|
+
expect(sent).toHaveLength(1);
|
|
145
|
+
const envelope = sent[0]?.data as {
|
|
146
|
+
id: string;
|
|
147
|
+
to: { kind: string; id: string };
|
|
148
|
+
task?: string;
|
|
149
|
+
body: { type: string; text: string; kind?: string };
|
|
150
|
+
};
|
|
151
|
+
expect(envelope.id).toMatch(/^msg-[0-9a-f]{6,8}$/);
|
|
152
|
+
expect(envelope.to).toEqual({ kind: "strategy", id: "fake" });
|
|
153
|
+
expect(envelope.task).toBe(TASK_ID);
|
|
154
|
+
expect(envelope.body.type).toBe("assignment");
|
|
155
|
+
expect(envelope.body.text).toBe(TEXT);
|
|
156
|
+
|
|
157
|
+
// The strategy is the responsible party: it accepts the task and answers. Its
|
|
158
|
+
// work depends on the task, so the whole chain hangs from the task.
|
|
159
|
+
await client.emit(Work, { text: TEXT, task: TASK_ID }, { links: [created0?.id as never] });
|
|
160
|
+
await eventually(() => asked.length === 1, 10_000, "the strategy should have been asked");
|
|
161
|
+
expect(asked[0]?.text).toBe(TEXT);
|
|
162
|
+
|
|
163
|
+
// The result arrives: the task is done, and the result is what it answered.
|
|
164
|
+
const result = await client.call(taskResult, { task: TASK_ID });
|
|
165
|
+
expect(result).toEqual({
|
|
166
|
+
task: TASK_ID,
|
|
167
|
+
outcome: { status: "result", result: `done: ${TEXT}` },
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// Finishing the task again is refused: a task finishes once.
|
|
171
|
+
let refused: unknown;
|
|
172
|
+
try {
|
|
173
|
+
await client.call(taskFinish, { task: TASK_ID, result: "something else" });
|
|
174
|
+
} catch (error) {
|
|
175
|
+
refused = error;
|
|
176
|
+
}
|
|
177
|
+
expect(refused).toBeInstanceOf(Error);
|
|
178
|
+
expect((refused as Error).message).toContain("already done");
|
|
179
|
+
|
|
180
|
+
// The first end stands: the result is still the one the strategy gave.
|
|
181
|
+
const again = await client.call(taskResult, { task: TASK_ID });
|
|
182
|
+
expect(again).toEqual(result);
|
|
183
|
+
|
|
184
|
+
// The trace from the record that created the task joins the task, the message
|
|
185
|
+
// and the result.
|
|
186
|
+
expect(created0).toBeDefined();
|
|
187
|
+
const traced = await client.trace(created0?.id as never);
|
|
188
|
+
const types = traced.map((record) => record.type);
|
|
189
|
+
expect(types).toContain(TaskCreated.name);
|
|
190
|
+
expect(types).toContain(MessageSent.name);
|
|
191
|
+
expect(types).toContain(TaskDone.name);
|
|
192
|
+
|
|
193
|
+
await client.close().catch(() => {});
|
|
194
|
+
} finally {
|
|
195
|
+
await niwa.close().catch(() => {});
|
|
196
|
+
await home.cleanup();
|
|
197
|
+
}
|
|
198
|
+
}, 30_000);
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* The records of one type, from the beginning of the journal.
|
|
202
|
+
*
|
|
203
|
+
* The subscription never ends by itself, so the reader takes the records it needs and stops.
|
|
204
|
+
*/
|
|
205
|
+
async function recordsOf(
|
|
206
|
+
client: Awaited<ReturnType<typeof connect>>,
|
|
207
|
+
type: RecordTypeName,
|
|
208
|
+
): Promise<StoredRecord[]> {
|
|
209
|
+
const found: StoredRecord[] = [];
|
|
210
|
+
const stream = client.records.since({ types: [type] }) as AsyncGenerator<StoredRecord>;
|
|
211
|
+
for (let index = 0; index < 10; index += 1) {
|
|
212
|
+
const next = await Promise.race([
|
|
213
|
+
stream.next(),
|
|
214
|
+
Bun.sleep(200).then(() => "timed out" as const),
|
|
215
|
+
]);
|
|
216
|
+
if (next === "timed out" || next.done === true) break;
|
|
217
|
+
found.push(next.value);
|
|
218
|
+
}
|
|
219
|
+
return found;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** Wait for a condition, or say that it did not hold. */
|
|
223
|
+
async function eventually(
|
|
224
|
+
fn: () => boolean | Promise<boolean>,
|
|
225
|
+
timeoutMs = 10_000,
|
|
226
|
+
message?: string,
|
|
227
|
+
): Promise<void> {
|
|
228
|
+
const deadline = Date.now() + timeoutMs;
|
|
229
|
+
for (;;) {
|
|
230
|
+
if (await fn()) return;
|
|
231
|
+
if (Date.now() >= deadline) throw new Error(message ?? "the condition was not met");
|
|
232
|
+
await Bun.sleep(20);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** A host with the tasks module whose clients and handlers all speak as the strategy; tasks are made through the host, which asks in the name of whom it likes. */
|
|
237
|
+
async function withTasks(run: (client: Awaited<ReturnType<typeof connect>>, host: Host) => Promise<void>) {
|
|
238
|
+
const home = await tempHome();
|
|
239
|
+
const niwa = createNiwa({ home: home.path });
|
|
240
|
+
niwa.use(messages({ bind }));
|
|
241
|
+
niwa.use(tasks({ messages: { bind } }));
|
|
242
|
+
await niwa.serve();
|
|
243
|
+
try {
|
|
244
|
+
const client = await connectAsOwner(home.path);
|
|
245
|
+
try {
|
|
246
|
+
await run(client, niwa);
|
|
247
|
+
} finally {
|
|
248
|
+
await client.close().catch(() => {});
|
|
249
|
+
}
|
|
250
|
+
} finally {
|
|
251
|
+
await niwa.close().catch(() => {});
|
|
252
|
+
await home.cleanup();
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** The few words that describe the work. */
|
|
257
|
+
const DESCRIPTION = "Move argument parsing";
|
|
258
|
+
|
|
259
|
+
const contractOf = (responsible: Address = STRATEGY) => ({
|
|
260
|
+
kind: "change",
|
|
261
|
+
description: DESCRIPTION,
|
|
262
|
+
text: TEXT,
|
|
263
|
+
askedBy: ASKER,
|
|
264
|
+
responsible,
|
|
265
|
+
reportTo: REPORTER,
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
test("only the responsible party reports and finishes, a task finishes once, and a task that does not exist is refused", async () => {
|
|
269
|
+
await withTasks(async (client, host) => {
|
|
270
|
+
const t1 = (await host.call(taskCreate, contractOf({ kind: "agent", id: "other" }))).id;
|
|
271
|
+
await expect(client.call(taskReport, { task: t1, text: "hi" })).rejects.toThrow("only the responsible party");
|
|
272
|
+
await expect(client.call(taskFinish, { task: t1, result: 1 })).rejects.toThrow("only the responsible party");
|
|
273
|
+
await expect(client.call(taskFinish, { task: TaskId.parse("task-000000"), result: 1 })).rejects.toThrow("does not exist");
|
|
274
|
+
|
|
275
|
+
const t2 = (await host.call(taskCreate, contractOf())).id;
|
|
276
|
+
await client.call(taskReport, { task: t2, text: "half way" });
|
|
277
|
+
// A finish needs a result and a failure needs a reason: a stale caller that still sends a reason to task.finish fails loudly.
|
|
278
|
+
await expect(client.call("task.finish" as never, { task: t2 } as never)).rejects.toThrow("result");
|
|
279
|
+
await expect(client.call("task.finish" as never, { task: t2, reason: "x" } as never)).rejects.toThrow("result");
|
|
280
|
+
await expect(client.call("task.fail" as never, { task: t2 } as never)).rejects.toThrow("reason");
|
|
281
|
+
await expect(client.call(taskFail, { task: t2, reason: "" })).rejects.toThrow("reason");
|
|
282
|
+
await client.call(taskFail, { task: t2, reason: "broke" });
|
|
283
|
+
await expect(client.call(taskReport, { task: t2, text: "late" })).rejects.toThrow("already done");
|
|
284
|
+
});
|
|
285
|
+
}, 30_000);
|
|
286
|
+
|
|
287
|
+
test("task.result waits for a task that ends later, and loses no end that comes at once", async () => {
|
|
288
|
+
await withTasks(async (client, host) => {
|
|
289
|
+
const late = (await host.call(taskCreate, contractOf())).id;
|
|
290
|
+
const waiting = client.call(taskResult, { task: late });
|
|
291
|
+
await Bun.sleep(100);
|
|
292
|
+
await client.call(taskFinish, { task: late, result: "ok" });
|
|
293
|
+
expect(await waiting).toEqual({ task: late, outcome: { status: "result", result: "ok" } });
|
|
294
|
+
|
|
295
|
+
// The end arrives while the result is being asked for: neither order may hang.
|
|
296
|
+
for (let index = 0; index < 15; index += 1) {
|
|
297
|
+
const id = (await host.call(taskCreate, contractOf())).id;
|
|
298
|
+
const both = await Promise.all([
|
|
299
|
+
client.call(taskResult, { task: id }),
|
|
300
|
+
client.call(taskFinish, { task: id, result: index }),
|
|
301
|
+
]);
|
|
302
|
+
expect(both[0]).toEqual({ task: id, outcome: { status: "result", result: index } });
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
}, 30_000);
|
|
306
|
+
|
|
307
|
+
test("the state of a task is read from the journal: a new host knows it", async () => {
|
|
308
|
+
const home = await tempHome();
|
|
309
|
+
try {
|
|
310
|
+
let kept: TaskId | undefined;
|
|
311
|
+
for (const first of [true, false]) {
|
|
312
|
+
const niwa = createNiwa({ home: home.path });
|
|
313
|
+
niwa.use(messages({ bind }));
|
|
314
|
+
niwa.use(tasks({ messages: { bind } }));
|
|
315
|
+
await niwa.serve();
|
|
316
|
+
const client = await connectAsOwner(home.path);
|
|
317
|
+
try {
|
|
318
|
+
if (first) {
|
|
319
|
+
kept = (await niwa.call(taskCreate, contractOf())).id;
|
|
320
|
+
await client.call(taskFinish, { task: kept, result: 7 });
|
|
321
|
+
} else if (kept !== undefined) {
|
|
322
|
+
expect(await client.call(taskResult, { task: kept })).toEqual({
|
|
323
|
+
task: kept,
|
|
324
|
+
outcome: { status: "result", result: 7 },
|
|
325
|
+
});
|
|
326
|
+
await expect(client.call(taskFinish, { task: kept, result: 8 })).rejects.toThrow("already done");
|
|
327
|
+
}
|
|
328
|
+
} finally {
|
|
329
|
+
await client.close().catch(() => {});
|
|
330
|
+
await niwa.close().catch(() => {});
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
} finally {
|
|
334
|
+
await home.cleanup();
|
|
335
|
+
}
|
|
336
|
+
}, 30_000);
|
|
337
|
+
|
|
338
|
+
// Where a task says to report, that is where the updates and the end go.
|
|
339
|
+
//
|
|
340
|
+
// The scenarios run in one process, against a real host on a temporary home:
|
|
341
|
+
//
|
|
342
|
+
// 1. a task is created with reportTo = owner: the Assignment goes to the responsible address
|
|
343
|
+
// 2. the responsible party reports: the Update goes to the owner
|
|
344
|
+
// 3. the task is finished: the Result goes to the owner
|
|
345
|
+
// 4. the two messages to the owner come in that order, and both carry the task id
|
|
346
|
+
// 5. a task that ends in a failure reports the failure, and a result that is not
|
|
347
|
+
// text is carried along as it is
|
|
348
|
+
// 6. an invocation left unfinished is run again by the next host: the message is
|
|
349
|
+
// the same fact, so it is not sent twice
|
|
350
|
+
|
|
351
|
+
/** The messages the journal holds, in the order they were written. */
|
|
352
|
+
async function sentTo(
|
|
353
|
+
home: string,
|
|
354
|
+
to: { kind: string; id?: string },
|
|
355
|
+
): Promise<Array<{ task?: string; body: { type: string; text: string; data?: unknown } }>> {
|
|
356
|
+
const client = await connectAsOwner(home);
|
|
357
|
+
try {
|
|
358
|
+
const records = await client.records.read({ types: [MessageSent.name] });
|
|
359
|
+
return records
|
|
360
|
+
.map(
|
|
361
|
+
(record) =>
|
|
362
|
+
record.data as {
|
|
363
|
+
to: { kind: string; id?: string };
|
|
364
|
+
task?: string;
|
|
365
|
+
body: { type: string; text: string; data?: unknown };
|
|
366
|
+
},
|
|
367
|
+
)
|
|
368
|
+
.filter((message) => message.to.kind === to.kind && message.to.id === to.id)
|
|
369
|
+
.map((message) => ({ task: message.task, body: message.body }));
|
|
370
|
+
} finally {
|
|
371
|
+
await client.close().catch(() => {});
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
test("mt-2: the updates and the end of a task go to where it says to report", async () => {
|
|
376
|
+
const home = await tempHome();
|
|
377
|
+
const niwa = createNiwa({ home: home.path });
|
|
378
|
+
niwa.use(messages({ bind }));
|
|
379
|
+
niwa.use(tasks({ messages: { bind } }));
|
|
380
|
+
await niwa.serve();
|
|
381
|
+
|
|
382
|
+
try {
|
|
383
|
+
const client = await connectAsOwner(home.path);
|
|
384
|
+
const reported = (await niwa.call(taskCreate, contractOf())).id;
|
|
385
|
+
await client.call(taskReport, { task: reported, text: "half way" });
|
|
386
|
+
await client.call(taskFinish, { task: reported, result: "done" });
|
|
387
|
+
|
|
388
|
+
// The two messages to the owner: the Update first, the Result after it.
|
|
389
|
+
await eventually(
|
|
390
|
+
async () => (await sentTo(home.path, { kind: "owner" })).length >= 2,
|
|
391
|
+
10_000,
|
|
392
|
+
"the owner should have been told about the task",
|
|
393
|
+
);
|
|
394
|
+
const toOwner = await sentTo(home.path, { kind: "owner" });
|
|
395
|
+
expect(toOwner.map((message) => message.body.type)).toEqual(["update", "result"]);
|
|
396
|
+
expect(toOwner[0]?.body.text).toBe("half way");
|
|
397
|
+
expect(toOwner[1]?.body.text).toBe("done");
|
|
398
|
+
// Both carry the task they are about.
|
|
399
|
+
expect(toOwner.map((message) => message.task)).toEqual([reported, reported]);
|
|
400
|
+
|
|
401
|
+
// The Assignment still goes to the responsible address, not to the reporter.
|
|
402
|
+
const toStrategy = await sentTo(home.path, { kind: "strategy", id: "fake" });
|
|
403
|
+
expect(toStrategy.map((message) => message.body.type)).toEqual(["assignment"]);
|
|
404
|
+
|
|
405
|
+
await client.close().catch(() => {});
|
|
406
|
+
} finally {
|
|
407
|
+
await niwa.close().catch(() => {});
|
|
408
|
+
await home.cleanup();
|
|
409
|
+
}
|
|
410
|
+
}, 30_000);
|
|
411
|
+
|
|
412
|
+
test("mt-2: a task that ends in a failure reports the failure", async () => {
|
|
413
|
+
const home = await tempHome();
|
|
414
|
+
const niwa = createNiwa({ home: home.path });
|
|
415
|
+
niwa.use(messages({ bind }));
|
|
416
|
+
niwa.use(tasks({ messages: { bind } }));
|
|
417
|
+
await niwa.serve();
|
|
418
|
+
|
|
419
|
+
try {
|
|
420
|
+
const client = await connectAsOwner(home.path);
|
|
421
|
+
const failed = (await niwa.call(taskCreate, contractOf())).id;
|
|
422
|
+
await client.call(taskFail, { task: failed, reason: "it broke" });
|
|
423
|
+
|
|
424
|
+
await eventually(
|
|
425
|
+
async () => (await sentTo(home.path, { kind: "owner" })).length >= 1,
|
|
426
|
+
10_000,
|
|
427
|
+
"the owner should have been told the task failed",
|
|
428
|
+
);
|
|
429
|
+
const toOwner = await sentTo(home.path, { kind: "owner" });
|
|
430
|
+
expect(toOwner[0]?.body.type).toBe("failure");
|
|
431
|
+
expect(toOwner[0]?.body.text).toBe("it broke");
|
|
432
|
+
|
|
433
|
+
await client.close().catch(() => {});
|
|
434
|
+
} finally {
|
|
435
|
+
await niwa.close().catch(() => {});
|
|
436
|
+
await home.cleanup();
|
|
437
|
+
}
|
|
438
|
+
}, 30_000);
|
|
439
|
+
|
|
440
|
+
test("mt-2: a result that is not text is carried along as it is", async () => {
|
|
441
|
+
const home = await tempHome();
|
|
442
|
+
const niwa = createNiwa({ home: home.path });
|
|
443
|
+
niwa.use(messages({ bind }));
|
|
444
|
+
niwa.use(tasks({ messages: { bind } }));
|
|
445
|
+
await niwa.serve();
|
|
446
|
+
|
|
447
|
+
try {
|
|
448
|
+
const client = await connectAsOwner(home.path);
|
|
449
|
+
const structured = (await niwa.call(taskCreate, contractOf())).id;
|
|
450
|
+
await client.call(taskFinish, { task: structured, result: { files: 2 } });
|
|
451
|
+
|
|
452
|
+
await eventually(
|
|
453
|
+
async () => (await sentTo(home.path, { kind: "owner" })).length >= 1,
|
|
454
|
+
10_000,
|
|
455
|
+
"the owner should have been told how the task ended",
|
|
456
|
+
);
|
|
457
|
+
const toOwner = await sentTo(home.path, { kind: "owner" });
|
|
458
|
+
expect(toOwner[0]?.body.type).toBe("result");
|
|
459
|
+
// The result is carried as it is, and the text says what it is.
|
|
460
|
+
expect(toOwner[0]?.body.data).toEqual({ files: 2 });
|
|
461
|
+
|
|
462
|
+
await client.close().catch(() => {});
|
|
463
|
+
} finally {
|
|
464
|
+
await niwa.close().catch(() => {});
|
|
465
|
+
await home.cleanup();
|
|
466
|
+
}
|
|
467
|
+
}, 30_000);
|
|
468
|
+
|
|
469
|
+
test("mt-2: a replay after a restart sends nothing twice", async () => {
|
|
470
|
+
const home = await tempHome();
|
|
471
|
+
// The guard holds the message of the first host inside its step: the invocation of
|
|
472
|
+
// the handler stays unfinished, so the second host runs it again.
|
|
473
|
+
let holding = true;
|
|
474
|
+
let entered = 0;
|
|
475
|
+
const held = new Promise<void>(() => {});
|
|
476
|
+
const guard = (): Plugin =>
|
|
477
|
+
plugin("report-guard", (api) => {
|
|
478
|
+
api.guard(messageSend(), async (input) => {
|
|
479
|
+
if (input.body.type !== "update") return;
|
|
480
|
+
entered += 1;
|
|
481
|
+
if (holding) await held;
|
|
482
|
+
});
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
const first = createNiwa({ home: home.path });
|
|
486
|
+
first.use(messages({ bind }));
|
|
487
|
+
first.use(tasks({ messages: { bind } }));
|
|
488
|
+
first.use(guard());
|
|
489
|
+
await first.serve();
|
|
490
|
+
|
|
491
|
+
try {
|
|
492
|
+
const client = await connectAsOwner(home.path);
|
|
493
|
+
const again = (await first.call(taskCreate, contractOf())).id;
|
|
494
|
+
await client.call(taskReport, { task: again, text: "half way" });
|
|
495
|
+
|
|
496
|
+
// The handler of the report is at its step, waiting for the guard.
|
|
497
|
+
await eventually(() => entered >= 1, 10_000, "the handler should have reached its step");
|
|
498
|
+
await client.close().catch(() => {});
|
|
499
|
+
await first.close().catch(() => {});
|
|
500
|
+
|
|
501
|
+
// The second host reads the same journal and runs the unfinished invocation
|
|
502
|
+
// again: the message is the same fact, so it is not written twice.
|
|
503
|
+
holding = false;
|
|
504
|
+
const second = createNiwa({ home: home.path });
|
|
505
|
+
second.use(messages({ bind }));
|
|
506
|
+
second.use(tasks({ messages: { bind } }));
|
|
507
|
+
second.use(guard());
|
|
508
|
+
await second.serve();
|
|
509
|
+
try {
|
|
510
|
+
// The handler ran again: the step was reached a second time.
|
|
511
|
+
await eventually(() => entered >= 2, 10_000, "the handler should have run again");
|
|
512
|
+
const client2 = await connectAsOwner(home.path);
|
|
513
|
+
await client2.call(taskFinish, { task: again, result: "done" });
|
|
514
|
+
await eventually(
|
|
515
|
+
async () => (await sentTo(home.path, { kind: "owner" })).length >= 2,
|
|
516
|
+
10_000,
|
|
517
|
+
"the owner should have been told how the task ended",
|
|
518
|
+
);
|
|
519
|
+
await client2.close().catch(() => {});
|
|
520
|
+
|
|
521
|
+
// The owner heard about the update once and about the end once.
|
|
522
|
+
const toOwner = await sentTo(home.path, { kind: "owner" });
|
|
523
|
+
expect(toOwner.map((message) => message.body.type)).toEqual(["update", "result"]);
|
|
524
|
+
expect(toOwner[0]?.body.text).toBe("half way");
|
|
525
|
+
expect(toOwner[1]?.body.text).toBe("done");
|
|
526
|
+
// The Assignment went to the responsible address once, as before.
|
|
527
|
+
const toStrategy = await sentTo(home.path, { kind: "strategy", id: "fake" });
|
|
528
|
+
expect(toStrategy.map((message) => message.body.type)).toEqual(["assignment"]);
|
|
529
|
+
} finally {
|
|
530
|
+
await second.close().catch(() => {});
|
|
531
|
+
}
|
|
532
|
+
} finally {
|
|
533
|
+
await first.close().catch(() => {});
|
|
534
|
+
await home.cleanup();
|
|
535
|
+
}
|
|
536
|
+
}, 30_000);
|