Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug where disabling background triggers did nothing. #5221

Merged
merged 7 commits into from Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
@@ -1,3 +1,4 @@
- Fixes gzipped file handling in Storage Emulator.
- 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)
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