-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathexecute.test.ts
More file actions
405 lines (319 loc) · 8.62 KB
/
execute.test.ts
File metadata and controls
405 lines (319 loc) · 8.62 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import path from 'node:path';
import test from 'ava';
import { createMockLogger } from '@openfn/logger';
import initWorkers from '../../src/api/call-worker';
import execute from '../../src/api/execute';
import {
JOB_COMPLETE,
JOB_START,
WORKFLOW_COMPLETE,
WORKFLOW_ERROR,
WORKFLOW_LOG,
WORKFLOW_START,
} from '../../src/events';
import ExecutionContext from '../../src/classes/ExecutionContext';
import type {
ExecuteOptions,
ExecutionContextOptions,
WorkflowState,
} from '../../src/types';
import type { EngineOptions } from '../../src/engine';
const workerPath = path.resolve('dist/test/mock-run.js');
const createContext = ({
state,
options,
}: {
state: Partial<WorkflowState>;
options: Partial<ExecutionContextOptions>;
}) => {
const logger = createMockLogger();
const { callWorker } = initWorkers(workerPath, {}, logger);
const ctx = new ExecutionContext({
// @ts-ignore
state: state || { workflowId: 'x' },
logger,
callWorker,
// @ts-ignore
options,
});
ctx.callWorker = callWorker;
return ctx;
};
const plan = {
id: 'x',
workflow: {
steps: [
{
id: 'j',
expression: '() => 22',
},
],
},
options: {},
};
const options = {
autoinstall: {
handleInstall: async () => {},
handleIsInstalled: async () => false,
},
} as Partial<EngineOptions>;
test.serial('execute should run a job and return the result', async (t) => {
const state = {
id: 'x',
plan,
} as Partial<WorkflowState>;
const context = createContext({ state, options });
const result = await execute(context);
t.is(result, 22);
});
// we can check the state object after each of these is returned
test.serial('should emit a workflow-start event', async (t) => {
const state = {
id: 'x',
plan,
} as WorkflowState;
let workflowStart;
const context = createContext({ state, options });
context.once(WORKFLOW_START, (evt) => (workflowStart = evt));
await execute(context);
// No need to do a deep test of the event payload here
t.is(workflowStart!.workflowId!, 'x');
// Just a shallow test on the actual version object to verify that it's been attached
t.truthy(workflowStart!.versions);
t.regex(workflowStart!.versions.node, new RegExp(/(\d+).(\d+).\d+/));
});
test.serial('should emit a log event with the memory limit', async (t) => {
const state = {
id: 'x',
plan,
} as WorkflowState;
const logs: any[] = [];
const context = createContext({
state,
options: {
...options,
memoryLimitMb: 666,
},
});
context.on(WORKFLOW_LOG, (evt) => {
logs.push(evt);
});
await execute(context);
const log = logs.find(({ name }) => name === 'RTE');
t.is(log.message[0], 'Memory limit: 666mb');
});
test.serial('should emit a workflow-complete event', async (t) => {
let workflowComplete;
const state = {
id: 'x',
plan,
} as WorkflowState;
const context = createContext({ state, options });
context.once(WORKFLOW_COMPLETE, (evt) => (workflowComplete = evt));
await execute(context);
t.is(workflowComplete!.workflowId, 'x');
t.is(workflowComplete!.state, 22);
});
test.serial('should emit a job-start event', async (t) => {
const state = {
id: 'x',
plan,
} as WorkflowState;
let event: any;
const context = createContext({ state, options });
context.once(JOB_START, (evt) => (event = evt));
await execute(context);
t.is(event.jobId, 'j');
});
test.serial('should emit a job-complete event', async (t) => {
const state = {
id: 'x',
plan,
} as WorkflowState;
let event: any;
const context = createContext({ state, options });
context.once(JOB_COMPLETE, (evt) => (event = evt));
await execute(context);
t.is(event.jobId, 'j');
t.is(event.state, 22);
t.assert(!isNaN(event.duration));
});
test.serial.only('should emit a log event', async (t) => {
let workflowLog: any;
const plan = {
id: 'y',
workflow: {
steps: [
{
expression: '() => { console.log("hi"); return 33 }',
},
],
},
options: {},
};
const state = {
id: 'y',
plan,
} as Partial<WorkflowState>;
const context = createContext({ state, options });
context.once(WORKFLOW_LOG, (evt) => (workflowLog = evt));
await execute(context);
t.is(workflowLog.workflowId, 'y');
t.is(workflowLog.level, 'info');
t.deepEqual(workflowLog.message, JSON.stringify(['hi']));
});
test.serial('log events are timestamped in hr time', async (t) => {
let workflowLog: any;
const plan = {
id: 'y',
workflow: {
steps: [
{
expression: '() => { console.log("hi"); return 33 }',
},
],
},
};
const state = {
id: 'y',
plan,
} as WorkflowState;
const context = createContext({ state, options });
context.once(WORKFLOW_LOG, (evt) => (workflowLog = evt));
await execute(context);
const { time } = workflowLog;
// Note: The time we get here is NOT a bigint because it's been serialized
t.true(typeof time === 'string');
t.is(time.length, 19);
});
test.serial('should emit error on timeout', async (t) => {
const state = {
id: 'zz',
plan: {
workflow: {
steps: [
{
expression: '() => { while(true) {} }',
},
],
},
},
} as WorkflowState;
const wfOptions: ExecuteOptions = {
...options,
runTimeoutMs: 10,
};
let event: any;
const context = createContext({ state, options: wfOptions });
context.once(WORKFLOW_ERROR, (evt) => (event = evt));
await execute(context);
t.truthy(event.threadId);
t.is(event.type, 'TimeoutError');
t.regex(event.message, /failed to return within 10ms/);
});
test.serial(
'should emit ExecutionError if something unexpected throws',
async (t) => {
const state = {
id: 'baa',
plan: {},
} as WorkflowState;
const context = createContext({ state, options });
context.once(WORKFLOW_ERROR, (evt) => {
t.is(evt.workflowId, state.id);
// This occured in the main thread, good to know!
t.is(evt.threadId, '-');
t.is(evt.type, 'ExecutionError');
t.regex(
evt.message,
/Cannot read properties of undefined \(reading 'repoDir'\)|Cannot destructure property \'repoDir\' of \'options\' as it is undefined\./
);
t.pass('error thrown');
});
// @ts-ignore
delete context.options; // this will make it throw, poor little guy
await execute(context);
}
);
test.serial('should emit CompileError if compilation fails', async (t) => {
const state = {
id: 'baa',
plan: {
workflow: {
steps: [{ id: 'j', expression: 'la la la' }],
},
},
} as WorkflowState;
const context = createContext({ state, options: {} });
context.once(WORKFLOW_ERROR, (evt) => {
t.is(evt.workflowId, state.id);
t.true(typeof evt.threadId === 'number');
t.is(evt.type, 'CompileError');
t.is(evt.message, "j: Unexpected identifier 'la'");
t.pass('error thrown');
});
await execute(context);
});
test.serial(
'on compile error, the error log should arrive before the workflow-error event',
async (t) => {
const state = {
id: 'compile-order',
plan: {
workflow: {
steps: [{ id: 'j', expression: 'la la la' }],
},
},
} as WorkflowState;
const context = createContext({ state, options: {} });
const orderedEvents: string[] = [];
context.on(WORKFLOW_LOG, (evt) => {
if (/error occurred during compilation/i.test(evt.message)) {
orderedEvents.push('log');
}
});
context.on(WORKFLOW_ERROR, () => {
orderedEvents.push('error');
});
await execute(context);
t.deepEqual(orderedEvents, ['log', 'error']);
}
);
test.serial('should stringify the whitelist array', async (t) => {
let passedOptions: any;
const state = {
id: 'x',
plan,
} as WorkflowState;
const opts = {
...options,
whitelist: [/abc/],
};
const context = createContext({ state, options: opts });
// @ts-ignore
context.callWorker = (_command, args) => {
passedOptions = args[2];
};
await execute(context);
t.truthy(passedOptions);
t.deepEqual(passedOptions.whitelist, ['/abc/']);
});
test.serial('should forward the jobLogLevel option', async (t) => {
let passedOptions: any;
const state = {
id: 'x',
plan,
} as WorkflowState;
const opts = {
...options,
jobLogLevel: 'none',
};
const context = createContext({ state, options: opts });
// @ts-ignore
context.callWorker = (_command, args) => {
passedOptions = args[2];
};
await execute(context);
t.truthy(passedOptions);
t.deepEqual(passedOptions.jobLogLevel, 'none');
});