From b7927e9bb5051d91489c454e97725775b17b9070 Mon Sep 17 00:00:00 2001 From: SevenWaysDP Date: Tue, 23 Jul 2024 17:50:35 +0200 Subject: [PATCH 1/4] refactor: add expiration to RPC message requests --- apps/server/src/infra/rabbitmq/rpc-message-producer.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/apps/server/src/infra/rabbitmq/rpc-message-producer.ts b/apps/server/src/infra/rabbitmq/rpc-message-producer.ts index 8a239b2cbcd..3bfb1fafc96 100644 --- a/apps/server/src/infra/rabbitmq/rpc-message-producer.ts +++ b/apps/server/src/infra/rabbitmq/rpc-message-producer.ts @@ -27,11 +27,15 @@ export abstract class RpcMessageProducer { } protected createRequest(event: string, payload: unknown) { + // expiration should be greater than timeout + const expiration = this.timeout > 0 ? this.timeout / 0.5 + this.timeout : undefined; + return { exchange: this.exchange, routingKey: event, payload, timeout: this.timeout, + expiration, }; } } From 0c57193aad8f86a9a195f34000405b04f559712c Mon Sep 17 00:00:00 2001 From: SevenWaysDP Date: Wed, 24 Jul 2024 09:21:50 +0200 Subject: [PATCH 2/4] fix expiration calculation in RPC message producer --- apps/server/src/infra/rabbitmq/rpc-message-producer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/server/src/infra/rabbitmq/rpc-message-producer.ts b/apps/server/src/infra/rabbitmq/rpc-message-producer.ts index 3bfb1fafc96..a4926d5946e 100644 --- a/apps/server/src/infra/rabbitmq/rpc-message-producer.ts +++ b/apps/server/src/infra/rabbitmq/rpc-message-producer.ts @@ -28,7 +28,7 @@ export abstract class RpcMessageProducer { protected createRequest(event: string, payload: unknown) { // expiration should be greater than timeout - const expiration = this.timeout > 0 ? this.timeout / 0.5 + this.timeout : undefined; + const expiration = this.timeout > 0 ? this.timeout / 2 + this.timeout : undefined; return { exchange: this.exchange, From 10cf7793fd7a5c737c880632eba59ef79fe80c35 Mon Sep 17 00:00:00 2001 From: Sergej Hoffmann <97111299+SevenWaysDP@users.noreply.github.com> Date: Wed, 24 Jul 2024 10:08:33 +0200 Subject: [PATCH 3/4] Update apps/server/src/infra/rabbitmq/rpc-message-producer.ts Co-authored-by: Max <53796487+dyedwiper@users.noreply.github.com> --- apps/server/src/infra/rabbitmq/rpc-message-producer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/server/src/infra/rabbitmq/rpc-message-producer.ts b/apps/server/src/infra/rabbitmq/rpc-message-producer.ts index a4926d5946e..84b74f6e173 100644 --- a/apps/server/src/infra/rabbitmq/rpc-message-producer.ts +++ b/apps/server/src/infra/rabbitmq/rpc-message-producer.ts @@ -28,7 +28,7 @@ export abstract class RpcMessageProducer { protected createRequest(event: string, payload: unknown) { // expiration should be greater than timeout - const expiration = this.timeout > 0 ? this.timeout / 2 + this.timeout : undefined; + const expiration = this.timeout > 0 ? this.timeout * 1.5 : undefined; return { exchange: this.exchange, From 02aeddff737b5ca15d831c773563bcda36af6e63 Mon Sep 17 00:00:00 2001 From: SevenWaysDP Date: Wed, 24 Jul 2024 10:51:46 +0200 Subject: [PATCH 4/4] fix tests --- .../preview.producer.spec.ts | 18 ++++++++++------ .../rabbitmq/rpc-message-producer.spec.ts | 1 + .../service/files-storage.producer.spec.ts | 21 +++++++++++++++---- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/apps/server/src/infra/preview-generator/preview.producer.spec.ts b/apps/server/src/infra/preview-generator/preview.producer.spec.ts index 47adea158a6..e6c5b680aff 100644 --- a/apps/server/src/infra/preview-generator/preview.producer.spec.ts +++ b/apps/server/src/infra/preview-generator/preview.producer.spec.ts @@ -12,9 +12,10 @@ import { PreviewProducer } from './preview.producer'; describe('PreviewProducer', () => { let module: TestingModule; let service: PreviewProducer; - let configService: DeepMocked; let amqpConnection: DeepMocked; + const timeout = 10000; + beforeAll(async () => { await setupEntities(); module = await Test.createTestingModule({ @@ -30,14 +31,20 @@ describe('PreviewProducer', () => { }, { provide: ConfigService, - useValue: createMock(), + useValue: createMock({ + get: jest.fn().mockImplementation((key: string) => { + if (key === 'INCOMING_REQUEST_TIMEOUT') { + return timeout; + } + throw new Error('Config key not found'); + }), + }), }, ], }).compile(); service = module.get(PreviewProducer); amqpConnection = module.get(AmqpConnection); - configService = module.get(ConfigService); }); afterAll(async () => { @@ -47,6 +54,7 @@ describe('PreviewProducer', () => { afterEach(() => { jest.resetAllMocks(); }); + it('should be defined', () => { expect(service).toBeDefined(); }); @@ -54,8 +62,6 @@ describe('PreviewProducer', () => { describe('generate', () => { describe('when valid params are passed and amqp connection return with a message', () => { const setup = () => { - const timeout = 10000; - const params: PreviewFileOptions = { originFilePath: 'file/test.jpeg', previewFilePath: 'preview/text.webp', @@ -67,13 +73,13 @@ describe('PreviewProducer', () => { const message = []; amqpConnection.request.mockResolvedValueOnce({ message }); - configService.get.mockReturnValue(timeout); const expectedParams = { exchange: FilesPreviewExchange, routingKey: FilesPreviewEvents.GENERATE_PREVIEW, payload: params, timeout, + expiration: timeout * 1.5, }; return { params, expectedParams, message }; diff --git a/apps/server/src/infra/rabbitmq/rpc-message-producer.spec.ts b/apps/server/src/infra/rabbitmq/rpc-message-producer.spec.ts index b2e94ea676b..fc0e2629485 100644 --- a/apps/server/src/infra/rabbitmq/rpc-message-producer.spec.ts +++ b/apps/server/src/infra/rabbitmq/rpc-message-producer.spec.ts @@ -59,6 +59,7 @@ describe('RpcMessageProducer', () => { routingKey: TestEvent, payload: params, timeout, + expiration: timeout * 1.5, }; return { params, expectedParams, message }; diff --git a/apps/server/src/modules/files-storage-client/service/files-storage.producer.spec.ts b/apps/server/src/modules/files-storage-client/service/files-storage.producer.spec.ts index 12b476fb84c..517b0be1593 100644 --- a/apps/server/src/modules/files-storage-client/service/files-storage.producer.spec.ts +++ b/apps/server/src/modules/files-storage-client/service/files-storage.producer.spec.ts @@ -12,7 +12,6 @@ import { FilesStorageProducer } from './files-storage.producer'; describe('FilesStorageProducer', () => { let module: TestingModule; let service: FilesStorageProducer; - let configService: DeepMocked; let amqpConnection: DeepMocked; const timeout = 10000; @@ -32,21 +31,30 @@ describe('FilesStorageProducer', () => { }, { provide: ConfigService, - useValue: createMock(), + useValue: createMock({ + get: jest.fn().mockImplementation((key: string) => { + if (key === 'INCOMING_REQUEST_TIMEOUT_COPY_API') { + return timeout; + } + throw new Error('Config key not found'); + }), + }), }, ], }).compile(); service = module.get(FilesStorageProducer); amqpConnection = module.get(AmqpConnection); - configService = module.get(ConfigService); - configService.get.mockReturnValue(timeout); }); afterAll(async () => { await module.close(); }); + afterEach(() => { + jest.clearAllMocks(); + }); + describe('copyFilesOfParent', () => { describe('when amqpConnection return with error in response', () => { const setup = () => { @@ -111,6 +119,7 @@ describe('FilesStorageProducer', () => { routingKey: FilesStorageEvents.COPY_FILES_OF_PARENT, payload: params, timeout, + expiration: timeout * 1.5, }; return { params, expectedParams, message }; @@ -163,6 +172,7 @@ describe('FilesStorageProducer', () => { routingKey: FilesStorageEvents.LIST_FILES_OF_PARENT, payload: parentId, timeout, + expiration: timeout * 1.5, }; const message = []; @@ -221,6 +231,7 @@ describe('FilesStorageProducer', () => { routingKey: FilesStorageEvents.DELETE_FILES_OF_PARENT, payload: parentId, timeout, + expiration: timeout * 1.5, }; return { parentId, message, expectedParams }; @@ -275,6 +286,7 @@ describe('FilesStorageProducer', () => { routingKey: FilesStorageEvents.DELETE_FILES, payload: [recordId], timeout, + expiration: timeout * 1.5, }; return { recordId, message, expectedParams }; }; @@ -329,6 +341,7 @@ describe('FilesStorageProducer', () => { routingKey: FilesStorageEvents.REMOVE_CREATORID_OF_FILES, payload: creatorId, timeout, + expiration: timeout * 1.5, }; return { creatorId, message, expectedParams };