Skip to content

Commit 460994d

Browse files
ahmedetefyHazAT
andauthored
fix(serverless): wrapEventFunction does not await for async code (#3740)
* 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 <daniel.griesser.86@gmail.com>
1 parent 2581f72 commit 460994d

File tree

3 files changed

+49
-14
lines changed

3 files changed

+49
-14
lines changed

packages/serverless/src/gcpfunction/cloud_events.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,11 @@ function _wrapCloudEventFunction(
7272
return (fn as CloudEventFunctionWithCallback)(context, newCallback);
7373
}
7474

75-
void Promise.resolve()
75+
return Promise.resolve()
7676
.then(() => (fn as CloudEventFunction)(context))
7777
.then(
78-
result => {
79-
newCallback(null, result);
80-
},
81-
err => {
82-
newCallback(err, undefined);
83-
},
78+
result => newCallback(null, result),
79+
err => newCallback(err, undefined),
8480
);
8581
};
8682
}

packages/serverless/src/gcpfunction/events.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,11 @@ function _wrapEventFunction(
6767
return (fn as EventFunctionWithCallback)(data, context, newCallback);
6868
}
6969

70-
void Promise.resolve()
70+
return Promise.resolve()
7171
.then(() => (fn as EventFunction)(data, context))
7272
.then(
73-
result => {
74-
newCallback(null, result);
75-
},
76-
err => {
77-
newCallback(err, undefined);
78-
},
73+
result => newCallback(null, result),
74+
err => newCallback(err, undefined),
7975
);
8076
};
8177
}

packages/serverless/test/gcpfunction.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,49 @@ describe('GCPFunction', () => {
206206
});
207207
});
208208

209+
describe('wrapEventFunction() as Promise', () => {
210+
test('successful execution', async () => {
211+
expect.assertions(5);
212+
213+
const func: EventFunction = (_data, _context) =>
214+
new Promise(resolve => {
215+
setTimeout(() => {
216+
resolve(42);
217+
}, 10);
218+
});
219+
const wrappedHandler = wrapEventFunction(func);
220+
await expect(handleEvent(wrappedHandler)).resolves.toBe(42);
221+
expect(Sentry.startTransaction).toBeCalledWith({ name: 'event.type', op: 'gcp.function.event' });
222+
// @ts-ignore see "Why @ts-ignore" note
223+
expect(Sentry.fakeScope.setSpan).toBeCalledWith(Sentry.fakeTransaction);
224+
// @ts-ignore see "Why @ts-ignore" note
225+
expect(Sentry.fakeTransaction.finish).toBeCalled();
226+
expect(Sentry.flush).toBeCalledWith(2000);
227+
});
228+
229+
test('capture error', async () => {
230+
expect.assertions(6);
231+
232+
const error = new Error('wat');
233+
const handler: EventFunction = (_data, _context) =>
234+
new Promise((_, reject) => {
235+
setTimeout(() => {
236+
reject(error);
237+
}, 10);
238+
});
239+
240+
const wrappedHandler = wrapEventFunction(handler);
241+
await expect(handleEvent(wrappedHandler)).rejects.toThrowError(error);
242+
expect(Sentry.startTransaction).toBeCalledWith({ name: 'event.type', op: 'gcp.function.event' });
243+
// @ts-ignore see "Why @ts-ignore" note
244+
expect(Sentry.fakeScope.setSpan).toBeCalledWith(Sentry.fakeTransaction);
245+
expect(Sentry.captureException).toBeCalledWith(error);
246+
// @ts-ignore see "Why @ts-ignore" note
247+
expect(Sentry.fakeTransaction.finish).toBeCalled();
248+
expect(Sentry.flush).toBeCalled();
249+
});
250+
});
251+
209252
describe('wrapEventFunction() with callback', () => {
210253
test('successful execution', async () => {
211254
expect.assertions(5);

0 commit comments

Comments
 (0)