refactor(core): Use DI for WorkflowRunner (no-changelog) (#8372)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2024-01-26 13:49:39 +01:00
committed by GitHub
parent bf11c7c1bd
commit c70fa66e76
21 changed files with 215 additions and 258 deletions

View File

@@ -1,11 +1,10 @@
import { ActiveExecutions } from '@/ActiveExecutions';
import PCancelable from 'p-cancelable';
import { v4 as uuid } from 'uuid';
import { Container } from 'typedi';
import type { IExecuteResponsePromiseData, IRun } from 'n8n-workflow';
import { createDeferredPromise } from 'n8n-workflow';
import type { IWorkflowExecutionDataProcess } from '@/Interfaces';
import { ExecutionRepository } from '@db/repositories/execution.repository';
import type { ExecutionRepository } from '@db/repositories/execution.repository';
import { mock } from 'jest-mock-extended';
const FAKE_EXECUTION_ID = '15';
@@ -14,7 +13,7 @@ const FAKE_SECOND_EXECUTION_ID = '20';
const updateExistingExecution = jest.fn();
const createNewExecution = jest.fn(async () => FAKE_EXECUTION_ID);
Container.set(ExecutionRepository, {
const executionRepository = mock<ExecutionRepository>({
updateExistingExecution,
createNewExecution,
});
@@ -23,7 +22,7 @@ describe('ActiveExecutions', () => {
let activeExecutions: ActiveExecutions;
beforeEach(() => {
activeExecutions = new ActiveExecutions(mock());
activeExecutions = new ActiveExecutions(mock(), executionRepository);
});
afterEach(() => {

View File

@@ -21,7 +21,7 @@ describe('WaitTracker', () => {
it('should query DB for waiting executions', async () => {
executionRepository.getWaitingExecutions.mockResolvedValue([execution]);
new WaitTracker(mock(), executionRepository, mock());
new WaitTracker(mock(), executionRepository, mock(), mock());
expect(executionRepository.getWaitingExecutions).toHaveBeenCalledTimes(1);
});
@@ -29,7 +29,7 @@ describe('WaitTracker', () => {
it('if no executions to start, should do nothing', () => {
executionRepository.getWaitingExecutions.mockResolvedValue([]);
new WaitTracker(mock(), executionRepository, mock());
new WaitTracker(mock(), executionRepository, mock(), mock());
expect(executionRepository.findSingleExecution).not.toHaveBeenCalled();
});
@@ -37,7 +37,7 @@ describe('WaitTracker', () => {
describe('if execution to start', () => {
it('if not enough time passed, should not start execution', async () => {
executionRepository.getWaitingExecutions.mockResolvedValue([execution]);
const waitTracker = new WaitTracker(mock(), executionRepository, mock());
const waitTracker = new WaitTracker(mock(), executionRepository, mock(), mock());
executionRepository.getWaitingExecutions.mockResolvedValue([execution]);
await waitTracker.getWaitingExecutions();
@@ -51,7 +51,7 @@ describe('WaitTracker', () => {
it('if enough time passed, should start execution', async () => {
executionRepository.getWaitingExecutions.mockResolvedValue([]);
const waitTracker = new WaitTracker(mock(), executionRepository, mock());
const waitTracker = new WaitTracker(mock(), executionRepository, mock(), mock());
executionRepository.getWaitingExecutions.mockResolvedValue([execution]);
await waitTracker.getWaitingExecutions();
@@ -68,7 +68,7 @@ describe('WaitTracker', () => {
describe('startExecution()', () => {
it('should query for execution to start', async () => {
executionRepository.getWaitingExecutions.mockResolvedValue([]);
const waitTracker = new WaitTracker(mock(), executionRepository, mock());
const waitTracker = new WaitTracker(mock(), executionRepository, mock(), mock());
executionRepository.findSingleExecution.mockResolvedValue(execution);
waitTracker.startExecution(execution.id);

View File

@@ -1,11 +1,9 @@
import Container from 'typedi';
import { WorkflowHooks, type ExecutionError, type IWorkflowExecuteHooks } from 'n8n-workflow';
import type { User } from '@db/entities/User';
import { WorkflowRunner } from '@/WorkflowRunner';
import { WorkflowHooks, type ExecutionError, type IWorkflowExecuteHooks } from 'n8n-workflow';
import { Push } from '@/push';
import Container from 'typedi';
import config from '@/config';
import { mockInstance } from '../shared/mocking';
import * as testDb from '../integration/shared/testDb';
import { setupTestServer } from '../integration/shared/utils';
import { createUser } from '../integration/shared/db/users';
@@ -26,10 +24,7 @@ const watchedWorkflowExecuteAfter = jest.spyOn(watchers, 'workflowExecuteAfter')
beforeAll(async () => {
owner = await createUser({ role: 'global:owner' });
mockInstance(Push);
Container.set(Push, new Push());
runner = new WorkflowRunner();
runner = Container.get(WorkflowRunner);
hookFunctions = {
workflowExecuteAfter: [watchers.workflowExecuteAfter],

View File

@@ -1,8 +1,11 @@
import type { INode } from 'n8n-workflow';
import { WorkflowExecutionService } from '@/workflows/workflowExecution.service';
import type { IWorkflowDb } from '@/Interfaces';
import { mock } from 'jest-mock-extended';
import type { WorkflowEntity } from '@db/entities/WorkflowEntity';
import type { IWorkflowDb } from '@/Interfaces';
import { WorkflowExecutionService } from '@/workflows/workflowExecution.service';
import type { WorkflowRunner } from '@/WorkflowRunner';
const webhookNode: INode = {
name: 'Webhook',
type: 'n8n-nodes-base.webhook',
@@ -47,17 +50,28 @@ const hackerNewsNode: INode = {
};
describe('WorkflowExecutionService', () => {
let workflowExecutionService: WorkflowExecutionService;
const workflowRunner = mock<WorkflowRunner>();
const workflowExecutionService = new WorkflowExecutionService(
mock(),
mock(),
mock(),
mock(),
mock(),
mock(),
workflowRunner,
);
beforeAll(() => {
workflowExecutionService = new WorkflowExecutionService(
mock(),
mock(),
mock(),
mock(),
mock(),
mock(),
);
describe('runWorkflow()', () => {
test('should call `WorkflowRunner.run()`', async () => {
const node = mock<INode>();
const workflow = mock<WorkflowEntity>({ active: true, nodes: [node] });
workflowRunner.run.mockResolvedValue('fake-execution-id');
await workflowExecutionService.runWorkflow(workflow, node, [[]], mock(), 'trigger');
expect(workflowRunner.run).toHaveBeenCalledTimes(1);
});
});
describe('selectPinnedActivatorStarter()', () => {