2024-01-16 01:53:17 -08:00
|
|
|
import { WaitTracker } from '@/WaitTracker';
|
|
|
|
import { mock } from 'jest-mock-extended';
|
|
|
|
import type { ExecutionRepository } from '@/databases/repositories/execution.repository';
|
|
|
|
import type { IExecutionResponse } from '@/Interfaces';
|
2024-06-03 08:43:24 -07:00
|
|
|
import { OrchestrationService } from '@/services/orchestration.service';
|
2024-04-04 04:28:20 -07:00
|
|
|
import type { MultiMainSetup } from '@/services/orchestration/main/MultiMainSetup.ee';
|
2024-01-16 01:53:17 -08:00
|
|
|
|
|
|
|
jest.useFakeTimers();
|
|
|
|
|
|
|
|
describe('WaitTracker', () => {
|
|
|
|
const executionRepository = mock<ExecutionRepository>();
|
2024-06-03 08:43:24 -07:00
|
|
|
const multiMainSetup = mock<MultiMainSetup>();
|
|
|
|
const orchestrationService = new OrchestrationService(mock(), mock(), multiMainSetup);
|
2024-01-16 01:53:17 -08:00
|
|
|
|
|
|
|
const execution = mock<IExecutionResponse>({
|
|
|
|
id: '123',
|
|
|
|
waitTill: new Date(Date.now() + 1000),
|
|
|
|
});
|
|
|
|
|
2024-06-03 08:43:24 -07:00
|
|
|
let waitTracker: WaitTracker;
|
|
|
|
beforeEach(() => {
|
|
|
|
waitTracker = new WaitTracker(
|
|
|
|
mock(),
|
|
|
|
executionRepository,
|
|
|
|
mock(),
|
|
|
|
mock(),
|
|
|
|
orchestrationService,
|
|
|
|
);
|
|
|
|
multiMainSetup.on.mockReturnThis();
|
|
|
|
});
|
|
|
|
|
2024-01-16 01:53:17 -08:00
|
|
|
afterEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
2024-06-03 08:43:24 -07:00
|
|
|
describe('init()', () => {
|
2024-06-17 03:49:40 -07:00
|
|
|
it('should query DB for waiting executions if leader', async () => {
|
|
|
|
jest.spyOn(orchestrationService, 'isLeader', 'get').mockReturnValue(true);
|
2024-01-16 01:53:17 -08:00
|
|
|
executionRepository.getWaitingExecutions.mockResolvedValue([execution]);
|
|
|
|
|
2024-06-03 08:43:24 -07:00
|
|
|
waitTracker.init();
|
2024-01-16 01:53:17 -08:00
|
|
|
|
|
|
|
expect(executionRepository.getWaitingExecutions).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
2024-06-17 03:49:40 -07:00
|
|
|
it('if follower, should do nothing', () => {
|
|
|
|
executionRepository.getWaitingExecutions.mockResolvedValue([]);
|
|
|
|
|
|
|
|
waitTracker.init();
|
|
|
|
|
|
|
|
expect(executionRepository.findSingleExecution).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2024-01-16 01:53:17 -08:00
|
|
|
it('if no executions to start, should do nothing', () => {
|
|
|
|
executionRepository.getWaitingExecutions.mockResolvedValue([]);
|
|
|
|
|
2024-06-03 08:43:24 -07:00
|
|
|
waitTracker.init();
|
2024-01-16 01:53:17 -08:00
|
|
|
|
|
|
|
expect(executionRepository.findSingleExecution).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('if execution to start', () => {
|
|
|
|
it('if not enough time passed, should not start execution', async () => {
|
|
|
|
executionRepository.getWaitingExecutions.mockResolvedValue([execution]);
|
2024-06-03 08:43:24 -07:00
|
|
|
waitTracker.init();
|
2024-01-16 01:53:17 -08:00
|
|
|
|
|
|
|
executionRepository.getWaitingExecutions.mockResolvedValue([execution]);
|
|
|
|
await waitTracker.getWaitingExecutions();
|
|
|
|
|
|
|
|
const startExecutionSpy = jest.spyOn(waitTracker, 'startExecution');
|
|
|
|
|
|
|
|
jest.advanceTimersByTime(100);
|
|
|
|
|
|
|
|
expect(startExecutionSpy).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('if enough time passed, should start execution', async () => {
|
|
|
|
executionRepository.getWaitingExecutions.mockResolvedValue([]);
|
2024-06-03 08:43:24 -07:00
|
|
|
waitTracker.init();
|
2024-01-16 01:53:17 -08:00
|
|
|
|
|
|
|
executionRepository.getWaitingExecutions.mockResolvedValue([execution]);
|
|
|
|
await waitTracker.getWaitingExecutions();
|
|
|
|
|
|
|
|
const startExecutionSpy = jest.spyOn(waitTracker, 'startExecution');
|
|
|
|
|
|
|
|
jest.advanceTimersByTime(2_000);
|
|
|
|
|
|
|
|
expect(startExecutionSpy).toHaveBeenCalledWith(execution.id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('startExecution()', () => {
|
|
|
|
it('should query for execution to start', async () => {
|
|
|
|
executionRepository.getWaitingExecutions.mockResolvedValue([]);
|
2024-06-03 08:43:24 -07:00
|
|
|
waitTracker.init();
|
2024-01-16 01:53:17 -08:00
|
|
|
|
|
|
|
executionRepository.findSingleExecution.mockResolvedValue(execution);
|
|
|
|
waitTracker.startExecution(execution.id);
|
|
|
|
jest.advanceTimersByTime(5);
|
|
|
|
|
|
|
|
expect(executionRepository.findSingleExecution).toHaveBeenCalledWith(execution.id, {
|
|
|
|
includeData: true,
|
|
|
|
unflattenData: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2024-04-04 04:28:20 -07:00
|
|
|
|
2024-04-09 06:50:10 -07:00
|
|
|
describe('single-main setup', () => {
|
|
|
|
it('should start tracking', () => {
|
|
|
|
executionRepository.getWaitingExecutions.mockResolvedValue([]);
|
|
|
|
|
2024-06-03 08:43:24 -07:00
|
|
|
waitTracker.init();
|
2024-04-09 06:50:10 -07:00
|
|
|
|
|
|
|
expect(executionRepository.getWaitingExecutions).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-04-04 04:28:20 -07:00
|
|
|
describe('multi-main setup', () => {
|
|
|
|
it('should start tracking if leader', () => {
|
2024-06-03 08:43:24 -07:00
|
|
|
jest.spyOn(orchestrationService, 'isLeader', 'get').mockReturnValue(true);
|
|
|
|
jest.spyOn(orchestrationService, 'isSingleMainSetup', 'get').mockReturnValue(false);
|
2024-04-04 04:28:20 -07:00
|
|
|
|
|
|
|
executionRepository.getWaitingExecutions.mockResolvedValue([]);
|
|
|
|
|
2024-06-03 08:43:24 -07:00
|
|
|
waitTracker.init();
|
2024-04-04 04:28:20 -07:00
|
|
|
|
|
|
|
expect(executionRepository.getWaitingExecutions).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not start tracking if follower', () => {
|
2024-06-03 08:43:24 -07:00
|
|
|
jest.spyOn(orchestrationService, 'isLeader', 'get').mockReturnValue(false);
|
|
|
|
jest.spyOn(orchestrationService, 'isSingleMainSetup', 'get').mockReturnValue(false);
|
2024-04-04 04:28:20 -07:00
|
|
|
|
|
|
|
executionRepository.getWaitingExecutions.mockResolvedValue([]);
|
|
|
|
|
2024-06-03 08:43:24 -07:00
|
|
|
waitTracker.init();
|
2024-04-04 04:28:20 -07:00
|
|
|
|
|
|
|
expect(executionRepository.getWaitingExecutions).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2024-01-16 01:53:17 -08:00
|
|
|
});
|