From eae863f0aedaa7ce15ee7bf45d4b09fb7e641d24 Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Thu, 15 Feb 2024 11:34:36 +0100 Subject: [PATCH] chore(tests): change timeout reporting in idempotency e2e tests --- .../e2e/idempotentDecorator.test.FunctionCode.ts | 4 +++- .../tests/e2e/idempotentDecorator.test.ts | 10 ++++++---- .../makeHandlerIdempotent.test.FunctionCode.ts | 9 ++++----- .../tests/e2e/makeHandlerIdempotent.test.ts | 9 +++++---- packages/testing/src/TestInvocationLogs.ts | 16 ++++++++++++++++ 5 files changed, 34 insertions(+), 14 deletions(-) diff --git a/packages/idempotency/tests/e2e/idempotentDecorator.test.FunctionCode.ts b/packages/idempotency/tests/e2e/idempotentDecorator.test.FunctionCode.ts index 8dd29c0b29..fc0909c747 100644 --- a/packages/idempotency/tests/e2e/idempotentDecorator.test.FunctionCode.ts +++ b/packages/idempotency/tests/e2e/idempotentDecorator.test.FunctionCode.ts @@ -80,7 +80,9 @@ class DefaultLambda implements LambdaInterface { ): Promise { logger.addContext(context); - await new Promise((resolve) => setTimeout(resolve, 1500)); + await new Promise((resolve) => + setTimeout(resolve, context.getRemainingTimeInMillis()) + ); logger.info('Processed event', { details: event.foo }); diff --git a/packages/idempotency/tests/e2e/idempotentDecorator.test.ts b/packages/idempotency/tests/e2e/idempotentDecorator.test.ts index ec740e0f03..cfee5257a1 100644 --- a/packages/idempotency/tests/e2e/idempotentDecorator.test.ts +++ b/packages/idempotency/tests/e2e/idempotentDecorator.test.ts @@ -74,7 +74,8 @@ describe('Idempotency e2e test decorator, default settings', () => { function: { entry: lambdaFunctionCodeFilePath, handler: 'handlerTimeout', - timeout: Duration.seconds(2), + timeout: Duration.seconds(3), + memorySize: 512, }, }, { @@ -284,10 +285,11 @@ describe('Idempotency e2e test decorator, default settings', () => { }); expect(idempotencyRecord.Items?.[0].status).toEqual('COMPLETED'); - // During the first invocation the handler should be called, so the logs should contain 1 log - expect(functionLogs[0]).toHaveLength(2); - expect(functionLogs[0][0]).toContain('Task timed out after'); + // During the first invocation the function should timeout so the logs should not contain any log and the report log should contain a timeout message + expect(functionLogs[0]).toHaveLength(0); + expect(logs[0].getReportLog()).toMatch(/Status: timeout$/); + // During the second invocation the handler should be called and complete, so the logs should contain 1 log expect(functionLogs[1]).toHaveLength(1); expect(TestInvocationLogs.parseFunctionLog(functionLogs[1][0])).toEqual( expect.objectContaining({ diff --git a/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.FunctionCode.ts b/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.FunctionCode.ts index 499eda7cca..d5afb7a101 100644 --- a/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.FunctionCode.ts +++ b/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.FunctionCode.ts @@ -65,12 +65,11 @@ export const handlerTimeout = middy( logger.addContext(context); if (event.invocation === 0) { - await new Promise((resolve) => setTimeout(resolve, 4000)); + await new Promise((resolve) => { + setTimeout(resolve, context.getRemainingTimeInMillis()); + }); } - - logger.info('Processed event', { - details: event.foo, - }); + logger.info('Processed event', { details: event.foo }); return { foo: event.foo, diff --git a/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts b/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts index 1af54e589d..0f37d5c88d 100644 --- a/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts +++ b/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts @@ -74,7 +74,8 @@ describe(`Idempotency E2E tests, middy middleware usage`, () => { function: { entry: lambdaFunctionCodeFilePath, handler: 'handlerTimeout', - timeout: Duration.seconds(2), + timeout: Duration.seconds(3), + memorySize: 512, }, }, { @@ -263,9 +264,9 @@ describe(`Idempotency E2E tests, middy middleware usage`, () => { }); expect(idempotencyRecords.Items?.[0].status).toEqual('COMPLETED'); - // During the first invocation the function should timeout so the logs should contain 2 logs - expect(functionLogs[0]).toHaveLength(2); - expect(functionLogs[0][0]).toContain('Task timed out after'); + // During the first invocation the function should timeout so the logs should not contain any log and the report log should contain a timeout message + expect(functionLogs[0]).toHaveLength(0); + expect(logs[0].getReportLog()).toMatch(/Status: timeout$/); // During the second invocation the handler should be called and complete, so the logs should // contain 1 log expect(functionLogs[1]).toHaveLength(1); diff --git a/packages/testing/src/TestInvocationLogs.ts b/packages/testing/src/TestInvocationLogs.ts index fa396c98eb..10fcf957a0 100644 --- a/packages/testing/src/TestInvocationLogs.ts +++ b/packages/testing/src/TestInvocationLogs.ts @@ -105,6 +105,22 @@ class TestInvocationLogs { return filteredLogs; } + /** + * Return the log that contains the report of the function `REPORT RequestId` + */ + public getReportLog(): string { + const endLogIndex = TestInvocationLogs.getReportLogIndex(this.logs); + + return this.logs[endLogIndex]; + } + + /** + * The index of the log that contains the report of the function `REPORT RequestId` + */ + public static getReportLogIndex(logs: string[]): number { + return logs.findIndex((log) => log.startsWith('REPORT RequestId')); + } + public static getStartLogIndex(logs: string[]): number { return logs.findIndex((log) => log.startsWith('START RequestId')); }