feat: Make task runners work with n8n from npm (no-changelog) (#11015)
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
import { GlobalConfig } from '@n8n/config';
|
||||
import Container from 'typedi';
|
||||
|
||||
import { TaskRunnerService } from '@/runners/runner-ws-server';
|
||||
import { TaskBroker } from '@/runners/task-broker.service';
|
||||
import { TaskRunnerProcess } from '@/runners/task-runner-process';
|
||||
import { retryUntil } from '@test-integration/retry-until';
|
||||
import { setupTaskRunnerTestServer } from '@test-integration/utils/task-runner-test-server';
|
||||
|
||||
describe('TaskRunnerProcess', () => {
|
||||
const authToken = 'token';
|
||||
const globalConfig = Container.get(GlobalConfig);
|
||||
globalConfig.taskRunners.authToken = authToken;
|
||||
const testServer = setupTaskRunnerTestServer({});
|
||||
globalConfig.port = testServer.port;
|
||||
|
||||
const runnerProcess = Container.get(TaskRunnerProcess);
|
||||
const taskBroker = Container.get(TaskBroker);
|
||||
const taskRunnerService = Container.get(TaskRunnerService);
|
||||
|
||||
afterEach(async () => {
|
||||
await runnerProcess.stop();
|
||||
});
|
||||
|
||||
const getNumConnectedRunners = () => taskRunnerService.runnerConnections.size;
|
||||
const getNumRegisteredRunners = () => taskBroker.getKnownRunners().size;
|
||||
|
||||
it('should start and connect the task runner', async () => {
|
||||
// Act
|
||||
await runnerProcess.start();
|
||||
|
||||
// Assert
|
||||
expect(runnerProcess.isRunning).toBeTruthy();
|
||||
|
||||
// Wait until the runner has connected
|
||||
await retryUntil(() => expect(getNumConnectedRunners()).toBe(1));
|
||||
expect(getNumRegisteredRunners()).toBe(1);
|
||||
});
|
||||
|
||||
it('should stop an disconnect the task runner', async () => {
|
||||
// Arrange
|
||||
await runnerProcess.start();
|
||||
|
||||
// Wait until the runner has connected
|
||||
await retryUntil(() => expect(getNumConnectedRunners()).toBe(1));
|
||||
expect(getNumRegisteredRunners()).toBe(1);
|
||||
|
||||
// Act
|
||||
await runnerProcess.stop();
|
||||
|
||||
// Assert
|
||||
// Wait until the runner has disconnected
|
||||
await retryUntil(() => expect(getNumConnectedRunners()).toBe(0));
|
||||
|
||||
expect(runnerProcess.isRunning).toBeFalsy();
|
||||
expect(getNumRegisteredRunners()).toBe(0);
|
||||
});
|
||||
|
||||
it('should restart the task runner if it exits', async () => {
|
||||
// Arrange
|
||||
await runnerProcess.start();
|
||||
|
||||
// Wait until the runner has connected
|
||||
await retryUntil(() => expect(getNumConnectedRunners()).toBe(1));
|
||||
const processId = runnerProcess.pid;
|
||||
|
||||
// Act
|
||||
// @ts-expect-error private property
|
||||
runnerProcess.process?.kill('SIGKILL');
|
||||
|
||||
// Assert
|
||||
// Wait until the runner is running again
|
||||
await retryUntil(() => expect(runnerProcess.isRunning).toBeTruthy());
|
||||
expect(runnerProcess.pid).not.toBe(processId);
|
||||
|
||||
// Wait until the runner has connected again
|
||||
await retryUntil(() => expect(getNumConnectedRunners()).toBe(1));
|
||||
expect(getNumConnectedRunners()).toBe(1);
|
||||
expect(getNumRegisteredRunners()).toBe(1);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,43 @@
|
||||
import { GlobalConfig } from '@n8n/config';
|
||||
import cookieParser from 'cookie-parser';
|
||||
import type { Application } from 'express';
|
||||
import express from 'express';
|
||||
import type { Server } from 'node:http';
|
||||
import type { AddressInfo } from 'node:net';
|
||||
import Container from 'typedi';
|
||||
|
||||
import { rawBodyReader } from '@/middlewares';
|
||||
import { setupRunnerHandler, setupRunnerServer } from '@/runners/runner-ws-server';
|
||||
|
||||
export interface TaskRunnerTestServer {
|
||||
app: Application;
|
||||
httpServer: Server;
|
||||
port: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up a task runner HTTP & WS server for testing purposes
|
||||
*/
|
||||
export const setupTaskRunnerTestServer = ({}): TaskRunnerTestServer => {
|
||||
const app = express();
|
||||
app.use(rawBodyReader);
|
||||
app.use(cookieParser());
|
||||
|
||||
const testServer: TaskRunnerTestServer = {
|
||||
app,
|
||||
httpServer: app.listen(0),
|
||||
port: 0,
|
||||
};
|
||||
|
||||
testServer.port = (testServer.httpServer.address() as AddressInfo).port;
|
||||
|
||||
const globalConfig = Container.get(GlobalConfig);
|
||||
setupRunnerServer(globalConfig.endpoints.rest, testServer.httpServer, testServer.app);
|
||||
setupRunnerHandler(globalConfig.endpoints.rest, testServer.app);
|
||||
|
||||
afterAll(async () => {
|
||||
testServer.httpServer.close();
|
||||
});
|
||||
|
||||
return testServer;
|
||||
};
|
||||
Reference in New Issue
Block a user