-
Notifications
You must be signed in to change notification settings - Fork 11
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 #474 from OpenFn/worker-integration-tests
Worker integration tests
- Loading branch information
Showing
12 changed files
with
269 additions
and
389 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 @@ | ||
get('https://jsonplaceholder.typicode.com/todos/1'); |
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,36 @@ | ||
import path from 'node:path'; | ||
import crypto from 'node:crypto'; | ||
|
||
import createLightningServer from '@openfn/lightning-mock'; | ||
import createEngine from '@openfn/engine-multi'; | ||
import createWorkerServer from '@openfn/ws-worker'; | ||
import { createMockLogger } from '@openfn/logger'; | ||
|
||
export const randomPort = () => parseInt(2000 + Math.random() * 1000); | ||
|
||
export const initLightning = (port = 4000) => { | ||
// TODO the lightning mock right now doesn't use the secret | ||
// but we may want to add tests against this | ||
return createLightningServer({ port }); | ||
}; | ||
|
||
export const initWorker = async (lightningPort, engineArgs = {}) => { | ||
const workerPort = randomPort(); | ||
|
||
const engine = await createEngine({ | ||
// logger: createLogger('engine', { level: 'debug' }), | ||
logger: createMockLogger(), | ||
repoDir: path.resolve('./tmp/repo/default'), | ||
...engineArgs, | ||
}); | ||
|
||
const worker = createWorkerServer(engine, { | ||
logger: createMockLogger(), | ||
// logger: createLogger('worker', { level: 'debug' }), | ||
port: workerPort, | ||
lightning: `ws://localhost:${lightningPort}/worker`, | ||
secret: crypto.randomUUID(), | ||
}); | ||
|
||
return { engine, worker }; | ||
}; |
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,55 @@ | ||
import test from 'ava'; | ||
import crypto from 'node:crypto'; | ||
import path from 'node:path'; | ||
|
||
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/openfn/repo/exit-reason'), | ||
})); | ||
}); | ||
|
||
test.after(async () => { | ||
lightning.destroy(); | ||
await worker.destroy(); | ||
}); | ||
|
||
const run = async (attempt) => { | ||
return new Promise<any>(async (done) => { | ||
lightning.on('attempt:complete', (evt) => { | ||
if (attempt.id === evt.attemptId) { | ||
done(evt.payload); | ||
} | ||
}); | ||
|
||
lightning.enqueueAttempt(attempt); | ||
}); | ||
}; | ||
|
||
test('crash: syntax error', async (t) => { | ||
const attempt = { | ||
id: crypto.randomUUID(), | ||
jobs: [ | ||
{ | ||
adaptor: '@openfn/language-common@latest', | ||
body: 'fn(() => throw "e")', | ||
}, | ||
], | ||
}; | ||
|
||
const result = await run(attempt); | ||
|
||
const { reason, error_type, error_message } = result; | ||
|
||
t.is(reason, 'crash'); | ||
t.is(error_type, 'CompileError'); | ||
t.regex(error_message, /Unexpected token \(1:9\)$/); | ||
}); |
Oops, something went wrong.