-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.test.ts
More file actions
98 lines (88 loc) · 2.63 KB
/
task.test.ts
File metadata and controls
98 lines (88 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { describe, test, expect } from "vitest";
import { leaf, sequence, parallel, leafChainOf } from "./task";
import { retry, timeout } from "./sample-strategy";
describe("Task DSL", () => {
test("basic sequence and parallel execution", async () => {
const program = leafChainOf(
sequence("rootask", [
sequence("task1_1", [
leaf("task2_1", async (i: string) => i + "task2_1"),
parallel("task2_2", [
leaf("p1", async (i: string) => i + "p1"),
leaf("p2", async (i: string) => i + "p2"),
]),
]),
leaf("task1_2", async (i: object) => JSON.stringify(i) + "task1_2"),
])
);
const result = await program("start: ");
expect(result).toBe(
'{"p1":"start: task2_1p1","p2":"start: task2_1p2"}task1_2'
);
});
test("retry strategy - should retry failed tasks", async () => {
let attemptCount = 0;
const program = leafChainOf(
sequence(
"root",
[
leaf("failing-task", async (i: string) => {
attemptCount++;
if (attemptCount < 3) {
throw new Error("Simulated failure");
}
return i + " success!";
}),
],
retry(3)
)
);
const result = await program("retry-test:");
expect(result).toBe("retry-test: success!");
expect(attemptCount).toBe(3);
});
test("timeout strategy - should timeout slow tasks", async () => {
const program = leafChainOf(
sequence(
"root",
[
leaf("slow-task", async (i: string) => {
await new Promise((resolve) => setTimeout(resolve, 2000));
return i + " completed";
}),
],
timeout(1000)
)
);
await expect(program("timeout-test:")).rejects.toThrow("Task timeout");
});
test("nested strategy - should apply retry only to inner sequence", async () => {
let nestedAttempt = 0;
const program = leafChainOf(
sequence("outer", [
leaf("task1", async (i: string) => {
return i + "-t1";
}),
sequence(
"inner-with-retry",
[
leaf("task2-fail", async (i: string) => {
nestedAttempt++;
if (nestedAttempt < 2) {
throw new Error("Inner task failed");
}
return i + "-t2";
}),
],
retry(3)
),
leaf("task3", async (i: string) => {
return i + "-t3";
}),
])
);
const result = await program("nested:");
expect(result).toBe("nested:-t1-t2-t3");
expect(nestedAttempt).toBe(2);
});
});