Skip to content

Commit

Permalink
Merge pull request #474 from OpenFn/worker-integration-tests
Browse files Browse the repository at this point in the history
Worker integration tests
  • Loading branch information
josephjclark authored Nov 14, 2023
2 parents 9bca088 + bef8eee commit a03ab3e
Show file tree
Hide file tree
Showing 12 changed files with 269 additions and 389 deletions.
191 changes: 0 additions & 191 deletions integration-tests/cli/test/execute-job.test.ts

This file was deleted.

3 changes: 0 additions & 3 deletions integration-tests/cli/test/fixtures/chuck.js

This file was deleted.

1 change: 1 addition & 0 deletions integration-tests/cli/test/fixtures/http.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
get('https://jsonplaceholder.typicode.com/todos/1');
36 changes: 36 additions & 0 deletions integration-tests/worker/src/init.ts
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 };
};
55 changes: 55 additions & 0 deletions integration-tests/worker/test/exit-reasons.test.ts
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\)$/);
});
Loading

0 comments on commit a03ab3e

Please sign in to comment.