Skip to content

Commit

Permalink
fix(serverless): wrapEventFunction does not await for async code (#3740)
Browse files Browse the repository at this point in the history
* fix(serverless): wrapEventFunction does not await for async

* fix: Promise function

* ref: More returns

* Added void to flush

* ref: Code style

Co-authored-by: Daniel Griesser <[email protected]>
  • Loading branch information
ahmedetefy and HazAT authored Jun 25, 2021
1 parent 2581f72 commit 460994d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
10 changes: 3 additions & 7 deletions packages/serverless/src/gcpfunction/cloud_events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,11 @@ function _wrapCloudEventFunction(
return (fn as CloudEventFunctionWithCallback)(context, newCallback);
}

void Promise.resolve()
return Promise.resolve()
.then(() => (fn as CloudEventFunction)(context))
.then(
result => {
newCallback(null, result);
},
err => {
newCallback(err, undefined);
},
result => newCallback(null, result),
err => newCallback(err, undefined),
);
};
}
10 changes: 3 additions & 7 deletions packages/serverless/src/gcpfunction/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,11 @@ function _wrapEventFunction(
return (fn as EventFunctionWithCallback)(data, context, newCallback);
}

void Promise.resolve()
return Promise.resolve()
.then(() => (fn as EventFunction)(data, context))
.then(
result => {
newCallback(null, result);
},
err => {
newCallback(err, undefined);
},
result => newCallback(null, result),
err => newCallback(err, undefined),
);
};
}
43 changes: 43 additions & 0 deletions packages/serverless/test/gcpfunction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,49 @@ describe('GCPFunction', () => {
});
});

describe('wrapEventFunction() as Promise', () => {
test('successful execution', async () => {
expect.assertions(5);

const func: EventFunction = (_data, _context) =>
new Promise(resolve => {
setTimeout(() => {
resolve(42);
}, 10);
});
const wrappedHandler = wrapEventFunction(func);
await expect(handleEvent(wrappedHandler)).resolves.toBe(42);
expect(Sentry.startTransaction).toBeCalledWith({ name: 'event.type', op: 'gcp.function.event' });
// @ts-ignore see "Why @ts-ignore" note
expect(Sentry.fakeScope.setSpan).toBeCalledWith(Sentry.fakeTransaction);
// @ts-ignore see "Why @ts-ignore" note
expect(Sentry.fakeTransaction.finish).toBeCalled();
expect(Sentry.flush).toBeCalledWith(2000);
});

test('capture error', async () => {
expect.assertions(6);

const error = new Error('wat');
const handler: EventFunction = (_data, _context) =>
new Promise((_, reject) => {
setTimeout(() => {
reject(error);
}, 10);
});

const wrappedHandler = wrapEventFunction(handler);
await expect(handleEvent(wrappedHandler)).rejects.toThrowError(error);
expect(Sentry.startTransaction).toBeCalledWith({ name: 'event.type', op: 'gcp.function.event' });
// @ts-ignore see "Why @ts-ignore" note
expect(Sentry.fakeScope.setSpan).toBeCalledWith(Sentry.fakeTransaction);
expect(Sentry.captureException).toBeCalledWith(error);
// @ts-ignore see "Why @ts-ignore" note
expect(Sentry.fakeTransaction.finish).toBeCalled();
expect(Sentry.flush).toBeCalled();
});
});

describe('wrapEventFunction() with callback', () => {
test('successful execution', async () => {
expect.assertions(5);
Expand Down

0 comments on commit 460994d

Please sign in to comment.