1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
"use strict";
const { ExperimentFakes } = ChromeUtils.import(
"resource://testing-common/MSTestUtils.jsm"
);
const { NormandyTestUtils } = ChromeUtils.import(
"resource://testing-common/NormandyTestUtils.jsm"
);
const { Sampling } = ChromeUtils.import(
"resource://gre/modules/components-utils/Sampling.jsm"
);
const { ClientEnvironment } = ChromeUtils.import(
"resource://normandy/lib/ClientEnvironment.jsm"
);
const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm");
/**
* The normal case: Enrollment of a new experiment
*/
add_task(async function test_add_to_store() {
const manager = ExperimentFakes.manager();
const recipe = ExperimentFakes.recipe("foo");
await manager.onStartup();
await manager.enroll(recipe);
const experiment = manager.store.get("foo");
Assert.ok(experiment, "should add an experiment with slug foo");
Assert.ok(
recipe.branches.includes(experiment.branch),
"should choose a branch from the recipe.branches"
);
Assert.equal(experiment.active, true, "should set .active = true");
Assert.ok(
NormandyTestUtils.isUuid(experiment.enrollmentId),
"should add a valid enrollmentId"
);
});
add_task(
async function test_setExperimentActive_sendEnrollmentTelemetry_called() {
const manager = ExperimentFakes.manager();
const sandbox = sinon.createSandbox();
sandbox.spy(manager, "setExperimentActive");
sandbox.spy(manager, "sendEnrollmentTelemetry");
await manager.onStartup();
await manager.onStartup();
await manager.enroll(ExperimentFakes.recipe("foo"));
const experiment = manager.store.get("foo");
Assert.equal(
manager.setExperimentActive.calledWith(experiment),
true,
"should call setExperimentActive after an enrollment"
);
Assert.equal(
manager.sendEnrollmentTelemetry.calledWith(experiment),
true,
"should call sendEnrollmentTelemetry after an enrollment"
);
}
);
/**
* Failure cases:
* - slug conflict
* - group conflict
*/
add_task(async function test_failure_name_conflict() {
const manager = ExperimentFakes.manager();
const sandbox = sinon.createSandbox();
sandbox.spy(manager, "sendFailureTelemetry");
await manager.onStartup();
// simulate adding a previouly enrolled experiment
manager.store.addExperiment(ExperimentFakes.experiment("foo"));
await Assert.rejects(
manager.enroll(ExperimentFakes.recipe("foo")),
/An experiment with the slug "foo" already exists/,
"should throw if a conflicting experiment exists"
);
Assert.equal(
manager.sendFailureTelemetry.calledWith(
"enrollFailed",
"foo",
"name-conflict"
),
true,
"should send failure telemetry if a conflicting experiment exists"
);
});
add_task(async function test_failure_group_conflict() {
const manager = ExperimentFakes.manager();
const sandbox = sinon.createSandbox();
sandbox.spy(manager, "sendFailureTelemetry");
await manager.onStartup();
// Two conflicting branches that both have the group "pink"
// These should not be allowed to exist simultaneously.
const existingBranch = {
slug: "treatment",
feature: { featureId: "pink", enabled: true },
};
const newBranch = {
slug: "treatment",
feature: { featureId: "pink", enabled: true },
};
// simulate adding an experiment with a conflicting group "pink"
manager.store.addExperiment(
ExperimentFakes.experiment("foo", {
branch: existingBranch,
})
);
// ensure .enroll chooses the special branch with the conflict
sandbox.stub(manager, "chooseBranch").returns(newBranch);
await Assert.rejects(
manager.enroll(ExperimentFakes.recipe("bar", { branches: [newBranch] })),
/An experiment with a conflicting feature already exists/,
"should throw if there is a feature conflict"
);
Assert.equal(
manager.sendFailureTelemetry.calledWith(
"enrollFailed",
"bar",
"feature-conflict"
),
true,
"should send failure telemetry if a feature conflict exists"
);
});
add_task(async function test_sampling_check() {
const manager = ExperimentFakes.manager();
let recipe = ExperimentFakes.recipe("foo", { bucketConfig: null });
const sandbox = sinon.createSandbox();
sandbox.stub(Sampling, "bucketSample").resolves(true);
sandbox.replaceGetter(ClientEnvironment, "userId", () => 42);
Assert.ok(
!manager.isInBucketAllocation(recipe.bucketConfig),
"fails for no bucket config"
);
recipe = ExperimentFakes.recipe("foo2", {
bucketConfig: { randomizationUnit: "foo" },
});
Assert.ok(
!manager.isInBucketAllocation(recipe.bucketConfig),
"fails for unknown randomizationUnit"
);
recipe = ExperimentFakes.recipe("foo3");
const result = await manager.isInBucketAllocation(recipe.bucketConfig);
Assert.equal(
Sampling.bucketSample.callCount,
1,
"it should call bucketSample"
);
Assert.ok(result, "result should be true");
const { args } = Sampling.bucketSample.firstCall;
Assert.equal(args[0][0], 42, "called with expected randomization id");
Assert.equal(
args[0][1],
recipe.bucketConfig.namespace,
"called with expected namespace"
);
Assert.equal(
args[1],
recipe.bucketConfig.start,
"called with expected start"
);
Assert.equal(
args[2],
recipe.bucketConfig.count,
"called with expected count"
);
Assert.equal(
args[3],
recipe.bucketConfig.total,
"called with expected total"
);
sandbox.reset();
});
add_task(async function enroll_in_reference_aw_experiment() {
const SYNC_DATA_PREF = "messaging-system.syncdatastore.data";
Services.prefs.clearUserPref(SYNC_DATA_PREF);
let dir = await OS.File.getCurrentDirectory();
let src = OS.Path.join(dir, "reference_aboutwelcome_experiment_content.json");
let bytes = await OS.File.read(src);
const decoder = new TextDecoder();
const content = JSON.parse(decoder.decode(bytes));
// Create two dummy branches with the content from disk
const branches = ["treatment-a", "treatment-b"].map(slug => ({
slug,
ratio: 1,
feature: { value: content, enabled: true, featureId: "aboutwelcome" },
}));
let recipe = ExperimentFakes.recipe("reference-aw", { branches });
// Ensure we get enrolled
recipe.bucketConfig.count = recipe.bucketConfig.total;
const manager = ExperimentFakes.manager();
await manager.onStartup();
await manager.enroll(recipe);
Assert.ok(manager.store.get("reference-aw"), "Successful onboarding");
let prefValue = Services.prefs.getStringPref(SYNC_DATA_PREF);
Assert.ok(
prefValue,
"aboutwelcome experiment enrollment should be stored to prefs"
);
// In case some regression causes us to store a significant amount of data
// in prefs.
Assert.ok(prefValue.length < 3498, "Make sure we don't bloat the prefs");
});
|