forked from growthbook/growthbook-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperiment_test.go
534 lines (452 loc) · 13.4 KB
/
experiment_test.go
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
package growthbook
import (
"fmt"
"reflect"
"regexp"
"testing"
)
type trackCall struct {
experiment *Experiment
result *Result
}
type tracker struct {
calls []trackCall
cb func(experiment *Experiment, result *Result)
}
func track() *tracker {
t := tracker{[]trackCall{}, nil}
t.cb = func(experiment *Experiment, result *Result) {
t.calls = append(t.calls, trackCall{experiment, result})
}
return &t
}
func TestExperimentTracking(t *testing.T) {
context := NewContext().
WithUserAttributes(Attributes{"id": "1"})
tr := track()
gb := New(context).WithTrackingCallback(tr.cb)
exp1 := NewExperiment("my-tracked-test").WithVariations(0, 1)
exp2 := NewExperiment("my-other-tracked-test").WithVariations(0, 1)
res1 := gb.Run(exp1)
gb.Run(exp1)
gb.Run(exp1)
res4 := gb.Run(exp2)
context = context.WithUserAttributes(Attributes{"id": "2"})
res5 := gb.Run(exp2)
if len(tr.calls) != 3 {
t.Errorf("expected 3 calls to tracking callback, got %d", len(tr.calls))
} else {
if !reflect.DeepEqual(tr.calls[0], trackCall{exp1, res1}) {
t.Errorf("unexpected callback result")
}
if !reflect.DeepEqual(tr.calls[1], trackCall{exp2, res4}) {
t.Errorf("unexpected callback result")
}
if !reflect.DeepEqual(tr.calls[2], trackCall{exp2, res5}) {
t.Errorf("unexpected callback result")
}
}
}
func TestExperimentForcesVariationFromOverrides(t *testing.T) {
forceVal := 1
context := NewContext().
WithOverrides(ExperimentOverrides{
"forced-test": &ExperimentOverride{
Force: &forceVal,
}})
gb := New(context).
WithAttributes(Attributes{"id": "6"})
res := gb.Run(NewExperiment("forced-test").WithVariations(0, 1))
if res.VariationID != 1 {
t.Error("expected variation ID 1, got", res.VariationID)
}
if res.InExperiment != true {
t.Error("expected InExperiment to be true")
}
if res.HashUsed != false {
t.Error("expected HashUsed to be false")
}
}
func TestExperimentCoverageFromOverrides(t *testing.T) {
overrideVal := 0.01
context := NewContext().
WithOverrides(ExperimentOverrides{
"my-test": &ExperimentOverride{
Coverage: &overrideVal,
}})
gb := New(context).
WithAttributes(Attributes{"id": "1"})
res := gb.Run(NewExperiment("my-test").WithVariations(0, 1))
if res.VariationID != 0 {
t.Error("expected variation ID 0, got", res.VariationID)
}
if res.InExperiment != false {
t.Error("expected InExperiment to be false")
}
}
func TestExperimentDoesNotTrackWhenForcedWithOverrides(t *testing.T) {
context := NewContext().
WithUserAttributes(Attributes{"id": "6"})
tr := track()
gb := New(context).WithTrackingCallback(tr.cb)
exp := NewExperiment("forced-test").WithVariations(0, 1)
forceVal := 1
context = context.WithOverrides(ExperimentOverrides{
"forced-test": &ExperimentOverride{Force: &forceVal},
})
gb.Run(exp)
if len(tr.calls) != 0 {
t.Error("expected 0 calls to tracking callback, got ", len(tr.calls))
}
}
func TestExperimentURLFromOverrides(t *testing.T) {
urlRe := regexp.MustCompile(`^\/path`)
context := NewContext().
WithUserAttributes(Attributes{"id": "1"}).
WithOverrides(ExperimentOverrides{
"my-test": &ExperimentOverride{URL: urlRe},
})
gb := New(context)
if gb.Run(NewExperiment("my-test").WithVariations(0, 1)).InExperiment != false {
t.Error("expected InExperiment to be false")
}
}
func TestExperimentFiltersUserGroups(t *testing.T) {
context := NewContext().
WithUserAttributes(Attributes{"id": "123"}).
WithGroups(map[string]bool{
"alpha": true,
"beta": true,
"internal": false,
"qa": false,
})
gb := New(context)
exp := NewExperiment("my-test").
WithVariations(0, 1).
WithGroups("internal", "qa")
if gb.Run(exp).InExperiment != false {
t.Error("1: expected InExperiment to be false")
}
exp = NewExperiment("my-test").
WithVariations(0, 1).
WithGroups("internal", "qa", "beta")
if gb.Run(exp).InExperiment != true {
t.Error("2: expected InExperiment to be true")
}
exp = NewExperiment("my-test").
WithVariations(0, 1)
if gb.Run(exp).InExperiment != true {
t.Error("3: expected InExperiment to be true")
}
}
func TestExperimentSetsAttributes(t *testing.T) {
attributes := Attributes{
"id": "1",
"browser": "firefox",
}
gb := New(nil).WithAttributes(attributes)
if !reflect.DeepEqual(gb.Attributes(), attributes) {
t.Error("expected attributes to match")
}
}
func TestExperimentCustomIncludeCallback(t *testing.T) {
context := NewContext().
WithUserAttributes(Attributes{"id": "1"})
gb := New(context)
exp := NewExperiment("my-test").
WithVariations(0, 1).
WithIncludeFunction(func() bool { return false })
if gb.Run(exp).InExperiment != false {
t.Error("expected InExperiment to be false")
}
}
func TestExperimentTrackingSkippedWhenContextDisabled(t *testing.T) {
context := NewContext().
WithUserAttributes(Attributes{"id": "1"}).
WithEnabled(false)
tr := track()
gb := New(context).WithTrackingCallback(tr.cb)
gb.Run(NewExperiment("disabled-test").WithVariations(0, 1))
if len(tr.calls) != 0 {
t.Errorf("expected 0 calls to tracking callback, got %d", len(tr.calls))
}
}
func TestExperimentQuerystringForceDisablsTracking(t *testing.T) {
context := NewContext().
WithUserAttributes(Attributes{"id": "1"}).
WithURL(mustParseUrl("http://example.com?forced-test-qs=1"))
tr := track()
gb := New(context).WithTrackingCallback(tr.cb)
gb.Run(NewExperiment("forced-test-qs").WithVariations(0, 1))
if len(tr.calls) != 0 {
t.Errorf("expected 0 calls to tracking callback, got %d", len(tr.calls))
}
}
func TestExperimentURLTargeting(t *testing.T) {
context := NewContext().
WithUserAttributes(Attributes{"id": "1"}).
WithURL(mustParseUrl("http://example.com"))
gb := New(context)
exp := NewExperiment("my-test").
WithVariations(0, 1).
WithURL(regexp.MustCompile("^/post/[0-9]+"))
check := func(icase int, e *Experiment, inExperiment bool, value interface{}) {
result := gb.Run(e)
if result.InExperiment != inExperiment {
t.Errorf("%d: expected InExperiment = %v, got %v",
icase, inExperiment, result.InExperiment)
}
if !reflect.DeepEqual(result.Value, value) {
t.Errorf("%d: expected value = %v, got %v",
icase, value, result.Value)
}
}
check(1, exp, false, 0)
context = context.WithURL(mustParseUrl("http://example.com/post/123"))
check(2, exp, true, 1)
exp.URL = regexp.MustCompile("http://example.com/post/[0-9]+")
check(3, exp, true, 1)
}
func TestExperimentIgnoresDraftExperiments(t *testing.T) {
context := NewContext().
WithUserAttributes(Attributes{"id": "1"})
gb := New(context)
exp := NewExperiment("my-test").
WithStatus(DraftStatus).
WithVariations(0, 1)
res1 := gb.Run(exp)
context = context.WithURL(mustParseUrl("http://example.com/?my-test=1"))
res2 := gb.Run(exp)
if res1.InExperiment != false {
t.Error("1: expected InExperiment to be false")
}
if res1.HashUsed != false {
t.Error("1: expected HashUsed to be false")
}
if res1.Value != 0 {
t.Errorf("1: expected Value to be 0, got %v", res1.Value)
}
if res2.InExperiment != true {
t.Error("2: expected InExperiment to be true")
}
if res2.HashUsed != false {
t.Error("2: expected HashUsed to be false")
}
if res2.Value != 1 {
t.Errorf("2: expected Value to be 1, got %v", res2.Value)
}
}
func TestExperimentIgnoresStoppedExperimentsUnlessForced(t *testing.T) {
context := NewContext().
WithUserAttributes(Attributes{"id": "1"})
gb := New(context)
expLose := NewExperiment("my-test").
WithStatus(StoppedStatus).
WithVariations(0, 1, 2)
expWin := NewExperiment("my-test").
WithStatus(StoppedStatus).
WithVariations(0, 1, 2).
WithForce(2)
res1 := gb.Run(expLose)
res2 := gb.Run(expWin)
if res1.InExperiment != false {
t.Error("1: expected InExperiment to be false")
}
if res1.HashUsed != false {
t.Error("1: expected HashUsed to be false")
}
if res1.Value != 0 {
t.Errorf("1: expected Value to be 0, got %v", res1.Value)
}
if res2.InExperiment != true {
t.Error("2: expected InExperiment to be true")
}
if res2.HashUsed != false {
t.Error("2: expected HashUsed to be false")
}
if res2.Value != 2 {
t.Errorf("2: expected Value to be 2, got %v", res2.Value)
}
}
func TestExperimentDoesEvenWeighting(t *testing.T) {
context := NewContext()
gb := New(context)
// Full coverage
exp := NewExperiment("my-test").WithVariations(0, 1)
variations := map[string]int{
"0": 0,
"1": 0,
"-1": 0,
}
countVariations(t, context, gb, exp, 1000, variations)
if variations["0"] != 503 {
t.Errorf("1: expected variations[\"0\"] to be 503, got %v", variations["0"])
}
// Reduced coverage
exp = exp.WithCoverage(0.4)
variations = map[string]int{
"0": 0,
"1": 0,
"-1": 0,
}
countVariations(t, context, gb, exp, 10000, variations)
if variations["0"] != 2044 {
t.Errorf("2: expected variations[\"0\"] to be 2044, got %v", variations["0"])
}
if variations["1"] != 1980 {
t.Errorf("2: expected variations[\"1\"] to be 1980, got %v", variations["0"])
}
if variations["-1"] != 5976 {
t.Errorf("2: expected variations[\"0\"] to be 5976, got %v", variations["0"])
}
// 3-way
exp = exp.WithCoverage(0.6).WithVariations(0, 1, 2)
variations = map[string]int{
"0": 0,
"1": 0,
"2": 0,
"-1": 0,
}
countVariations(t, context, gb, exp, 10000, variations)
expected := map[string]int{
"-1": 3913,
"0": 2044,
"1": 2000,
"2": 2043,
}
if !reflect.DeepEqual(variations, expected) {
t.Errorf("3: expected variations counts %#v, git %#v", expected, variations)
}
}
func TestExperimentForcesMultipleVariationsAtOnce(t *testing.T) {
context := NewContext().
WithUserAttributes(Attributes{"id": "1"})
gb := New(context)
exp := NewExperiment("my-test").
WithVariations(0, 1)
res1 := gb.Run(exp)
commonCheck(t, 1, res1, true, true, 1)
gb = gb.WithForcedVariations(ForcedVariationsMap{
"my-test": 0,
})
res2 := gb.Run(exp)
commonCheck(t, 2, res2, true, false, 0)
gb = gb.WithForcedVariations(nil)
res3 := gb.Run(exp)
commonCheck(t, 3, res3, true, true, 1)
}
func TestExperimentOnceForcesAllVariationsInQAMode(t *testing.T) {
context := NewContext().
WithUserAttributes(Attributes{"id": "1"}).
WithQAMode(true)
gb := New(context)
exp := NewExperiment("my-test").
WithVariations(0, 1)
res1 := gb.Run(exp)
commonCheck(t, 1, res1, false, false, 0)
// Still works if explicitly forced
context = context.WithForcedVariations(ForcedVariationsMap{"my-test": 1})
res2 := gb.Run(exp)
commonCheck(t, 2, res2, true, false, 1)
// Works if the experiment itself is forced
exp2 := NewExperiment("my-test-2").WithVariations(0, 1).WithForce(1)
res3 := gb.Run(exp2)
commonCheck(t, 3, res3, true, false, 1)
}
func TestExperimentFiresSubscriptionsCorrectly(t *testing.T) {
context := NewContext().
WithUserAttributes(Attributes{"id": "1"})
gb := New(context)
fired := false
checkFired := func(icase int, f bool) {
if fired != f {
t.Errorf("%d: expected fired to be %v", icase, f)
}
}
unsubscriber := gb.Subscribe(func(experiment *Experiment, result *Result) {
fired = true
})
checkFired(1, false)
exp := NewExperiment("my-test").WithVariations(0, 1)
// Should fire when user is put in an experiment
gb.Run(exp)
checkFired(2, true)
// Does not fire if nothing has changed
fired = false
gb.Run(exp)
checkFired(3, false)
// Does not fire after unsubscribed
unsubscriber()
exp2 := NewExperiment("other-test").WithVariations(0, 1)
gb.Run(exp2)
checkFired(4, false)
}
func TestExperimentStoresAssignedVariations(t *testing.T) {
context := NewContext().
WithUserAttributes(Attributes{"id": "1"})
gb := New(context)
gb.Run(NewExperiment("my-test").WithVariations(0, 1))
gb.Run(NewExperiment("my-test-3").WithVariations(0, 1))
assignedVars := gb.GetAllResults()
if len(assignedVars) != 2 {
t.Errorf("expected len(assignedVars) to be 2, got %d", len(assignedVars))
}
if assignedVars["my-test"].Result.VariationID != 1 {
t.Errorf("expected assignedVars[\"my-test\"] to be 1, got %d",
assignedVars["my-test"].Result.VariationID)
}
if assignedVars["my-test-3"].Result.VariationID != 0 {
t.Errorf("expected assignedVars[\"my-test-3\"] to be 0, got %d",
assignedVars["my-test-3"].Result.VariationID)
}
}
func TestExperimentDoesNotHaveBiasWhenUsingNamespaces(t *testing.T) {
context := NewContext().
WithUserAttributes(Attributes{"id": "1"})
gb := New(context)
variations := map[string]int{
"0": 0,
"1": 0,
"-1": 0,
}
exp := NewExperiment("my-test").
WithVariations(0, 1).
WithNamespace(&Namespace{"namespace", 0.0, 0.5})
countVariations(t, context, gb, exp, 10000, variations)
expected := map[string]int{
"-1": 4973,
"0": 2538,
"1": 2489,
}
if !reflect.DeepEqual(variations, expected) {
t.Errorf("expected variations counts %#v, git %#v", expected, variations)
}
}
func commonCheck(t *testing.T, icase int, res *Result,
inExperiment bool, hashUsed bool, value FeatureValue) {
if res.InExperiment != inExperiment {
t.Errorf("%d: expected InExperiment to be %v", icase, inExperiment)
}
if res.HashUsed != hashUsed {
t.Errorf("%d: expected HashUsed to be %v", icase, hashUsed)
}
if res.Value != value {
t.Errorf("3: expected Value to be %#v, got %#v", value, res.Value)
}
}
func countVariations(t *testing.T, context *Context, gb *GrowthBook,
exp *Experiment, runs int, variations map[string]int) {
for i := 0; i < runs; i++ {
context = context.WithUserAttributes(Attributes{"id": fmt.Sprint(i)})
res := gb.Run(exp)
v := -1
ok := false
if res.InExperiment {
v, ok = res.Value.(int)
if !ok {
t.Error("non int feature result")
}
}
variations[fmt.Sprint(v)]++
}
}