-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(core): Expand test coverage for pruning (#11567)
- Loading branch information
Showing
6 changed files
with
242 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
213 changes: 213 additions & 0 deletions
213
packages/cli/src/services/pruning/__tests__/pruning.service.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
import type { GlobalConfig } from '@n8n/config'; | ||
import { mock } from 'jest-mock-extended'; | ||
import type { InstanceSettings } from 'n8n-core'; | ||
|
||
import type { MultiMainSetup } from '@/scaling/multi-main-setup.ee'; | ||
import type { OrchestrationService } from '@/services/orchestration.service'; | ||
import { mockLogger } from '@test/mocking'; | ||
|
||
import { PruningService } from '../pruning.service'; | ||
|
||
describe('PruningService', () => { | ||
describe('init', () => { | ||
it('should start pruning if leader', () => { | ||
const pruningService = new PruningService( | ||
mockLogger(), | ||
mock<InstanceSettings>({ isLeader: true }), | ||
mock(), | ||
mock(), | ||
mock<OrchestrationService>({ | ||
isMultiMainSetupEnabled: true, | ||
multiMainSetup: mock<MultiMainSetup>(), | ||
}), | ||
mock(), | ||
); | ||
const startPruningSpy = jest.spyOn(pruningService, 'startPruning'); | ||
|
||
pruningService.init(); | ||
|
||
expect(startPruningSpy).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not start pruning if follower', () => { | ||
const pruningService = new PruningService( | ||
mockLogger(), | ||
mock<InstanceSettings>({ isLeader: false }), | ||
mock(), | ||
mock(), | ||
mock<OrchestrationService>({ | ||
isMultiMainSetupEnabled: true, | ||
multiMainSetup: mock<MultiMainSetup>(), | ||
}), | ||
mock(), | ||
); | ||
const startPruningSpy = jest.spyOn(pruningService, 'startPruning'); | ||
|
||
pruningService.init(); | ||
|
||
expect(startPruningSpy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should register leadership events if multi-main setup is enabled', () => { | ||
const pruningService = new PruningService( | ||
mockLogger(), | ||
mock<InstanceSettings>({ isLeader: true }), | ||
mock(), | ||
mock(), | ||
mock<OrchestrationService>({ | ||
isMultiMainSetupEnabled: true, | ||
multiMainSetup: mock<MultiMainSetup>({ on: jest.fn() }), | ||
}), | ||
mock(), | ||
); | ||
|
||
pruningService.init(); | ||
|
||
// @ts-expect-error Private method | ||
expect(pruningService.orchestrationService.multiMainSetup.on).toHaveBeenCalledWith( | ||
'leader-takeover', | ||
expect.any(Function), | ||
); | ||
|
||
// @ts-expect-error Private method | ||
expect(pruningService.orchestrationService.multiMainSetup.on).toHaveBeenCalledWith( | ||
'leader-stepdown', | ||
expect.any(Function), | ||
); | ||
}); | ||
}); | ||
|
||
describe('isEnabled', () => { | ||
it('should return `true` based on config if leader main', () => { | ||
const pruningService = new PruningService( | ||
mockLogger(), | ||
mock<InstanceSettings>({ isLeader: true, instanceType: 'main' }), | ||
mock(), | ||
mock(), | ||
mock<OrchestrationService>({ | ||
isMultiMainSetupEnabled: true, | ||
multiMainSetup: mock<MultiMainSetup>(), | ||
}), | ||
mock<GlobalConfig>({ pruning: { isEnabled: true } }), | ||
); | ||
|
||
// @ts-expect-error Private method | ||
const isEnabled = pruningService.isEnabled(); | ||
|
||
expect(isEnabled).toBe(true); | ||
}); | ||
|
||
it('should return `false` based on config if leader main', () => { | ||
const pruningService = new PruningService( | ||
mockLogger(), | ||
mock<InstanceSettings>({ isLeader: true, instanceType: 'main' }), | ||
mock(), | ||
mock(), | ||
mock<OrchestrationService>({ | ||
isMultiMainSetupEnabled: true, | ||
multiMainSetup: mock<MultiMainSetup>(), | ||
}), | ||
mock<GlobalConfig>({ pruning: { isEnabled: false } }), | ||
); | ||
|
||
// @ts-expect-error Private method | ||
const isEnabled = pruningService.isEnabled(); | ||
|
||
expect(isEnabled).toBe(false); | ||
}); | ||
|
||
it('should return `false` if non-main even if enabled', () => { | ||
const pruningService = new PruningService( | ||
mockLogger(), | ||
mock<InstanceSettings>({ isLeader: false, instanceType: 'worker' }), | ||
mock(), | ||
mock(), | ||
mock<OrchestrationService>({ | ||
isMultiMainSetupEnabled: true, | ||
multiMainSetup: mock<MultiMainSetup>(), | ||
}), | ||
mock<GlobalConfig>({ pruning: { isEnabled: true } }), | ||
); | ||
|
||
// @ts-expect-error Private method | ||
const isEnabled = pruningService.isEnabled(); | ||
|
||
expect(isEnabled).toBe(false); | ||
}); | ||
|
||
it('should return `false` if follower main even if enabled', () => { | ||
const pruningService = new PruningService( | ||
mockLogger(), | ||
mock<InstanceSettings>({ isLeader: false, isFollower: true, instanceType: 'main' }), | ||
mock(), | ||
mock(), | ||
mock<OrchestrationService>({ | ||
isMultiMainSetupEnabled: true, | ||
multiMainSetup: mock<MultiMainSetup>(), | ||
}), | ||
mock<GlobalConfig>({ pruning: { isEnabled: true }, multiMainSetup: { enabled: true } }), | ||
); | ||
|
||
// @ts-expect-error Private method | ||
const isEnabled = pruningService.isEnabled(); | ||
|
||
expect(isEnabled).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('startPruning', () => { | ||
it('should not start pruning if service is disabled', () => { | ||
const pruningService = new PruningService( | ||
mockLogger(), | ||
mock<InstanceSettings>({ isLeader: true, instanceType: 'main' }), | ||
mock(), | ||
mock(), | ||
mock<OrchestrationService>({ | ||
isMultiMainSetupEnabled: true, | ||
multiMainSetup: mock<MultiMainSetup>(), | ||
}), | ||
mock<GlobalConfig>({ pruning: { isEnabled: false } }), | ||
); | ||
|
||
// @ts-expect-error Private method | ||
const setSoftDeletionInterval = jest.spyOn(pruningService, 'setSoftDeletionInterval'); | ||
|
||
// @ts-expect-error Private method | ||
const scheduleHardDeletion = jest.spyOn(pruningService, 'scheduleHardDeletion'); | ||
|
||
pruningService.startPruning(); | ||
|
||
expect(setSoftDeletionInterval).not.toHaveBeenCalled(); | ||
expect(scheduleHardDeletion).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should start pruning if service is enabled', () => { | ||
const pruningService = new PruningService( | ||
mockLogger(), | ||
mock<InstanceSettings>({ isLeader: true, instanceType: 'main' }), | ||
mock(), | ||
mock(), | ||
mock<OrchestrationService>({ | ||
isMultiMainSetupEnabled: true, | ||
multiMainSetup: mock<MultiMainSetup>(), | ||
}), | ||
mock<GlobalConfig>({ pruning: { isEnabled: true } }), | ||
); | ||
|
||
const setSoftDeletionInterval = jest | ||
// @ts-expect-error Private method | ||
.spyOn(pruningService, 'setSoftDeletionInterval') | ||
.mockImplementation(); | ||
|
||
const scheduleHardDeletion = jest | ||
// @ts-expect-error Private method | ||
.spyOn(pruningService, 'scheduleHardDeletion') | ||
.mockImplementation(); | ||
|
||
pruningService.startPruning(); | ||
|
||
expect(setSoftDeletionInterval).toHaveBeenCalled(); | ||
expect(scheduleHardDeletion).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.