Skip to content

Commit

Permalink
Fix bug where disabling background triggers did nothing.
Browse files Browse the repository at this point in the history
  • Loading branch information
taeold committed Nov 7, 2022
1 parent 98e23ed commit d020bc1
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
13 changes: 13 additions & 0 deletions scripts/integration-helpers/framework.ts
Expand Up @@ -53,6 +53,7 @@ interface ConnectionInfo {

export interface FrameworkOptions {
emulators?: {
hub: ConnectionInfo;
database: ConnectionInfo;
firestore: ConnectionInfo;
functions: ConnectionInfo;
Expand All @@ -63,6 +64,7 @@ export interface FrameworkOptions {
}

export class EmulatorEndToEndTest {
emulatorHubPort = 0;
rtdbEmulatorHost = "localhost";
rtdbEmulatorPort = 0;
firestoreEmulatorHost = "localhost";
Expand All @@ -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;
Expand Down Expand Up @@ -409,4 +412,14 @@ export class TriggerEndToEndTest extends EmulatorEndToEndTest {
}
}, interval);
}

disableBackgroundTriggers(): Promise<Response> {
const url = `http://localhost:${this.emulatorHubPort}/functions/disableBackgroundTriggers`;
return fetch(url, { method: "PUT" });
}

enableBackgroundTriggers(): Promise<Response> {
const url = `http://localhost:${this.emulatorHubPort}/functions/enableBackgroundTriggers`;
return fetch(url, { method: "PUT" });
}
}
62 changes: 62 additions & 0 deletions scripts/triggers-end-to-end-tests/tests.ts
Expand Up @@ -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);
});
});
});
5 changes: 5 additions & 0 deletions src/emulator/functionsEmulator.ts
Expand Up @@ -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}`);

Expand Down

0 comments on commit d020bc1

Please sign in to comment.