Skip to content

Commit

Permalink
fix(logger): enable sequential invocation in e2e test (#658)
Browse files Browse the repository at this point in the history
* fix(logger): add invocation mode in e2e tests

* fix(logger): enable cold start e2e test
  • Loading branch information
Dmitry Balabanov authored Mar 14, 2022
1 parent f38a9fa commit 800424b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
13 changes: 6 additions & 7 deletions packages/logger/tests/e2e/basicFeatures.middy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
});

Expand Down
26 changes: 19 additions & 7 deletions packages/logger/tests/helpers/e2eUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,10 @@ export const deployStack = async (stackArtifact: CloudFormationStackArtifact ):
return result.outputs;
};

export const invokeFunction = async (functionName: string, times: number = 1): Promise<InvocationLogs[]> => {
export const invokeFunction = async (functionName: string, times: number = 1, invocationMode: 'PARALLEL' | 'SEQUENTIAL' = 'PARALLEL'): Promise<InvocationLogs[]> => {
const invocationLogs: InvocationLogs[] = [];
const promises = [];

for (let i = 0; i < times; i++) {

const promiseFactory = () : Promise<void> => {
const invokePromise = lambdaClient
.invoke({
FunctionName: functionName,
Expand All @@ -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;
};
Expand All @@ -106,3 +111,10 @@ export const destroyStack = async (app: App, stack: Stack): Promise<void> => {
quiet: true,
});
};

const chainPromises = async (promiseFactories: (() => Promise<void>)[]) : Promise<void> => {
let chain = Promise.resolve();
promiseFactories.forEach(factory => chain = chain.then(factory));

return chain;
};

0 comments on commit 800424b

Please sign in to comment.