From ab3e6e47d3e9b36f97e4ffd8bbed82e1657eb637 Mon Sep 17 00:00:00 2001 From: Ahmed Etefy Date: Wed, 23 Jun 2021 15:32:52 +0200 Subject: [PATCH 1/5] fix(serverless): wrapEventFunction does not await for async --- packages/serverless/src/gcpfunction/events.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/serverless/src/gcpfunction/events.ts b/packages/serverless/src/gcpfunction/events.ts index 45ef6a1004a3..0161a161ecc2 100644 --- a/packages/serverless/src/gcpfunction/events.ts +++ b/packages/serverless/src/gcpfunction/events.ts @@ -29,7 +29,7 @@ function _wrapEventFunction( flushTimeout: 2000, ...wrapOptions, }; - return (data, context, callback) => { + return async (data, context, callback) => { const transaction = startTransaction({ name: context.eventType, op: 'gcp.function.event', @@ -67,7 +67,7 @@ function _wrapEventFunction( return (fn as EventFunctionWithCallback)(data, context, newCallback); } - void Promise.resolve() + await Promise.resolve() .then(() => (fn as EventFunction)(data, context)) .then( result => { From 04862a79c91ca9b772fff666feb8607567b677ea Mon Sep 17 00:00:00 2001 From: Daniel Griesser Date: Thu, 24 Jun 2021 10:40:34 +0200 Subject: [PATCH 2/5] fix: Promise function --- .../src/gcpfunction/cloud_events.ts | 4 +- packages/serverless/src/gcpfunction/events.ts | 6 +-- packages/serverless/test/gcpfunction.test.ts | 43 +++++++++++++++++++ 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/packages/serverless/src/gcpfunction/cloud_events.ts b/packages/serverless/src/gcpfunction/cloud_events.ts index a1cb4eee56d5..6b7200c89783 100644 --- a/packages/serverless/src/gcpfunction/cloud_events.ts +++ b/packages/serverless/src/gcpfunction/cloud_events.ts @@ -59,7 +59,7 @@ function _wrapCloudEventFunction( } transaction.finish(); - void flush(options.flushTimeout) + flush(options.flushTimeout) .then(() => { callback(...args); }) @@ -72,7 +72,7 @@ function _wrapCloudEventFunction( return (fn as CloudEventFunctionWithCallback)(context, newCallback); } - void Promise.resolve() + return Promise.resolve() .then(() => (fn as CloudEventFunction)(context)) .then( result => { diff --git a/packages/serverless/src/gcpfunction/events.ts b/packages/serverless/src/gcpfunction/events.ts index 0161a161ecc2..f9c7a0af1c5b 100644 --- a/packages/serverless/src/gcpfunction/events.ts +++ b/packages/serverless/src/gcpfunction/events.ts @@ -29,7 +29,7 @@ function _wrapEventFunction( flushTimeout: 2000, ...wrapOptions, }; - return async (data, context, callback) => { + return (data, context, callback) => { const transaction = startTransaction({ name: context.eventType, op: 'gcp.function.event', @@ -54,7 +54,7 @@ function _wrapEventFunction( } transaction.finish(); - void flush(options.flushTimeout) + flush(options.flushTimeout) .then(() => { callback(...args); }) @@ -67,7 +67,7 @@ function _wrapEventFunction( return (fn as EventFunctionWithCallback)(data, context, newCallback); } - await Promise.resolve() + return Promise.resolve() .then(() => (fn as EventFunction)(data, context)) .then( result => { diff --git a/packages/serverless/test/gcpfunction.test.ts b/packages/serverless/test/gcpfunction.test.ts index e5f6094b8172..4b5a00199796 100644 --- a/packages/serverless/test/gcpfunction.test.ts +++ b/packages/serverless/test/gcpfunction.test.ts @@ -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); From 1fd7b562eefa84948a652ddfb55724ccedcbb814 Mon Sep 17 00:00:00 2001 From: Daniel Griesser Date: Thu, 24 Jun 2021 10:44:17 +0200 Subject: [PATCH 3/5] ref: More returns --- packages/serverless/src/gcpfunction/cloud_events.ts | 4 ++-- packages/serverless/src/gcpfunction/events.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/serverless/src/gcpfunction/cloud_events.ts b/packages/serverless/src/gcpfunction/cloud_events.ts index 6b7200c89783..3f98a1e8448d 100644 --- a/packages/serverless/src/gcpfunction/cloud_events.ts +++ b/packages/serverless/src/gcpfunction/cloud_events.ts @@ -76,10 +76,10 @@ function _wrapCloudEventFunction( .then(() => (fn as CloudEventFunction)(context)) .then( result => { - newCallback(null, result); + return newCallback(null, result); }, err => { - newCallback(err, undefined); + return newCallback(err, undefined); }, ); }; diff --git a/packages/serverless/src/gcpfunction/events.ts b/packages/serverless/src/gcpfunction/events.ts index f9c7a0af1c5b..81158320996e 100644 --- a/packages/serverless/src/gcpfunction/events.ts +++ b/packages/serverless/src/gcpfunction/events.ts @@ -71,10 +71,10 @@ function _wrapEventFunction( .then(() => (fn as EventFunction)(data, context)) .then( result => { - newCallback(null, result); + return newCallback(null, result); }, err => { - newCallback(err, undefined); + return newCallback(err, undefined); }, ); }; From 5ddf309fcee49ed103d080aec27d99287c9e5333 Mon Sep 17 00:00:00 2001 From: Ahmed Etefy Date: Thu, 24 Jun 2021 13:01:13 +0200 Subject: [PATCH 4/5] Added void to flush --- packages/serverless/src/gcpfunction/cloud_events.ts | 2 +- packages/serverless/src/gcpfunction/events.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/serverless/src/gcpfunction/cloud_events.ts b/packages/serverless/src/gcpfunction/cloud_events.ts index 3f98a1e8448d..47d19d18adca 100644 --- a/packages/serverless/src/gcpfunction/cloud_events.ts +++ b/packages/serverless/src/gcpfunction/cloud_events.ts @@ -59,7 +59,7 @@ function _wrapCloudEventFunction( } transaction.finish(); - flush(options.flushTimeout) + void flush(options.flushTimeout) .then(() => { callback(...args); }) diff --git a/packages/serverless/src/gcpfunction/events.ts b/packages/serverless/src/gcpfunction/events.ts index 81158320996e..2b4c19842a89 100644 --- a/packages/serverless/src/gcpfunction/events.ts +++ b/packages/serverless/src/gcpfunction/events.ts @@ -54,7 +54,7 @@ function _wrapEventFunction( } transaction.finish(); - flush(options.flushTimeout) + void flush(options.flushTimeout) .then(() => { callback(...args); }) From b86ad963f32701b0575ed90cfea49c1b0fbe099c Mon Sep 17 00:00:00 2001 From: Daniel Griesser Date: Fri, 25 Jun 2021 09:15:04 +0200 Subject: [PATCH 5/5] ref: Code style --- packages/serverless/src/gcpfunction/cloud_events.ts | 8 ++------ packages/serverless/src/gcpfunction/events.ts | 8 ++------ 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/packages/serverless/src/gcpfunction/cloud_events.ts b/packages/serverless/src/gcpfunction/cloud_events.ts index 47d19d18adca..429cea995f1a 100644 --- a/packages/serverless/src/gcpfunction/cloud_events.ts +++ b/packages/serverless/src/gcpfunction/cloud_events.ts @@ -75,12 +75,8 @@ function _wrapCloudEventFunction( return Promise.resolve() .then(() => (fn as CloudEventFunction)(context)) .then( - result => { - return newCallback(null, result); - }, - err => { - return newCallback(err, undefined); - }, + result => newCallback(null, result), + err => newCallback(err, undefined), ); }; } diff --git a/packages/serverless/src/gcpfunction/events.ts b/packages/serverless/src/gcpfunction/events.ts index 2b4c19842a89..6b9c7b9b14a7 100644 --- a/packages/serverless/src/gcpfunction/events.ts +++ b/packages/serverless/src/gcpfunction/events.ts @@ -70,12 +70,8 @@ function _wrapEventFunction( return Promise.resolve() .then(() => (fn as EventFunction)(data, context)) .then( - result => { - return newCallback(null, result); - }, - err => { - return newCallback(err, undefined); - }, + result => newCallback(null, result), + err => newCallback(err, undefined), ); }; }