Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions integration-tests/worker/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import crypto from 'node:crypto';

import createLightningServer, { toBase64 } from '@openfn/lightning-mock';
import createEngine from '@openfn/engine-multi';
import createWorkerServer from '@openfn/ws-worker';
import createWorkerServer, { INTERNAL_SOCKET_READY } from '@openfn/ws-worker';
import { createMockLogger } from '@openfn/logger';
import createLogger from '@openfn/logger';

const debugWorker = process.env.OPENFN_DEBUG_WORKER;
const debugLightning = process.env.OPENFN_DEBUG_LIGHTNING;

export const randomPort = () => Math.round(2000 + Math.random() * 1000);
let nextPortNumber = 4400;

export const nextPort = () => nextPortNumber++;

export const initLightning = (port = 4000, privateKey?: string) => {
// TODO the lightning mock right now doesn't use the secret
Expand All @@ -30,7 +32,7 @@ export const initWorker = async (
engineArgs = {},
workerArgs = {}
) => {
const workerPort = randomPort();
const workerPort = nextPort();

const engineLogger = createMockLogger('engine', {
level: 'debug',
Expand All @@ -56,5 +58,18 @@ export const initWorker = async (
...workerArgs,
});

if (!worker.socket) {
await new Promise<void>((resolve, reject) => {
const timeout = setTimeout(() => {
reject(new Error('worker did not connect to lightning within 10s'));
}, 10_000);

worker.events.once(INTERNAL_SOCKET_READY, () => {
clearTimeout(timeout);
resolve();
});
});
}

return { engine, engineLogger, worker };
};
4 changes: 2 additions & 2 deletions integration-tests/worker/test/autoinstall.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'node:path';
import { rm } from 'node:fs/promises';
import { generateKeys } from '@openfn/lightning-mock';

import { initLightning, initWorker } from '../src/init';
import { initLightning, initWorker, nextPort } from '../src/init';
import { createRun, createJob } from '../src/factories';

const generate = (adaptor, version) => {
Expand Down Expand Up @@ -39,7 +39,7 @@ test.before(async () => {
} catch (e) {}

const keys = await generateKeys();
const lightningPort = 4321;
const lightningPort = nextPort();

lightning = initLightning(lightningPort, keys.private);

Expand Down
4 changes: 2 additions & 2 deletions integration-tests/worker/test/benchmark.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import test from 'ava';
import path from 'node:path';

import { createRun } from '../src/factories';
import { initLightning, initWorker } from '../src/init';
import { initLightning, initWorker, nextPort } from '../src/init';
import { run, humanMb } from '../src/util';

let lightning;
Expand All @@ -11,7 +11,7 @@ let worker;
const maxConcurrency = 20;

test.before(async () => {
const lightningPort = 4322;
const lightningPort = nextPort();

lightning = initLightning(lightningPort);

Expand Down
8 changes: 4 additions & 4 deletions integration-tests/worker/test/exit-reasons.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import test from 'ava';
import crypto from 'node:crypto';
import path from 'node:path';

import { initLightning, initWorker } from '../src/init';
import { initLightning, initWorker, nextPort } from '../src/init';

let lightning;
let worker;

test.before(async () => {
const lightningPort = 4321;
const lightningPort = nextPort();

lightning = initLightning(lightningPort);

Expand All @@ -23,8 +23,8 @@ test.after(async () => {
});

const run = async (attempt) => {
return new Promise<any>(async (done) => {
lightning.once('run:complete', (evt) => {
return new Promise<any>((done) => {
lightning.onSocketEvent('run:complete', attempt.id, (evt) => {
if (attempt.id === evt.runId) {
done(evt.payload);
}
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/worker/test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import crypto from 'node:crypto';
import Koa from 'koa';
import { generateKeys } from '@openfn/lightning-mock';

import { initLightning, initWorker, randomPort } from '../src/init';
import { initLightning, initWorker, nextPort } from '../src/init';

let lightning;
let worker;
Expand All @@ -14,7 +14,7 @@ let lightningPort;

test.before(async () => {
const keys = await generateKeys();
lightningPort = randomPort();
lightningPort = nextPort();
lightning = initLightning(lightningPort, keys.private);

const engineArgs = {
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/worker/test/runs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import {
createJob,
createTrigger,
} from '../src/factories';
import { initLightning, initWorker } from '../src/init';
import { initLightning, initWorker, nextPort } from '../src/init';

let lightning;
let worker;

test.before(async () => {
const keys = await generateKeys();
const lightningPort = 4321;
const lightningPort = nextPort();

lightning = initLightning(lightningPort, keys.private);

Expand Down
10 changes: 6 additions & 4 deletions integration-tests/worker/test/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ test.serial('allow a job to complete after receiving a sigterm', (t) => {
test.serial("don't restore the claim loop after a sigterm", (t) => {
return new Promise(async (done) => {
let abort = false;
let didSendSigterm = false;
const port = getPort();

const job = createJob({
Expand Down Expand Up @@ -208,7 +207,6 @@ test.serial("don't restore the claim loop after a sigterm", (t) => {
// After the second run starts, there should be no more claims
lightning.on('run:start', (evt) => {
if (evt.runId === b.id) {
didSendSigterm = true;
// Kill the worker once the second job has started
// This will force an overlap of two pending runs at full capacity
workerProcess.kill('SIGTERM');
Expand All @@ -220,8 +218,12 @@ test.serial("don't restore the claim loop after a sigterm", (t) => {
lightning.enqueueRun(a);
lightning.enqueueRun(b);

lightning.on('claim', (e) => {
if (didSendSigterm && !abort) {
lightning.on('claim', () => {
const didReceiveSigterm = workerLogs.some((l) =>
l.match(/SIGTERM RECEIVED/)
);

if (didReceiveSigterm && !abort) {
abort = true;
t.fail('Claim triggered after sigterm');
done();
Expand Down