|
| 1 | +import type { Logger } from '@n8n/backend-common'; |
| 2 | +import type { TaskRunnersConfig } from '@n8n/config'; |
| 3 | +import type { ErrorReporter } from 'n8n-core'; |
| 4 | +import { mock } from 'vitest-mock-extended'; |
| 5 | + |
| 6 | +import type { EventService } from '@/events/event.service'; |
| 7 | +import type { TaskBrokerServer } from '@/task-runners/task-broker/task-broker-server'; |
| 8 | +import type { JsTaskRunnerProcess } from '@/task-runners/task-runner-process-js'; |
| 9 | +import type { PyTaskRunnerProcess } from '@/task-runners/task-runner-process-py'; |
| 10 | +import { TaskRunnerModule } from '@/task-runners/task-runner-module'; |
| 11 | + |
| 12 | +describe('TaskRunnerModule', () => { |
| 13 | + describe('stop', () => { |
| 14 | + it('should stop the broker server before the runner processes, so the drain can finish in-flight tasks first', async () => { |
| 15 | + const logger = mock<Logger>(); |
| 16 | + logger.scoped.mockReturnValue(logger); |
| 17 | + const module = new TaskRunnerModule( |
| 18 | + logger, |
| 19 | + mock<ErrorReporter>(), |
| 20 | + mock<TaskRunnersConfig>(), |
| 21 | + mock<EventService>(), |
| 22 | + ); |
| 23 | + |
| 24 | + const brokerServer = mock<TaskBrokerServer>(); |
| 25 | + const jsRunnerProcess = mock<JsTaskRunnerProcess>(); |
| 26 | + const pyRunnerProcess = mock<PyTaskRunnerProcess>(); |
| 27 | + Object.assign(module, { |
| 28 | + taskBrokerHttpServer: brokerServer, |
| 29 | + jsRunnerProcess, |
| 30 | + pyRunnerProcess, |
| 31 | + }); |
| 32 | + |
| 33 | + const order: string[] = []; |
| 34 | + // The broker stop completes asynchronously, so a stop() that merely starts |
| 35 | + // it without awaiting would record the runner stops first. |
| 36 | + brokerServer.stop.mockImplementation(async () => { |
| 37 | + await new Promise((resolve) => setImmediate(resolve)); |
| 38 | + order.push('brokerServer'); |
| 39 | + }); |
| 40 | + jsRunnerProcess.stop.mockImplementation(async () => { |
| 41 | + order.push('jsRunnerProcess'); |
| 42 | + }); |
| 43 | + pyRunnerProcess.stop.mockImplementation(async () => { |
| 44 | + order.push('pyRunnerProcess'); |
| 45 | + }); |
| 46 | + |
| 47 | + await module.stop(); |
| 48 | + |
| 49 | + expect(order).toHaveLength(3); |
| 50 | + expect(order[0]).toBe('brokerServer'); |
| 51 | + }); |
| 52 | + }); |
| 53 | +}); |
0 commit comments