From 800424bc77223682ad6cdcc9f35080aff30ba91e Mon Sep 17 00:00:00 2001 From: Dmitry Balabanov Date: Mon, 14 Mar 2022 17:19:25 +0100 Subject: [PATCH] fix(logger): enable sequential invocation in e2e test (#658) * fix(logger): add invocation mode in e2e tests * fix(logger): enable cold start e2e test --- .../tests/e2e/basicFeatures.middy.test.ts | 13 +++++----- packages/logger/tests/helpers/e2eUtils.ts | 26 ++++++++++++++----- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/packages/logger/tests/e2e/basicFeatures.middy.test.ts b/packages/logger/tests/e2e/basicFeatures.middy.test.ts index 1bb0708562..fb3976658b 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); @@ -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); }); 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; +};