mirror of
https://github.com/n8n-io/n8n.git
synced 2026-10-12 02:19:43 +00:00
test: Use fake timers for history compaction tests (#35573)
This commit is contained in:
+70
@@ -64,6 +64,16 @@ describe('WorkflowHistoryCompactionService', () => {
|
||||
});
|
||||
|
||||
describe('startCompacting', () => {
|
||||
beforeEach(() => {
|
||||
// Set the system to a time that isn't 3 AM to avoid hitting the "trim once a day" window
|
||||
const mockDate = new Date(2026, 10, 10, 1, 0, 0);
|
||||
vi.setSystemTime(mockDate);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it('should start compacting if service is enabled and DB is migrated', () => {
|
||||
const compactingService = new WorkflowHistoryCompactionService(
|
||||
config,
|
||||
@@ -194,4 +204,64 @@ describe('WorkflowHistoryCompactionService', () => {
|
||||
// should still call recent history compaction
|
||||
expect(optimizeHistoriesSpy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should trim if triggered at 3 AM with trimOnStartUp as false', () => {
|
||||
const mockDate = new Date(2026, 10, 10, 3, 0, 0);
|
||||
vi.setSystemTime(mockDate);
|
||||
|
||||
const compactingService = new WorkflowHistoryCompactionService(
|
||||
{ ...config, trimOnStartUp: false },
|
||||
globalConfig,
|
||||
mockLogger(),
|
||||
mock<InstanceSettings>({ isLeader: true, instanceType: 'main', isMultiMain: true }),
|
||||
dbConnection,
|
||||
mock(),
|
||||
mock<EventService>(),
|
||||
);
|
||||
|
||||
vi
|
||||
// @ts-expect-error Private method
|
||||
.spyOn(compactingService, 'optimizeHistories')
|
||||
.mockImplementation((() => {}) as never);
|
||||
const trimLongRunningHistoriesSpy = vi
|
||||
// @ts-expect-error Private method
|
||||
.spyOn(compactingService, 'trimLongRunningHistories')
|
||||
.mockImplementation((() => {}) as never);
|
||||
|
||||
compactingService.startCompacting();
|
||||
|
||||
expect(trimLongRunningHistoriesSpy).toHaveBeenCalled();
|
||||
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it('should not trim if triggered outside of 3 AM with trimOnStartUp as false', () => {
|
||||
const mockDate = new Date(2026, 10, 10, 5, 0, 0);
|
||||
vi.setSystemTime(mockDate);
|
||||
|
||||
const compactingService = new WorkflowHistoryCompactionService(
|
||||
{ ...config, trimOnStartUp: false },
|
||||
globalConfig,
|
||||
mockLogger(),
|
||||
mock<InstanceSettings>({ isLeader: true, instanceType: 'main', isMultiMain: true }),
|
||||
dbConnection,
|
||||
mock(),
|
||||
mock<EventService>(),
|
||||
);
|
||||
|
||||
vi
|
||||
// @ts-expect-error Private method
|
||||
.spyOn(compactingService, 'optimizeHistories')
|
||||
.mockImplementation((() => {}) as never);
|
||||
const trimLongRunningHistoriesSpy = vi
|
||||
// @ts-expect-error Private method
|
||||
.spyOn(compactingService, 'trimLongRunningHistories')
|
||||
.mockImplementation((() => {}) as never);
|
||||
|
||||
compactingService.startCompacting();
|
||||
|
||||
expect(trimLongRunningHistoriesSpy).not.toHaveBeenCalled();
|
||||
|
||||
vi.useRealTimers();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user