Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(logger): enable sequential invocation in e2e test #658

Merged
merged 2 commits into from Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
};