-
Notifications
You must be signed in to change notification settings - Fork 1
/
feature_test.go
97 lines (76 loc) · 2.38 KB
/
feature_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
package feature_test
import (
"fmt"
"math"
"testing"
"github.com/jimdoescode/feature"
)
type mock struct {
uid []byte
always bool
}
func (u mock) GetGroupIdentifier() []byte {
return u.uid
}
func (u mock) AlwaysEnabled() bool {
return u.always
}
// This mock has a group identifier that should enable the "feature"
// flag at 50% and disable the "another_feature" flag at 50%
var enabled mock = mock{[]byte{48, 208, 243, 152, 32}, false}
// This mock has a group identifier that should disable the "feature" flag at 50%.
var disabled mock = mock{[]byte{12, 254, 105, 216, 171}, false}
func TestEnabledFor(t *testing.T) {
flag := feature.NewFlag("feature", 0.5)
if !flag.EnabledFor(enabled) {
t.Error("Mock not reporting as enabled.")
}
if flag.EnabledFor(disabled) {
t.Error("Mock reporting as enabled.")
}
// Verify that mocks stay in the same buckets on subsequent calls.
if !flag.EnabledFor(enabled) {
t.Error("Mock not reporting as enabled on second attempt.")
}
if flag.EnabledFor(disabled) {
t.Error("Mock reporting as enabled on second attempt.")
}
}
func TestDifferentFlagEnabledFor(t *testing.T) {
flag := feature.NewFlag("another_feature", 0.5)
// Verify that different flags enable different things.
if flag.EnabledFor(enabled) {
t.Error("Mock is reporting as enabled when it shouldn't.")
}
}
func TestAlwaysEnabledFor(t *testing.T) {
flag := feature.NewFlag("feature", 0.5)
if flag.EnabledFor(disabled) {
t.Error("Mock reporting as enabled.")
}
// This mock has a group identifier that should disable the "test" flag
// at 50% but with the always enabled flag it will still report as enabled
always := mock{disabled.GetGroupIdentifier(), true}
if !flag.EnabledFor(always) {
t.Error("Always Enabled mock not reporting as enabled.")
}
}
// Note: This test might be flakey since it's using completely random
// values. They should converge on the flag percentage but might be
// over the desired tolerance sometimes.
func TestEnabled(t *testing.T) {
percent := 0.75
tolerance := 0.01
flag := feature.NewFlag("random_feature", percent)
max := 100000
count := 0
for i := 0; i < max; i++ {
if flag.Enabled() {
count++
}
}
hits := float64(count) / float64(max)
if diff := math.Abs(hits - percent); diff > tolerance {
t.Error(fmt.Sprintf("flag.Enabled exceeds tolerance of %f. Difference is %f after %d executions.", tolerance, diff, max))
}
}