Skip to content

test: wait for background work to finish in integration tests #2851

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

Merged
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
18 changes: 18 additions & 0 deletions tests/utils/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,12 @@ export async function invokeFunction(
process.env[key] = environment[key]
})

let resolveInvocation, rejectInvocation
const invocationPromise = new Promise((resolve, reject) => {
resolveInvocation = resolve
rejectInvocation = reject
})

const response = (await execute({
event: {
headers: headers || {},
Expand All @@ -405,8 +411,20 @@ export async function invokeFunction(
},
lambdaFunc: { handler },
timeoutMs: 4_000,
onInvocationEnd: (error) => {
// lambda-local resolve promise return from execute when response is closed
// but we should wait for tracked background work to finish
// before resolving the promise to allow background work to finish
if (error) {
rejectInvocation(error)
} else {
resolveInvocation()
}
},
})) as LambdaResponse

await invocationPromise
Copy link
Contributor Author

@pieh pieh Apr 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

possibly instead of awaiting here we should return this promise, but before https://github.com/netlify/serverless-functions-api/pull/358 landed we were basically waiting for stream to close here and we were blocking stream closing to ensure background work

making change to return this promise as part of results instead of awaiting would like require test adjustments, so I think it's better to restore previous behavior


const responseHeaders = Object.entries(response.multiValueHeaders || {}).reduce(
(prev, [key, value]) => ({
...prev,
Expand Down
Loading