-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature_test.go
171 lines (127 loc) · 4.07 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
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
package rollout
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/vmihailenco/msgpack/v4"
)
func TestNewFeature(t *testing.T) {
f := NewFeature("example")
assert.EqualValues(t, "example", f.name)
assert.Zero(t, f.percentage)
assert.Empty(t, f.teamIDs)
}
func TestEncodeDecode(t *testing.T) {
// encode a mix of percentage and teams
in := NewFeature("example")
in.percentage = 50
in.activateTeam(1)
in.activateTeam(2)
in.activateTeam(3)
data, err := msgpack.Marshal(in)
assert.NoError(t, err)
out := NewFeature("example")
err = msgpack.Unmarshal(data, out)
assert.NoError(t, err)
assert.EqualValues(t, in.Name(), out.Name())
assert.EqualValues(t, in.percentage, out.percentage)
assert.EqualValues(t, in.teamIDs, out.teamIDs)
assert.True(t, out.isTeamActive(1, false))
assert.True(t, out.isTeamActive(2, false))
assert.True(t, out.isTeamActive(3, false))
// encode just percentage
in = NewFeature("example")
in.percentage = 50
data, err = msgpack.Marshal(in)
assert.NoError(t, err)
out = NewFeature("example")
err = msgpack.Unmarshal(data, out)
assert.NoError(t, err)
assert.EqualValues(t, in.Name(), out.Name())
assert.EqualValues(t, in.percentage, out.percentage)
assert.EqualValues(t, in.teamIDs, out.teamIDs)
// encode just teams
in = NewFeature("example")
in.activateTeam(1)
in.activateTeam(2)
in.activateTeam(3)
data, err = msgpack.Marshal(in)
assert.NoError(t, err)
out = NewFeature("example")
err = msgpack.Unmarshal(data, out)
assert.NoError(t, err)
assert.EqualValues(t, in.Name(), out.Name())
assert.EqualValues(t, in.percentage, out.percentage)
assert.EqualValues(t, in.teamIDs, out.teamIDs)
assert.True(t, out.isTeamActive(1, false))
assert.True(t, out.isTeamActive(2, false))
assert.True(t, out.isTeamActive(3, false))
}
func TestEnableDisableTeam(t *testing.T) {
f := NewFeature("example")
assert.False(t, f.isTeamActive(1, false))
f.activateTeam(1)
f.activateTeam(2)
assert.True(t, f.isTeamActive(1, false))
assert.True(t, f.isTeamActive(2, false))
assert.False(t, f.isTeamActive(3, false))
f.deactivateTeam(1)
assert.False(t, f.isTeamActive(1, false))
assert.True(t, f.isTeamActive(2, false))
}
func TestEnableDisableFeature(t *testing.T) {
f := NewFeature("example")
assert.False(t, f.isTeamActive(1, false))
f.activate()
assert.True(t, f.isTeamActive(1, false))
assert.True(t, f.isTeamActive(999999999999, false))
f.deactivate()
assert.False(t, f.isTeamActive(1, false))
assert.False(t, f.isTeamActive(99999999999, false))
}
func TestRollout(t *testing.T) {
f := NewFeature("example")
// 75% < Team 1 < 100%
// 25% < Team 2 < 50%
// 0 % < Team 3 < 25%
assert.False(t, f.isTeamActive(1, true))
assert.False(t, f.isTeamActive(2, true))
assert.False(t, f.isTeamActive(3, true))
f.activatePercentage(25)
assert.False(t, f.isTeamActive(1, true))
assert.False(t, f.isTeamActive(2, true))
assert.True(t, f.isTeamActive(3, true))
f.activatePercentage(50)
assert.False(t, f.isTeamActive(1, true))
assert.True(t, f.isTeamActive(2, true))
assert.True(t, f.isTeamActive(3, true))
f.activatePercentage(75)
assert.False(t, f.isTeamActive(1, true))
assert.True(t, f.isTeamActive(2, true))
assert.True(t, f.isTeamActive(3, true))
f.activatePercentage(100)
assert.True(t, f.isTeamActive(1, true))
assert.True(t, f.isTeamActive(2, true))
assert.True(t, f.isTeamActive(3, true))
}
func TestRolloutactivateTeamMix(t *testing.T) {
f := NewFeature("example")
// 75% < Team 1 < 100%
// 25% < Team 2 < 50%
// 0 % < Team 3 < 25%
assert.False(t, f.isTeamActive(1, true))
assert.False(t, f.isTeamActive(2, true))
assert.False(t, f.isTeamActive(3, true))
f.activateTeam(1)
f.activatePercentage(25)
assert.True(t, f.isTeamActive(1, true))
assert.False(t, f.isTeamActive(2, true))
assert.True(t, f.isTeamActive(3, true))
}
func TestRandomizePercentage(t *testing.T) {
f := NewFeature("example")
f.activatePercentage(50)
// teamID == 10 will be active at 50% when randomized, but not when static
teamID := int64(10)
assert.True(t, f.isTeamActive(teamID, true))
assert.False(t, f.isTeamActive(teamID, false))
}