@fincity/kirun-js 3.5.0 → 3.7.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,140 @@
1
+ import {
2
+ FunctionDefinition,
3
+ FunctionExecutionParameters,
4
+ KIRunFunctionRepository,
5
+ KIRunSchemaRepository,
6
+ KIRuntime,
7
+ } from '../../../src';
8
+
9
+ /**
10
+ * Regression test for the runaway-execution guard.
11
+ *
12
+ * The counter used to be incremented only when a queue size changed, which disabled the guard
13
+ * in exactly the case it exists for: a graph making no progress leaves the queue sizes
14
+ * untouched, so the counter never advanced and executeGraph could spin forever. The check was
15
+ * also `==` rather than `>=`, so a skipped boundary value would never trip it.
16
+ */
17
+
18
+ // A CountLoop with a step in its body, so execution takes many statement iterations.
19
+ const loopingDefinition = {
20
+ name: 'loops',
21
+ namespace: 'TestUI',
22
+ steps: {
23
+ loop: {
24
+ statementName: 'loop',
25
+ name: 'CountLoop',
26
+ namespace: 'System.Loop',
27
+ parameterMap: {
28
+ count: {
29
+ one: {
30
+ key: 'one',
31
+ type: 'VALUE',
32
+ value: 40,
33
+ },
34
+ },
35
+ },
36
+ },
37
+ printer: {
38
+ statementName: 'printer',
39
+ name: 'Print',
40
+ namespace: 'System',
41
+ dependentStatements: {
42
+ 'Steps.loop.iteration': true,
43
+ },
44
+ parameterMap: {
45
+ values: {
46
+ one: {
47
+ key: 'one',
48
+ type: 'EXPRESSION',
49
+ expression: 'Steps.loop.iteration.index',
50
+ },
51
+ },
52
+ },
53
+ },
54
+ },
55
+ };
56
+
57
+ function run(): Promise<any> {
58
+ const runtime = new KIRuntime(FunctionDefinition.from(loopingDefinition));
59
+
60
+ return runtime.execute(
61
+ new FunctionExecutionParameters(
62
+ new KIRunFunctionRepository(),
63
+ new KIRunSchemaRepository(),
64
+ ).setArguments(new Map()),
65
+ );
66
+ }
67
+
68
+ describe('KIRuntime execution iteration guard', () => {
69
+ const originalMax = KIRuntime.MAX_EXECUTION_ITERATIONS;
70
+
71
+ afterEach(() => {
72
+ KIRuntime.MAX_EXECUTION_ITERATIONS = originalMax;
73
+ });
74
+
75
+ test('default cap is 1,000,000 and is tunable', () => {
76
+ expect(originalMax).toBe(1000000);
77
+ });
78
+
79
+ test('a loop that exceeds the cap is stopped', async () => {
80
+ // Low enough that a 40-pass loop is guaranteed to cross it. If the counter were still
81
+ // gated on a queue-size change this would run to completion instead of throwing.
82
+ KIRuntime.MAX_EXECUTION_ITERATIONS = 5;
83
+
84
+ await expect(run()).rejects.toThrow(/Execution locked in an infinite loop/);
85
+ });
86
+
87
+ test('the same loop completes when the cap is not exceeded', async () => {
88
+ KIRuntime.MAX_EXECUTION_ITERATIONS = originalMax;
89
+
90
+ await expect(run()).resolves.toBeDefined();
91
+ });
92
+
93
+ // A linear chain pops one vertex and pushes its successor, so the queue size is unchanged
94
+ // across passes even though real work happened. Under the old "only count when a queue size
95
+ // changed" rule the counter stayed at zero here, which is precisely why a stuck graph could
96
+ // never trip the guard. Counting must now advance.
97
+ test('counts passes even when the queue size never changes', async () => {
98
+ const linearChain = {
99
+ name: 'linear',
100
+ namespace: 'TestUI',
101
+ steps: {
102
+ first: {
103
+ statementName: 'first',
104
+ name: 'Print',
105
+ namespace: 'System',
106
+ parameterMap: {
107
+ values: { one: { key: 'one', type: 'VALUE', value: 'a' } },
108
+ },
109
+ },
110
+ second: {
111
+ statementName: 'second',
112
+ name: 'Print',
113
+ namespace: 'System',
114
+ dependentStatements: { 'Steps.first.output': true },
115
+ parameterMap: {
116
+ values: { one: { key: 'one', type: 'VALUE', value: 'b' } },
117
+ },
118
+ },
119
+ third: {
120
+ statementName: 'third',
121
+ name: 'Print',
122
+ namespace: 'System',
123
+ dependentStatements: { 'Steps.second.output': true },
124
+ parameterMap: {
125
+ values: { one: { key: 'one', type: 'VALUE', value: 'c' } },
126
+ },
127
+ },
128
+ },
129
+ };
130
+
131
+ const params = new FunctionExecutionParameters(
132
+ new KIRunFunctionRepository(),
133
+ new KIRunSchemaRepository(),
134
+ ).setArguments(new Map());
135
+
136
+ await new KIRuntime(FunctionDefinition.from(linearChain)).execute(params);
137
+
138
+ expect(params.getCount()).toBeGreaterThanOrEqual(3);
139
+ });
140
+ });