From 3d9a09139aaabc6e38a48282cbd4884c85b8bf7c Mon Sep 17 00:00:00 2001 From: Dmitry Balabanov Date: Mon, 14 Mar 2022 14:05:51 +0000 Subject: [PATCH 1/2] fix(logger): add invocation mode in e2e tests --- .../tests/e2e/basicFeatures.middy.test.ts | 2 +- packages/logger/tests/helpers/e2eUtils.ts | 26 ++++++++++++++----- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/packages/logger/tests/e2e/basicFeatures.middy.test.ts b/packages/logger/tests/e2e/basicFeatures.middy.test.ts index 1bb0708562..91f860e9a9 100644 --- a/packages/logger/tests/e2e/basicFeatures.middy.test.ts +++ b/packages/logger/tests/e2e/basicFeatures.middy.test.ts @@ -72,7 +72,7 @@ describe(`logger E2E tests basic functionalities (middy) for runtime: ${runtime} logGroupName = outputs[STACK_OUTPUT_LOG_GROUP]; // Invoke the function twice (one for cold start, another for warm start) - invocationLogs = await invokeFunction(functionName, 2); + invocationLogs = await invokeFunction(functionName, 2, 'SEQUENTIAL'); console.log('logGroupName', logGroupName); diff --git a/packages/logger/tests/helpers/e2eUtils.ts b/packages/logger/tests/helpers/e2eUtils.ts index 67de1343f0..76e39976d6 100644 --- a/packages/logger/tests/helpers/e2eUtils.ts +++ b/packages/logger/tests/helpers/e2eUtils.ts @@ -68,11 +68,10 @@ export const deployStack = async (stackArtifact: CloudFormationStackArtifact ): return result.outputs; }; -export const invokeFunction = async (functionName: string, times: number = 1): Promise => { +export const invokeFunction = async (functionName: string, times: number = 1, invocationMode: 'PARALLEL' | 'SEQUENTIAL' = 'PARALLEL'): Promise => { const invocationLogs: InvocationLogs[] = []; - const promises = []; - - for (let i = 0; i < times; i++) { + + const promiseFactory = () : Promise => { const invokePromise = lambdaClient .invoke({ FunctionName: functionName, @@ -86,9 +85,15 @@ export const invokeFunction = async (functionName: string, times: number = 1): P throw new Error('No LogResult field returned in the response of Lambda invocation. This should not happen.'); } }); - promises.push(invokePromise); - } - await Promise.all(promises); + + return invokePromise; + }; + + const promiseFactories = Array.from({ length: times }, () => promiseFactory ); + const invocation = invocationMode == 'PARALLEL' + ? Promise.all(promiseFactories.map(factory => factory())) + : chainPromises(promiseFactories); + await invocation; return invocationLogs; }; @@ -106,3 +111,10 @@ export const destroyStack = async (app: App, stack: Stack): Promise => { quiet: true, }); }; + +const chainPromises = async (promiseFactories: (() => Promise)[]) : Promise => { + let chain = Promise.resolve(); + promiseFactories.forEach(factory => chain = chain.then(factory)); + + return chain; +}; From aeea13e007148f32fc601af96d6e07490565dded Mon Sep 17 00:00:00 2001 From: Dmitry Balabanov Date: Mon, 14 Mar 2022 14:06:19 +0000 Subject: [PATCH 2/2] fix(logger): enable cold start e2e test --- packages/logger/tests/e2e/basicFeatures.middy.test.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/logger/tests/e2e/basicFeatures.middy.test.ts b/packages/logger/tests/e2e/basicFeatures.middy.test.ts index 91f860e9a9..fb3976658b 100644 --- a/packages/logger/tests/e2e/basicFeatures.middy.test.ts +++ b/packages/logger/tests/e2e/basicFeatures.middy.test.ts @@ -103,12 +103,11 @@ describe(`logger E2E tests basic functionalities (middy) for runtime: ${runtime} for (const message of coldStartLogMessages) { expect(message).toContain(`"cold_start":true`); } - // TODO: There is an issue with the way in which functions are invoked that always forces a cold start. (#590) - // Link to tracked issue: https://github.com/awslabs/aws-lambda-powertools-typescript/issues/590 - // const normalLogMessages = invocationLogs[1].getFunctionLogs(LEVEL.INFO); - // for (const message of normalLogMessages) { - // expect(message).not.toContain(`"cold_start":true`); - // } + + const normalLogMessages = invocationLogs[1].getFunctionLogs(LEVEL.INFO); + for (const message of normalLogMessages) { + expect(message).not.toContain(`"cold_start":true`); + } }, TEST_CASE_TIMEOUT); });