This repository has been archived by the owner on Dec 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
rollout_test.go
94 lines (87 loc) · 4.16 KB
/
rollout_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
package rollout
import (
"fmt"
"testing"
)
func TestUserId(t *testing.T) {
rollout := &client{currentData: make(map[string]string)}
rollout.swapData([]byte(`{"feature:hello": "0|1|"}`))
groups := []string{"foo"}
assert(t, rollout.FeatureActive("hello", 1, groups), "feature should be active")
assert(t, !rollout.FeatureActive("hello", 2, groups), "feature should not be active")
assert(t, !rollout.FeatureActive("nosuchfeature", 1, groups), "feature should not be active")
}
func TestGroup(t *testing.T) {
rollout := &client{currentData: make(map[string]string)}
rollout.swapData([]byte(`{"feature:hello": "0||foo"}`))
groupA := []string{"foo"}
groupB := []string{"bar"}
assert(t, rollout.FeatureActive("hello", 1, groupA), "feature should be active")
assert(t, !rollout.FeatureActive("hello", 1, groupB), "feature should not be active")
assert(t, !rollout.FeatureActive("nosuchfeature", 1, groupA), "feature should not be active")
}
func TestAll(t *testing.T) {
rollout := &client{currentData: make(map[string]string)}
rollout.swapData([]byte(`{"feature:hello": "0||all"}`))
group := []string{""}
assert(t, rollout.FeatureActive("hello", 1, group), "feature should be active")
assert(t, rollout.FeatureActive("hello", 2, group), "feature should be active")
}
func TestPercentage(t *testing.T) {
rollout := &client{currentData: make(map[string]string)}
groups := []string{"foo"}
rollout.swapData([]byte(`{"feature:hello": "0.0||"}`))
assert(t, !rollout.FeatureActive("hello", 1, groups), "feature should not be active")
assert(t, !rollout.FeatureActive("hello", 2, groups), "feature should not be active")
rollout.swapData([]byte(`{"feature:hello": "25.0||"}`))
assert(t, rollout.FeatureActive("hello", 1, groups), "feature should be active")
assert(t, !rollout.FeatureActive("hello", 26, groups), "feature should not be active")
assert(t, !rollout.FeatureActive("nosuchfeature", 1, groups), "feature should not be active")
rollout.swapData([]byte(`{"feature:hello": "50||"}`))
assert(t, rollout.FeatureActive("hello", 1, groups), "feature should be active")
assert(t, rollout.FeatureActive("hello", 26, groups), "feature should be active")
assert(t, !rollout.FeatureActive("nosuchfeature", 1, groups), "feature should not be active")
}
func TestRawPercentage(t *testing.T) {
rollout := &client{currentData: make(map[string]string)}
t.Run("happy path", func(t *testing.T) {
rollout.swapData([]byte(`{"feature:percentage_123456": "75.0||"}`))
r, e := rollout.RawPercentage("percentage_123456")
assert(t, r == 75.0, "rawpercentage happy path test broken")
assert(t, e == nil, "rawpercentage happy path test got non-nil error")
})
t.Run("missing org", func(t *testing.T) {
rollout.swapData([]byte(`{"feature:percentage_123456": "75.0||"}`))
r, e := rollout.RawPercentage("percentage_111111")
assert(t, r == 0.0, "rawpercentage missing org test broken")
assert(t, e != nil, "rawpercentage missing org test got nil error")
assert(t,
e.Error() == "feature not found",
fmt.Sprintf("rawpercentage missing org test got wrong error: %v", e.Error()))
assert(t, e == ErrFeatureNotFound, "wrong error returned")
})
t.Run("bad data (splits)", func(t *testing.T) {
rollout.swapData([]byte(`{"feature:percentage_123456": "75.0|"}`))
r, e := rollout.RawPercentage("percentage_123456")
assert(t, r == 0.0, "rawpercentage bad data (splits) test broken")
assert(t, e != nil, "rawpercentage bad data (splits) test got nil error")
assert(t,
e.Error() == "invalid value for feature:percentage_123456: 75.0|",
fmt.Sprintf("rawpercentage missing org test got wrong error: %v", e.Error()))
})
t.Run("bad data (values)", func(t *testing.T) {
rollout.swapData([]byte(`{"feature:percentage_123456": "garbage||"}`))
r, e := rollout.RawPercentage("percentage_123456")
assert(t, r == 0.0, "rawpercentage bad data (values) test broken")
assert(t, e != nil, "rawpercentage bad data (values) test got nil error")
assert(t,
e.Error() == "rollout invalid percentage: garbage",
fmt.Sprintf("rawpercentage missing org test got wrong error: %v", e.Error()))
})
}
func assert(t *testing.T, condition bool, explanation interface{}) {
if !condition {
t.Error(explanation)
t.FailNow()
}
}