-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #484 from OpenFn/parallel-state
Fix input_data_clip id
- Loading branch information
Showing
40 changed files
with
1,540 additions
and
242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,18 @@ | ||
{ | ||
"name": "@openfn/integration-tests-worker", | ||
"private": true, | ||
"version": "1.0.15", | ||
"version": "1.0.16", | ||
"description": "Lightning WOrker integration tests", | ||
"author": "Open Function Group <[email protected]>", | ||
"license": "ISC", | ||
"type": "module", | ||
"scripts": { | ||
"clean": "rimraf dist tmp/repo/*", | ||
"clean": "rimraf dist tmp/repo/**", | ||
"build:pack": "pnpm clean && cd ../.. && pnpm pack:local integration-tests/worker/dist --no-version", | ||
"build": "pnpm build:pack && docker build --tag worker-integration-tests .", | ||
"start": "docker run worker-integration-tests", | ||
"test": "pnpm clean && npx ava -s --timeout 2m" | ||
"test": "pnpm clean && npx ava -s --timeout 2m", | ||
"test:cache": "npx ava -s --timeout 2m" | ||
}, | ||
"dependencies": { | ||
"@openfn/engine-multi": "workspace:^", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import crypto from 'node:crypto'; | ||
|
||
export const createAttempt = (triggers, jobs, edges, args) => ({ | ||
id: crypto.randomUUID(), | ||
triggers, | ||
jobs, | ||
edges, | ||
...args, | ||
}); | ||
|
||
export const createJob = (args) => ({ | ||
id: crypto.randomUUID(), | ||
adaptor: '@openfn/language-common@latest', | ||
body: 'fn((s) => s)', | ||
...args, | ||
}); | ||
|
||
export const createTrigger = () => ({ | ||
id: crypto.randomUUID(), | ||
}); | ||
|
||
export const createEdge = (a: any, b: any, condition?: string) => { | ||
const edge: any = { | ||
id: crypto.randomUUID(), | ||
target_job_id: b.id, | ||
}; | ||
if (!a.body) { | ||
edge.source_trigger_id = a.id; | ||
} else { | ||
edge.source_job_id = a.id; | ||
} | ||
return edge; | ||
}; | ||
|
||
export default createAttempt; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
import test from 'ava'; | ||
import path from 'node:path'; | ||
|
||
import { | ||
createAttempt, | ||
createEdge, | ||
createJob, | ||
createTrigger, | ||
} from '../src/factories'; | ||
import { initLightning, initWorker } from '../src/init'; | ||
|
||
let lightning; | ||
let worker; | ||
|
||
test.before(async () => { | ||
const lightningPort = 4321; | ||
|
||
lightning = initLightning(lightningPort); | ||
|
||
({ worker } = await initWorker(lightningPort, { | ||
repoDir: path.resolve('tmp/repo/attempts'), | ||
})); | ||
}); | ||
|
||
test.after(async () => { | ||
lightning.destroy(); | ||
await worker.destroy(); | ||
}); | ||
|
||
const run = async (attempt) => { | ||
return new Promise<any>(async (done, reject) => { | ||
lightning.once('attempt:complete', (evt) => { | ||
if (attempt.id === evt.attemptId) { | ||
done(lightning.getResult(attempt.id)); | ||
} else { | ||
// If we get here, something has gone very wrong | ||
reject('attempt not found'); | ||
} | ||
}); | ||
|
||
lightning.enqueueAttempt(attempt); | ||
}); | ||
}; | ||
|
||
test('echo initial state', async (t) => { | ||
const initialState = { data: { count: 22 } }; | ||
|
||
lightning.addDataclip('s1', initialState); | ||
|
||
const job = createJob({ body: 'fn((s) => s)' }); | ||
const attempt = createAttempt([], [job], [], { | ||
dataclip_id: 's1', | ||
}); | ||
|
||
const result = await run(attempt); | ||
|
||
t.deepEqual(result, { | ||
data: { | ||
count: 22, | ||
}, | ||
}); | ||
}); | ||
|
||
test('start from a trigger node', async (t) => { | ||
let runStartEvent; | ||
let runCompleteEvent; | ||
|
||
const initialState = { data: { count: 22 } }; | ||
|
||
lightning.addDataclip('s1', initialState); | ||
|
||
const trigger = createTrigger(); | ||
const job = createJob({ body: 'fn((s) => s)' }); | ||
const edge = createEdge(trigger, job); | ||
const attempt = createAttempt([trigger], [job], [edge], { | ||
dataclip_id: 's1', | ||
}); | ||
|
||
lightning.once('run:start', (evt) => { | ||
runStartEvent = evt.payload; | ||
}); | ||
|
||
lightning.once('run:complete', (evt) => { | ||
runCompleteEvent = evt.payload; | ||
}); | ||
|
||
await run(attempt); | ||
|
||
t.truthy(runStartEvent); | ||
t.is(runStartEvent.job_id, job.id); | ||
t.truthy(runStartEvent.run_id); | ||
t.is(runStartEvent.input_dataclip_id, 's1'); | ||
|
||
t.truthy(runCompleteEvent); | ||
t.is(runCompleteEvent.reason, 'success'); | ||
t.is(runCompleteEvent.error_message, null); | ||
t.is(runCompleteEvent.error_type, null); | ||
t.is(runCompleteEvent.job_id, job.id); | ||
t.truthy(runCompleteEvent.output_dataclip_id); | ||
t.is(runCompleteEvent.output_dataclip, JSON.stringify(initialState)); | ||
}); | ||
|
||
// hmm this event feels a bit fine-grained for this | ||
// This file should just be about input-output | ||
// TODO maybe move it into integrations later | ||
test('run parallel jobs', async (t) => { | ||
const initialState = { data: { count: 22 } }; | ||
|
||
lightning.addDataclip('s1', initialState); | ||
|
||
/* | ||
[a] | ||
/ \ | ||
[x] [y] | ||
*/ | ||
const a = createJob({ body: 'fn((s) => ({ data: { a: true }}))' }); | ||
const x = createJob({ body: 'fn((s) => { s.data.x = true; return s; })' }); | ||
const y = createJob({ body: 'fn((s) => { s.data.y = true; return s; })' }); | ||
const ax = createEdge(a, x); | ||
const ay = createEdge(a, y); | ||
const jobs = [a, x, y]; | ||
const edges = [ax, ay]; | ||
|
||
const attempt = createAttempt([], jobs, edges, { | ||
dataclip_id: 's1', | ||
}); | ||
|
||
// This saves JSON returned by a job | ||
const outputJson = {}; | ||
|
||
// This saves the dataclip returned by a job | ||
const outputId = {}; | ||
|
||
lightning.on('run:start', (evt) => { | ||
// x and y should both be passed the dataclip produced by job a | ||
if (evt.payload.run_id === x.id || evt.payload.run_id === y.id) { | ||
evt.payload.input_dataclip_id = outputId[a.id]; | ||
} | ||
}); | ||
|
||
lightning.on('run:complete', (evt) => { | ||
// save the output dataclip | ||
outputJson[evt.payload.job_id] = evt.payload.output_dataclip_id; | ||
outputJson[evt.payload.job_id] = JSON.parse(evt.payload.output_dataclip); | ||
}); | ||
|
||
const result = await run(attempt); | ||
|
||
t.deepEqual(outputJson[x.id].data, { | ||
a: true, | ||
x: true, | ||
// Should not include a write from y | ||
}); | ||
t.deepEqual(outputJson[y.id].data, { | ||
a: true, | ||
y: true, | ||
// Should not include a write from x | ||
}); | ||
|
||
// I think the result should look like this - but it won't without work | ||
// t.deepEqual(result, { | ||
// [x.id]: { | ||
// a: true, | ||
// x: true, | ||
// }, | ||
// [y.id]: { | ||
// a: true, | ||
// y: true, | ||
// }, | ||
// }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.