refactor(core): Set up worker server (#10814)

This commit is contained in:
Iván Ovejero
2024-09-17 10:14:12 +02:00
committed by GitHub
parent e6d84db899
commit 94aa680c9b
6 changed files with 285 additions and 114 deletions

View File

@@ -0,0 +1,127 @@
import type { GlobalConfig } from '@n8n/config';
import type express from 'express';
import { mock } from 'jest-mock-extended';
import { AssertionError } from 'node:assert';
import * as http from 'node:http';
import config from '@/config';
import { PortTakenError } from '@/errors/port-taken.error';
import type { ExternalHooks } from '@/external-hooks';
import { bodyParser, rawBodyReader } from '@/middlewares';
import { WorkerServer } from '../worker-server';
const app = mock<express.Application>();
jest.mock('node:http');
jest.mock('express', () => ({ __esModule: true, default: () => app }));
const addressInUseError = () => {
const error: NodeJS.ErrnoException = new Error('Port already in use');
error.code = 'EADDRINUSE';
return error;
};
describe('WorkerServer', () => {
let globalConfig: GlobalConfig;
const externalHooks = mock<ExternalHooks>();
beforeEach(() => {
config.set('generic.instanceType', 'worker');
globalConfig = mock<GlobalConfig>({
queue: {
health: { active: true, port: 5678 },
},
credentials: {
overwrite: { endpoint: '' },
},
});
jest.restoreAllMocks();
});
describe('constructor', () => {
it('should throw if non-worker instance type', () => {
config.set('generic.instanceType', 'webhook');
expect(
() => new WorkerServer(globalConfig, mock(), mock(), mock(), externalHooks),
).toThrowError(AssertionError);
});
it('should throw if port taken', async () => {
const server = mock<http.Server>();
jest.spyOn(http, 'createServer').mockReturnValue(server);
server.on.mockImplementation((event: string, callback: (arg?: unknown) => void) => {
if (event === 'error') callback(addressInUseError());
return server;
});
expect(
() => new WorkerServer(globalConfig, mock(), mock(), mock(), externalHooks),
).toThrowError(PortTakenError);
});
it('should set up `/healthz` if health check is enabled', async () => {
jest.spyOn(http, 'createServer').mockReturnValue(mock<http.Server>());
new WorkerServer(globalConfig, mock(), mock(), mock(), externalHooks);
expect(app.get).toHaveBeenCalledWith('/healthz', expect.any(Function));
});
it('should not set up `/healthz` if health check is disabled', async () => {
globalConfig.queue.health.active = false;
jest.spyOn(http, 'createServer').mockReturnValue(mock<http.Server>());
new WorkerServer(globalConfig, mock(), mock(), mock(), externalHooks);
expect(app.get).not.toHaveBeenCalled();
});
it('should set up `/:endpoint` if overwrites endpoint is set', async () => {
jest.spyOn(http, 'createServer').mockReturnValue(mock<http.Server>());
const CREDENTIALS_OVERWRITE_ENDPOINT = 'credentials/overwrites';
globalConfig.credentials.overwrite.endpoint = CREDENTIALS_OVERWRITE_ENDPOINT;
new WorkerServer(globalConfig, mock(), mock(), mock(), externalHooks);
expect(app.post).toHaveBeenCalledWith(
`/${CREDENTIALS_OVERWRITE_ENDPOINT}`,
rawBodyReader,
bodyParser,
expect.any(Function),
);
});
it('should not set up `/:endpoint` if overwrites endpoint is not set', async () => {
jest.spyOn(http, 'createServer').mockReturnValue(mock<http.Server>());
new WorkerServer(globalConfig, mock(), mock(), mock(), externalHooks);
expect(app.post).not.toHaveBeenCalled();
});
});
describe('init', () => {
it('should call `worker.ready` external hook', async () => {
const server = mock<http.Server>();
jest.spyOn(http, 'createServer').mockReturnValue(server);
server.listen.mockImplementation((_port, callback: () => void) => {
callback();
return server;
});
const workerServer = new WorkerServer(globalConfig, mock(), mock(), mock(), externalHooks);
await workerServer.init();
expect(externalHooks.run).toHaveBeenCalledWith('worker.ready');
});
});
});