From d020bc1d71de725396409e718a4b7e78895b0ae7 Mon Sep 17 00:00:00 2001 From: Daniel Young Lee Date: Sun, 6 Nov 2022 13:33:22 -0800 Subject: [PATCH 1/3] Fix bug where disabling background triggers did nothing. --- scripts/integration-helpers/framework.ts | 13 +++++ scripts/triggers-end-to-end-tests/tests.ts | 62 ++++++++++++++++++++++ src/emulator/functionsEmulator.ts | 5 ++ 3 files changed, 80 insertions(+) diff --git a/scripts/integration-helpers/framework.ts b/scripts/integration-helpers/framework.ts index a5aeb59df3f..e0a90602944 100644 --- a/scripts/integration-helpers/framework.ts +++ b/scripts/integration-helpers/framework.ts @@ -53,6 +53,7 @@ interface ConnectionInfo { export interface FrameworkOptions { emulators?: { + hub: ConnectionInfo; database: ConnectionInfo; firestore: ConnectionInfo; functions: ConnectionInfo; @@ -63,6 +64,7 @@ export interface FrameworkOptions { } export class EmulatorEndToEndTest { + emulatorHubPort = 0; rtdbEmulatorHost = "localhost"; rtdbEmulatorPort = 0; firestoreEmulatorHost = "localhost"; @@ -87,6 +89,7 @@ export class EmulatorEndToEndTest { if (!config.emulators) { return; } + this.emulatorHubPort = config.emulators.hub?.port; this.rtdbEmulatorPort = config.emulators.database?.port; this.firestoreEmulatorPort = config.emulators.firestore?.port; this.functionsEmulatorPort = config.emulators.functions?.port; @@ -409,4 +412,14 @@ export class TriggerEndToEndTest extends EmulatorEndToEndTest { } }, interval); } + + disableBackgroundTriggers(): Promise { + const url = `http://localhost:${this.emulatorHubPort}/functions/disableBackgroundTriggers`; + return fetch(url, { method: "PUT" }); + } + + enableBackgroundTriggers(): Promise { + const url = `http://localhost:${this.emulatorHubPort}/functions/enableBackgroundTriggers`; + return fetch(url, { method: "PUT" }); + } } diff --git a/scripts/triggers-end-to-end-tests/tests.ts b/scripts/triggers-end-to-end-tests/tests.ts index a0dc6928a12..fc0ee330e21 100755 --- a/scripts/triggers-end-to-end-tests/tests.ts +++ b/scripts/triggers-end-to-end-tests/tests.ts @@ -429,4 +429,66 @@ describe("function triggers", () => { const v2response = await test.invokeHttpFunction("onreqv2timeout"); expect(v2response.status).to.equal(500); }); + + describe("disable/enableBackgroundTriggers", () => { + before(() => { + test.resetCounts(); + }); + + it("should disable all background triggers", async function (this) { + this.timeout(TEST_SETUP_TIMEOUT); + + const response = await test.disableBackgroundTriggers(); + expect(response.status).to.equal(200); + + await new Promise((resolve) => setTimeout(resolve, EMULATORS_WRITE_DELAY_MS)); + + await Promise.all([ + test.writeToRtdb(), + test.writeToFirestore(), + test.writeToPubsub(), + test.writeToAuth(), + test.writeToDefaultStorage(), + ]); + + await new Promise((resolve) => setTimeout(resolve, EMULATORS_WRITE_DELAY_MS * 2)); + + expect(test.rtdbTriggerCount).to.equal(0); + expect(test.rtdbV2TriggerCount).to.eq(0); + expect(test.firestoreTriggerCount).to.equal(0); + expect(test.pubsubTriggerCount).to.equal(0); + expect(test.pubsubV2TriggerCount).to.equal(0); + expect(test.authTriggerCount).to.equal(0); + expect(test.storageFinalizedTriggerCount).to.equal(0); + expect(test.storageV2FinalizedTriggerCount).to.equal(0); + }); + + it("should re-enable all background triggers", async function (this) { + this.timeout(TEST_SETUP_TIMEOUT); + + const response = await test.enableBackgroundTriggers(); + expect(response.status).to.equal(200); + + await new Promise((resolve) => setTimeout(resolve, EMULATORS_WRITE_DELAY_MS)); + + await Promise.all([ + test.writeToRtdb(), + test.writeToFirestore(), + test.writeToPubsub(), + test.writeToAuth(), + test.writeToDefaultStorage(), + ]); + + await new Promise((resolve) => setTimeout(resolve, EMULATORS_WRITE_DELAY_MS * 3)); + + expect(test.rtdbTriggerCount).to.equal(1); + expect(test.rtdbV2TriggerCount).to.eq(1); + expect(test.firestoreTriggerCount).to.equal(1); + expect(test.pubsubTriggerCount).to.equal(1); + expect(test.pubsubV2TriggerCount).to.equal(1); + expect(test.authTriggerCount).to.equal(1); + expect(test.storageFinalizedTriggerCount).to.equal(1); + expect(test.storageV2FinalizedTriggerCount).to.equal(1); + }); + }); }); diff --git a/src/emulator/functionsEmulator.ts b/src/emulator/functionsEmulator.ts index 1896ef14bdf..6f19031f858 100644 --- a/src/emulator/functionsEmulator.ts +++ b/src/emulator/functionsEmulator.ts @@ -1397,6 +1397,11 @@ export class FunctionsEmulator implements EmulatorInstance { } const record = this.getTriggerRecordByKey(triggerId); + // If trigger is disabled, exit early + if (!record.enabled) { + res.status(204).send("Background triggers are currently disabled."); + return; + } const trigger = record.def; logger.debug(`Accepted request ${method} ${req.url} --> ${triggerId}`); From 2cc4fbe12025ad52778e06a5382359c10583c122 Mon Sep 17 00:00:00 2001 From: Daniel Lee Date: Sun, 6 Nov 2022 16:53:25 -0800 Subject: [PATCH 2/3] Add changelog. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0516d8cf0f..471263aa793 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,2 +1,3 @@ - Add support for object list using certain Admin SDKs (#5208) - Fixes source token expiration issue by acquiring new source token upon expiration. +- Fix bug where disabling background triggers did nothing. #5221 From ae9d325bb0ea78acbfb05fcefa14d582e247e2c2 Mon Sep 17 00:00:00 2001 From: Daniel Lee Date: Sun, 6 Nov 2022 16:54:09 -0800 Subject: [PATCH 3/3] Fix typo. --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 471263aa793..8f0c65207e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,3 @@ - Add support for object list using certain Admin SDKs (#5208) - Fixes source token expiration issue by acquiring new source token upon expiration. -- Fix bug where disabling background triggers did nothing. #5221 +- Fix bug where disabling background triggers did nothing. (#5221)