Skip to content

Commit 2814902

Browse files
committed
Bug 1868676 - Part 1: Allow snapshots of multiple contexts. r=barret,omc-reviewers,aminomancer
`ASRouterTargeting` does _not_ include the experiment details that supply `activeExperiments`, `activeRollouts`, etc. Those are combined from additional contexts. This patch makes it easy to do that combination. N.b.: the use of `Proxy` instances makes iteration tricky, so we avoid it entirely. Differential Revision: https://phabricator.services.mozilla.com/D200124
1 parent 5e0f906 commit 2814902

File tree

5 files changed

+67
-13
lines changed

5 files changed

+67
-13
lines changed

browser/components/asrouter/modules/ASRouterTargeting.sys.mjs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,11 +1061,17 @@ export const ASRouterTargeting = {
10611061
* Asynchronous getters are handled. Getters that throw or reject
10621062
* are ignored.
10631063
*
1064-
* @param {object} target - the environment to snapshot.
1065-
* @return {object} snapshot of target with `environment` object and `version`
1066-
* integer.
1064+
* Leftward (earlier) targets supercede rightward (later) targets, just like
1065+
* `TargetingContext.combineContexts`.
1066+
*
1067+
* @param {object} options - object containing:
1068+
* @param {Array<object>|null} options.targets -
1069+
* targeting environments to snapshot; (default: `[ASRouterTargeting.Environment]`)
1070+
* @return {object} snapshot of target with `environment` object and `version` integer.
10671071
*/
1068-
async getEnvironmentSnapshot(target = ASRouterTargeting.Environment) {
1072+
async getEnvironmentSnapshot({
1073+
targets = [ASRouterTargeting.Environment],
1074+
} = {}) {
10691075
async function resolve(object) {
10701076
if (typeof object === "object" && object !== null) {
10711077
if (Array.isArray(object)) {
@@ -1105,7 +1111,13 @@ export const ASRouterTargeting = {
11051111
return object;
11061112
}
11071113

1108-
const environment = await resolve(target);
1114+
// We would like to use `TargetingContext.combineContexts`, but `Proxy`
1115+
// instances complicate iterating with `Object.keys`. Instead, merge by
1116+
// hand after resolving.
1117+
const environment = {};
1118+
for (let target of targets.toReversed()) {
1119+
Object.assign(environment, await resolve(target));
1120+
}
11091121

11101122
// Should we need to migrate in the future.
11111123
const snapshot = { environment, version: 1 };

browser/components/asrouter/tests/xpcshell/test_ASRouterTargeting_snapshot.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ add_task(async function should_ignore_rejections() {
1919
},
2020
};
2121

22-
let snapshot = await ASRouterTargeting.getEnvironmentSnapshot(target);
22+
let snapshot = await ASRouterTargeting.getEnvironmentSnapshot({
23+
targets: [target],
24+
});
2325
Assert.deepEqual(snapshot, { environment: { foo: 1 }, version: 1 });
2426
});
2527

@@ -51,7 +53,9 @@ add_task(async function nested_objects() {
5153
},
5254
};
5355

54-
const snapshot = await ASRouterTargeting.getEnvironmentSnapshot(target);
56+
const snapshot = await ASRouterTargeting.getEnvironmentSnapshot({
57+
targets: [target],
58+
});
5559
Assert.deepEqual(
5660
snapshot,
5761
{
@@ -85,7 +89,9 @@ add_task(async function arrays() {
8589
}),
8690
};
8791

88-
const snapshot = await ASRouterTargeting.getEnvironmentSnapshot(target);
92+
const snapshot = await ASRouterTargeting.getEnvironmentSnapshot({
93+
targets: [target],
94+
});
8995
Assert.deepEqual(
9096
snapshot,
9197
{
@@ -102,6 +108,32 @@ add_task(async function arrays() {
102108
);
103109
});
104110

111+
add_task(async function target_order() {
112+
let target1 = {
113+
foo: 1,
114+
bar: 1,
115+
baz: 1,
116+
};
117+
118+
let target2 = {
119+
foo: 2,
120+
bar: 2,
121+
};
122+
123+
let target3 = {
124+
foo: 3,
125+
};
126+
127+
// target3 supercedes target2; both supercede target1.
128+
let snapshot = await ASRouterTargeting.getEnvironmentSnapshot({
129+
targets: [target3, target2, target1],
130+
});
131+
Assert.deepEqual(snapshot, {
132+
environment: { foo: 3, bar: 2, baz: 1 },
133+
version: 1,
134+
});
135+
});
136+
105137
/*
106138
* NB: This test is last because it manipulates shutdown phases.
107139
*
@@ -132,7 +164,9 @@ add_task(async function should_ignore_rejections() {
132164
},
133165
};
134166

135-
let snapshot = await ASRouterTargeting.getEnvironmentSnapshot(target);
167+
let snapshot = await ASRouterTargeting.getEnvironmentSnapshot({
168+
targets: [target],
169+
});
136170
// `baz` is dropped since we're shutting down by the time it's processed.
137171
Assert.deepEqual(snapshot, { environment: { foo: 1, bar: 2 }, version: 1 });
138172
});

toolkit/components/backgroundtasks/tests/xpcshell/test_backgroundtask_experiments.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,9 @@ add_task(async function test_backgroundtask_Nimbus_targeting() {
277277
currentDate: ASRouterTargeting.Environment.currentDate,
278278
firefoxVersion: ASRouterTargeting.Environment.firefoxVersion,
279279
};
280-
let targetSnapshot = await ASRouterTargeting.getEnvironmentSnapshot(target);
280+
let targetSnapshot = await ASRouterTargeting.getEnvironmentSnapshot({
281+
targets: [target],
282+
});
281283

282284
for (let [targeting, expectedLength] of TARGETING_LIST) {
283285
// Start fresh each time.
@@ -331,7 +333,9 @@ add_task(async function test_backgroundtask_Messaging_targeting() {
331333
currentDate: ASRouterTargeting.Environment.currentDate,
332334
firefoxVersion: ASRouterTargeting.Environment.firefoxVersion,
333335
};
334-
let targetSnapshot = await ASRouterTargeting.getEnvironmentSnapshot(target);
336+
let targetSnapshot = await ASRouterTargeting.getEnvironmentSnapshot({
337+
targets: [target],
338+
});
335339

336340
for (let [targeting, expectedLength] of TARGETING_LIST) {
337341
// Start fresh each time.

toolkit/components/backgroundtasks/tests/xpcshell/test_backgroundtasksutils.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ add_task(
178178
),
179179
firefoxVersion: lazy.ASRouterTargeting.Environment.firefoxVersion,
180180
};
181-
let expected = await lazy.ASRouterTargeting.getEnvironmentSnapshot(target);
181+
let expected = await lazy.ASRouterTargeting.getEnvironmentSnapshot({
182+
targets: [target],
183+
});
182184

183185
let snapshotFile = profile.rootDir.clone();
184186
snapshotFile.append("targeting.snapshot.json");

toolkit/mozapps/update/tests/unit_background_update/test_backgroundupdate_glean.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,9 @@ add_task(async function test_targeting_exists() {
169169
profileAgeCreated: ASRouterTargeting.Environment.profileAgeCreated,
170170
firefoxVersion: ASRouterTargeting.Environment.firefoxVersion,
171171
};
172-
let targetSnapshot = await ASRouterTargeting.getEnvironmentSnapshot(target);
172+
let targetSnapshot = await ASRouterTargeting.getEnvironmentSnapshot({
173+
targets: [target],
174+
});
173175

174176
await do_readTargeting(JSON.stringify(targetSnapshot), reason => {
175177
Assert.equal(true, Glean.backgroundUpdate.targetingExists.testGetValue());

0 commit comments

Comments
 (0)