|
| 1 | +import { describe, it, beforeEach, afterEach } from 'mocha'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import sinon from 'sinon'; |
| 4 | + |
| 5 | +import OperationIdCleanerCommand from '../../../src/commands/cleaners/operation-id-cleaner-command.js'; |
| 6 | +import { |
| 7 | + OPERATION_ID_COMMAND_CLEANUP_TIME_MILLS, |
| 8 | + OPERATION_ID_FILES_FOR_REMOVAL_MAX_NUMBER, |
| 9 | + OPERATION_ID_MEMORY_CLEANUP_TIME_MILLS, |
| 10 | + OPERATION_ID_STATUS, |
| 11 | +} from '../../../src/constants/constants.js'; |
| 12 | + |
| 13 | +describe('OperationIdCleanerCommand', () => { |
| 14 | + let clock; |
| 15 | + let operationIdService; |
| 16 | + let repositoryModuleManager; |
| 17 | + let logger; |
| 18 | + let command; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + clock = sinon.useFakeTimers(new Date('2023-01-01T00:00:00Z').getTime()); |
| 22 | + |
| 23 | + operationIdService = { |
| 24 | + getOperationIdMemoryCacheSizeBytes: sinon.stub().returns(1024), |
| 25 | + getOperationIdFileCacheSizeBytes: sinon.stub().resolves(2048), |
| 26 | + removeExpiredOperationIdMemoryCache: sinon.stub().resolves(512), |
| 27 | + removeExpiredOperationIdFileCache: sinon.stub().resolves(3), |
| 28 | + }; |
| 29 | + |
| 30 | + repositoryModuleManager = { |
| 31 | + removeOperationIdRecord: sinon.stub().resolves(), |
| 32 | + }; |
| 33 | + |
| 34 | + logger = { |
| 35 | + debug: sinon.spy(), |
| 36 | + info: sinon.spy(), |
| 37 | + warn: sinon.spy(), |
| 38 | + error: sinon.spy(), |
| 39 | + }; |
| 40 | + |
| 41 | + command = new OperationIdCleanerCommand({ |
| 42 | + logger, |
| 43 | + repositoryModuleManager, |
| 44 | + operationIdService, |
| 45 | + fileService: {}, |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(() => { |
| 50 | + clock.restore(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('cleans memory with 1h TTL and files with 24h TTL while reporting footprint', async () => { |
| 54 | + await command.execute(); |
| 55 | + |
| 56 | + expect(operationIdService.getOperationIdMemoryCacheSizeBytes.calledOnce).to.be.true; |
| 57 | + expect(operationIdService.getOperationIdFileCacheSizeBytes.calledOnce).to.be.true; |
| 58 | + |
| 59 | + expect( |
| 60 | + repositoryModuleManager.removeOperationIdRecord.calledWith( |
| 61 | + Date.now() - OPERATION_ID_COMMAND_CLEANUP_TIME_MILLS, |
| 62 | + [OPERATION_ID_STATUS.COMPLETED, OPERATION_ID_STATUS.FAILED], |
| 63 | + ), |
| 64 | + ).to.be.true; |
| 65 | + |
| 66 | + expect( |
| 67 | + operationIdService.removeExpiredOperationIdMemoryCache.calledWith( |
| 68 | + OPERATION_ID_MEMORY_CLEANUP_TIME_MILLS, |
| 69 | + ), |
| 70 | + ).to.be.true; |
| 71 | + |
| 72 | + expect( |
| 73 | + operationIdService.removeExpiredOperationIdFileCache.calledWith( |
| 74 | + OPERATION_ID_COMMAND_CLEANUP_TIME_MILLS, |
| 75 | + OPERATION_ID_FILES_FOR_REMOVAL_MAX_NUMBER, |
| 76 | + ), |
| 77 | + ).to.be.true; |
| 78 | + |
| 79 | + expect(logger.debug.called).to.be.true; |
| 80 | + }); |
| 81 | + |
| 82 | + it('handles missing memory cache gracefully', async () => { |
| 83 | + operationIdService.getOperationIdMemoryCacheSizeBytes.throws(new Error('no memory cache')); |
| 84 | + await command.execute(); |
| 85 | + |
| 86 | + expect( |
| 87 | + repositoryModuleManager.removeOperationIdRecord.calledWith( |
| 88 | + Date.now() - OPERATION_ID_COMMAND_CLEANUP_TIME_MILLS, |
| 89 | + [OPERATION_ID_STATUS.COMPLETED, OPERATION_ID_STATUS.FAILED], |
| 90 | + ), |
| 91 | + ).to.be.true; |
| 92 | + |
| 93 | + expect( |
| 94 | + operationIdService.removeExpiredOperationIdFileCache.calledWith( |
| 95 | + OPERATION_ID_COMMAND_CLEANUP_TIME_MILLS, |
| 96 | + OPERATION_ID_FILES_FOR_REMOVAL_MAX_NUMBER, |
| 97 | + ), |
| 98 | + ).to.be.true; |
| 99 | + }); |
| 100 | +}); |
0 commit comments