-
Notifications
You must be signed in to change notification settings - Fork 10
/
user_persistent_storage_test.go
221 lines (197 loc) · 9.47 KB
/
user_persistent_storage_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
package statsig
import (
"fmt"
"os"
"testing"
)
func TestUserPersistentStorage(t *testing.T) {
persistentStorage := &userPersistentStorageExample{store: make(map[string]UserPersistedValues)}
bytes, _ := os.ReadFile("download_config_specs_sticky_experiments.json")
opts := &Options{
OutputLoggerOptions: getOutputLoggerOptionsForTest(t),
StatsigLoggerOptions: getStatsigLoggerOptionsForTest(t),
BootstrapValues: string(bytes),
UserPersistentStorage: persistentStorage,
}
InitializeWithOptions("secret-key", opts)
userInControl := User{UserID: "vj"}
userInTest := User{UserID: "hunter2"}
userNotInExp := User{UserID: "gb"}
expName := "the_allocated_experiment"
// Control group
exp := GetExperiment(userInControl, expName)
if exp.GroupName != "Control" {
t.Errorf("Expected: Control. Received: %s", exp.GroupName)
}
if exp.EvaluationDetails.Source != SourceBootstrap {
t.Errorf("Expected: %s. Received: %s", SourceBootstrap, exp.EvaluationDetails.Source)
}
// Test group
exp = GetExperiment(userInTest, expName)
if exp.GroupName != "Test" {
t.Errorf("Expected: Test. Received: %s", exp.GroupName)
}
if exp.EvaluationDetails.Source != SourceBootstrap {
t.Errorf("Expected: %s. Received: %s", SourceBootstrap, exp.EvaluationDetails.Source)
}
// Not allocated to the experiment
exp = GetExperiment(userNotInExp, expName)
if exp.RuleID != "layerAssignment" {
t.Errorf("Expected: layerAssignment. Received: %s", exp.RuleID)
}
// At this point, we have not opted in to sticky
if len(persistentStorage.store) != 0 {
t.Errorf("Expected persistent storage to be empty")
}
if persistentStorage.saveCalled != 0 {
t.Errorf("Expected persistent storage Save to have not been called")
}
// Control group with persistent storage enabled
// (should save to storage, but evaluate as normal until next call)
persistedValues := GetUserPersistedValues(userInControl, "userID")
exp = GetExperimentWithOptions(userInControl, expName, &GetExperimentOptions{PersistedValues: persistedValues})
if exp.GroupName != "Control" {
t.Errorf("Expected: Control. Received: %s", exp.GroupName)
}
if exp.EvaluationDetails.Source != SourceBootstrap {
t.Errorf("Expected: %s. Received: %s", SourceBootstrap, exp.EvaluationDetails.Source)
}
// Test group with persistent storage enabled
// (should save to storage, but evaluate as normal until next call)
persistedValues = GetUserPersistedValues(userInTest, "userID")
exp = GetExperimentWithOptions(userInTest, expName, &GetExperimentOptions{PersistedValues: persistedValues})
if exp.GroupName != "Test" {
t.Errorf("Expected: Test. Received: %s", exp.GroupName)
}
if exp.EvaluationDetails.Source != SourceBootstrap {
t.Errorf("Expected: %s. Received: %s", SourceBootstrap, exp.EvaluationDetails.Source)
}
// Verify that persistent storage has been updated
if len(persistentStorage.store) != 2 {
t.Errorf("Expected persistent storage to have size 2, Received: %d", len(persistentStorage.store))
}
if persistentStorage.saveCalled != 2 {
t.Errorf("Expected persistent storage Save to have been called 2 times, Received: %d", persistentStorage.saveCalled)
}
// Use sticky bucketing with valid persisted values
// (Should override userInControl to the first evaluation of userInControl)
persistedValues = GetUserPersistedValues(userInControl, "userID")
exp = GetExperimentWithOptions(userInControl, expName, &GetExperimentOptions{PersistedValues: persistedValues})
if exp.GroupName != "Control" {
t.Errorf("Expected: Control. Received: %s", exp.GroupName)
}
if exp.EvaluationDetails.Reason != ReasonPersisted {
t.Errorf("Expected: %s. Received: %s", ReasonPersisted, exp.EvaluationDetails.Reason)
}
persistedValues = GetUserPersistedValues(userInTest, "userID")
exp = GetExperimentWithOptions(userInTest, expName, &GetExperimentOptions{PersistedValues: persistedValues})
if exp.GroupName != "Test" {
t.Errorf("Expected: Test. Received: %s", exp.GroupName)
}
if exp.EvaluationDetails.Reason != ReasonPersisted {
t.Errorf("Expected: %s. Received: %s", ReasonPersisted, exp.EvaluationDetails.Reason)
}
// Use sticky bucketing with valid persisted values to assign a user that would otherwise be unallocated
// (Should override userNotInExp to the first evaluation of userInControl)
persistedValues = GetUserPersistedValues(userInControl, "userID")
exp = GetExperimentWithOptions(userNotInExp, expName, &GetExperimentOptions{PersistedValues: persistedValues})
if exp.GroupName != "Control" {
t.Errorf("Expected: Control. Received: %s", exp.GroupName)
}
if exp.EvaluationDetails.Reason != ReasonPersisted {
t.Errorf("Expected: %s. Received: %s", ReasonPersisted, exp.EvaluationDetails.Reason)
}
// Use sticky bucketing with valid persisted values for an unallocated user
// (Should not override since there are no persisted values)
persistedValues = GetUserPersistedValues(userNotInExp, "userID")
exp = GetExperimentWithOptions(userNotInExp, expName, &GetExperimentOptions{PersistedValues: persistedValues})
if exp.RuleID != "layerAssignment" {
t.Errorf("Expected: layerAssignment. Received: %s", exp.RuleID)
}
if exp.EvaluationDetails.Source != SourceBootstrap {
t.Errorf("Expected: %s. Received: %s", SourceBootstrap, exp.EvaluationDetails.Source)
}
// Use sticky bucketing on a different ID type that hasn't been saved to storage
// (Should not override since there are no persisted values)
persistedValues = GetUserPersistedValues(userInTest, "stableID")
exp = GetExperimentWithOptions(userInTest, expName, &GetExperimentOptions{PersistedValues: persistedValues})
if exp.GroupName != "Test" {
t.Errorf("Expected: Test. Received: %s", exp.GroupName)
}
if exp.EvaluationDetails.Source != SourceBootstrap {
t.Errorf("Expected: %s. Received: %s", SourceBootstrap, exp.EvaluationDetails.Source)
}
// Verify that persistent storage has been updated
if len(persistentStorage.store) != 2 {
t.Errorf("Expected persistent storage to have size 2, Received: %d", len(persistentStorage.store))
}
if persistentStorage.saveCalled != 3 {
t.Errorf("Expected persistent storage Save to have been called 3 times, Received: %d", persistentStorage.saveCalled)
}
// Verify that persisted values are deleted once the experiment is no longer active
bytes, _ = os.ReadFile("download_config_specs_sticky_experiments_inactive.json")
opts.BootstrapValues = string(bytes)
ShutdownAndDangerouslyClearInstance()
InitializeWithOptions("secret-key", opts)
stillActiveExpName := "another_allocated_experiment_still_active"
persistedValues = GetUserPersistedValues(userInControl, "userID")
exp = GetExperimentWithOptions(userInControl, stillActiveExpName, &GetExperimentOptions{PersistedValues: persistedValues})
if exp.GroupName != "Control" {
t.Errorf("Expected: Control. Received: %s", exp.GroupName)
}
if exp.EvaluationDetails.Source != SourceBootstrap {
t.Errorf("Expected: %s. Received: %s", SourceBootstrap, exp.EvaluationDetails.Source)
}
userInControlPersistedValues := persistentStorage.store[fmt.Sprintf("%s:userID", userInControl.UserID)]
if _, ok := userInControlPersistedValues[stillActiveExpName]; !ok {
t.Errorf("Expected %s to exist in user persisted storage for user %s", stillActiveExpName, userInControl.UserID)
}
persistedValues = GetUserPersistedValues(userInControl, "userID")
exp = GetExperimentWithOptions(userInControl, expName, &GetExperimentOptions{PersistedValues: persistedValues})
if exp.GroupName != "Control" {
t.Errorf("Expected: Control. Received: %s", exp.GroupName)
}
if exp.EvaluationDetails.Source != SourceBootstrap {
t.Errorf("Expected: %s. Received: %s", SourceBootstrap, exp.EvaluationDetails.Source)
}
userInControlPersistedValues = persistentStorage.store[fmt.Sprintf("%s:userID", userInControl.UserID)]
if _, ok := userInControlPersistedValues[expName]; ok {
t.Errorf("Expected %s to not exist in user persisted storage for user %s", expName, userInControl.UserID)
}
// Verify that persisted values are deleted once an experiment is evaluated without persisted values (opted-out)
exp = GetExperiment(userInTest, expName)
if exp.GroupName != "Test" {
t.Errorf("Expected: Test. Received: %s", exp.GroupName)
}
if exp.EvaluationDetails.Source != SourceBootstrap {
t.Errorf("Expected: %s. Received: %s", SourceBootstrap, exp.EvaluationDetails.Source)
}
userInTestPersistedValues := persistentStorage.store[fmt.Sprintf("%s:userID", userInTest.UserID)]
if _, ok := userInTestPersistedValues[expName]; ok {
t.Errorf("Expected %s to not exist in user persisted storage for user %s", expName, userInTest.UserID)
}
}
func TestInvalidUserPersistentStorage(t *testing.T) {
persistentStorage := &brokenUserPersistentStorageExample{}
bytes, _ := os.ReadFile("download_config_specs_sticky_experiments.json")
opts := &Options{
OutputLoggerOptions: getOutputLoggerOptionsForTest(t),
StatsigLoggerOptions: getStatsigLoggerOptionsForTest(t),
BootstrapValues: string(bytes),
UserPersistentStorage: persistentStorage,
}
InitializeWithOptions("secret-key", opts)
userInControl := User{UserID: "vj"}
expName := "the_allocated_experiment"
persistedValues := GetUserPersistedValues(userInControl, "userID")
exp := GetExperimentWithOptions(userInControl, expName, &GetExperimentOptions{PersistedValues: persistedValues})
if exp.GroupName != "Control" {
t.Errorf("Expected: Control. Received: %s", exp.GroupName)
}
if exp.EvaluationDetails.Source != SourceBootstrap {
t.Errorf("Expected: %s. Received: %s", SourceBootstrap, exp.EvaluationDetails.Source)
}
if err := recover(); err != nil {
t.Errorf("Expected not to panic")
}
}