Skip to content

Commit

Permalink
Bug 1714449 - Feature API does not fire update event on startup r=k88…
Browse files Browse the repository at this point in the history
…hudson

Differential Revision: https://phabricator.services.mozilla.com/D116777
  • Loading branch information
piatra committed Jun 7, 2021
1 parent 628a0fb commit 744c62a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
10 changes: 10 additions & 0 deletions toolkit/components/nimbus/lib/ExperimentStore.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ class ExperimentStore extends SharedDataMap {
super(sharedDataKey || DEFAULT_STORE_ID, options);
}

async init() {
await super.init();

this.getAllActive().forEach(experiment => {
experiment.featureIds?.forEach(feature =>
this._emitFeatureUpdate(feature, "feature-experiment-loaded")
);
});
}

/**
* Given a feature identifier, find an active experiment that matches that feature identifier.
* This assumes, for now, that there is only one active experiment per feature per browser.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,3 +523,33 @@ add_task(async function test_isEnabled_backwards_compatible() {
);
Assert.ok(exposureSpy.calledOnce, "Exposure event sent");
});

add_task(async function test_onUpdate_before_store_ready() {
let sandbox = sinon.createSandbox();
const feature = new ExperimentFeature("foo", FAKE_FEATURE_MANIFEST);
const stub = sandbox.stub();
const manager = ExperimentFakes.manager();
sandbox.stub(ExperimentAPI, "_store").get(() => manager.store);
sandbox.stub(manager.store, "getAllActive").returns([
ExperimentFakes.experiment("foo-experiment", {
featureIds: ["foo"],
branch: { slug: "control", feature: { featureId: "foo", value: null } },
}),
]);

// We register for updates before the store finished loading experiments
// from disk
feature.onUpdate(stub);

await manager.onStartup();

Assert.ok(
stub.calledOnce,
"Called on startup after loading experiments from disk"
);
Assert.equal(
stub.firstCall.args[1],
"feature-experiment-loaded",
"Called for the expected reason"
);
});

0 comments on commit 744c62a

Please sign in to comment.