Skip to content

fix(eventarc): resolve the emulator host when the client is constructed - #3223

Open
kyungseopk1m wants to merge 2 commits into
firebase:mainfrom
kyungseopk1m:fix/eventarc-emulator-host-caching
Open

fix(eventarc): resolve the emulator host when the client is constructed#3223
kyungseopk1m wants to merge 2 commits into
firebase:mainfrom
kyungseopk1m:fix/eventarc-emulator-host-caching

Conversation

@kyungseopk1m

Copy link
Copy Markdown
Contributor

Discussion

Follows the same fix as #3167 (issue #3059), which moved CLOUD_TASKS_EMULATOR_HOST in FunctionsApiClient to construction time.

EventarcApiClient reads process.env.CLOUD_EVENTARC_EMULATOR_HOST on every publish rather than when the client is built, so the routing of an existing channel depends on whatever the environment happens to hold at publish time. Create a production client, set the variable, create an emulator client, then unset it, and both publish to production.

This moves the lookup into the constructor and stores it on a readonly field, matching FunctionsApiClient and AbstractAuthRequestHandler, which both capture their emulator host up front.

Behavior change

Code that sets CLOUD_EVENTARC_EMULATOR_HOST after obtaining a channel will now keep hitting production. Nothing in the SDK, its tests, or #1686 documents that ordering as supported, and Cloud Tasks and Auth already behave this way, but it is user visible so it is worth calling out.

Scope

This keeps ?? instead of the .trim() || undefined normalization used in FunctionsApiClient. There the value is a truthiness flag, checked in four places, so a whitespace only value has to collapse to undefined. Here it is a plain URL prefix used once, and normalizing it would change what '' and ' ' do today, which is unrelated to the caching fix.

Testing

Two tests in test/unit/eventarc/eventarc.spec.ts, built the same way as the multi client test #3167 added. One covers the reported direction, an emulator channel created while the variable is set keeps using the emulator after it is cleared. The other covers the opposite direction, a production channel created before the variable is set keeps using production. Reverting the source fails both. The full unit suite passes at 6066.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the EventarcApiClient to resolve the Eventarc emulator host at construction time instead of at request time, and adds unit tests to verify this behavior. The feedback recommends saving and restoring the original value of process.env.CLOUD_EVENTARC_EMULATOR_HOST in the test hooks to prevent test pollution and potential flakiness.

Comment on lines +174 to +185
let mockAccessToken: string;
let httpStub: sinon.SinonStub;
let accessTokenStub: sinon.SinonStub;

before(() => {
mockAccessToken = utils.generateRandomAccessToken();
accessTokenStub = utils.stubGetAccessToken(mockAccessToken);
});

after(() => {
accessTokenStub?.restore();
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Modifying process.env in tests without restoring its original value can lead to test pollution and flaky tests if other test suites run in the same process. It is highly recommended to capture the original value of CLOUD_EVENTARC_EMULATOR_HOST in the before hook and restore it in the after hook.

    let mockAccessToken: string;
    let httpStub: sinon.SinonStub;
    let accessTokenStub: sinon.SinonStub;
    let originalEmulatorHost: string | undefined;

    before(() => {
      mockAccessToken = utils.generateRandomAccessToken();
      accessTokenStub = utils.stubGetAccessToken(mockAccessToken);
      originalEmulatorHost = process.env.CLOUD_EVENTARC_EMULATOR_HOST;
    });

    after(() => {
      accessTokenStub?.restore();
      if (originalEmulatorHost === undefined) {
        delete process.env.CLOUD_EVENTARC_EMULATOR_HOST;
      } else {
        process.env.CLOUD_EVENTARC_EMULATOR_HOST = originalEmulatorHost;
      }
    });

@kyungseopk1m

Copy link
Copy Markdown
Contributor Author

Done in 5e5a1db.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant