-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathclient_initialize_response_test.go
300 lines (275 loc) · 10.5 KB
/
client_initialize_response_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
package statsig
import (
"bytes"
"encoding/json"
"net/http"
"os"
"strconv"
"testing"
)
func TestInitializeResponseConsistency(t *testing.T) {
if os.Getenv("CI") != "" {
t.Skip("Disabled until optimization are complete")
}
user := User{
UserID: "123",
Email: "[email protected]",
Country: "US",
Custom: map[string]interface{}{"test": "123"},
CustomIDs: map[string]string{"stableID": "12345"},
}
for _, api := range testAPIs {
t.Run("Validate consistency for "+api, func(t *testing.T) {
endpoint := api + "/initialize"
input := map[string]interface{}{
"user": user,
"statsigMetadata": map[string]string{
"sdkType": "consistency-test",
"sessionID": "x123",
},
}
body, _ := json.Marshal(input)
req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(body))
if err != nil {
t.Errorf("Failed to make a request to %s", endpoint)
}
clientKey := os.Getenv("test_client_key")
req.Header.Add("STATSIG-API-KEY", clientKey)
req.Header.Set("Content-Type", "application/json")
req.Header.Add("STATSIG-CLIENT-TIME", strconv.FormatInt(getUnixMilli(), 10))
req.Header.Add("STATSIG-SDK-TYPE", getStatsigMetadata().SDKType)
req.Header.Add("STATSIG-SDK-VERSION", getStatsigMetadata().SDKVersion)
req.Header.Set("User-Agent", "")
client := http.Client{}
response, err := client.Do(req)
if err != nil {
t.Errorf("Failed to get a valid response from %s", endpoint)
}
defer response.Body.Close()
if response.StatusCode < 200 || response.StatusCode >= 300 {
t.Errorf("Request to %s failed with status %d", endpoint, response.StatusCode)
}
actualResponseBody, err := filterHttpResponseAndReadBody(response)
if err != nil {
t.Errorf("Error reading %s response body", endpoint)
}
InitializeWithOptions(secret, &Options{
API: api,
OutputLoggerOptions: getOutputLoggerOptionsForTest(t),
StatsigLoggerOptions: getStatsigLoggerOptionsForTest(t),
})
defer ShutdownAndDangerouslyClearInstance()
formattedResponse := GetClientInitializeResponse(user)
filterClientInitializeResponse(&formattedResponse)
formattedResponseJson, _ := json.Marshal(formattedResponse)
formattedResponseWithOptions := GetClientInitializeResponseWithOptions(user, &GCIROptions{})
filterClientInitializeResponse(&formattedResponseWithOptions)
formattedResponseWithOptionsJson, _ := json.Marshal(formattedResponseWithOptions)
if string(actualResponseBody) != string(formattedResponseJson) {
t.Errorf("Inconsistent response from GetClientInitializeResponse vs %s", endpoint)
}
if string(actualResponseBody) != string(formattedResponseWithOptionsJson) {
t.Errorf("Inconsistent response from GetClientInitializeResponseWithOptions vs %s", endpoint)
}
})
}
}
func TestClientInitializeResponseOptions(t *testing.T) {
user := User{
UserID: "123",
Email: "[email protected]",
Country: "US",
Custom: map[string]interface{}{"test": "123"},
CustomIDs: map[string]string{"stableID": "12345"},
}
InitializeWithOptions(secret, &Options{
OutputLoggerOptions: getOutputLoggerOptionsForTest(t),
StatsigLoggerOptions: getStatsigLoggerOptionsForTest(t),
})
defer ShutdownAndDangerouslyClearInstance()
response := GetClientInitializeResponseWithOptions(user, &GCIROptions{
IncludeConfigType: true,
HashAlgorithm: "none",
})
var featureGate GateInitializeResponse
var dynamicConfig ConfigInitializeResponse
var experiment ConfigInitializeResponse
var autotune ConfigInitializeResponse
var layer LayerInitializeResponse
for _, g := range response.FeatureGates {
if g.Name == "test_public" {
featureGate = g
}
}
for _, c := range response.DynamicConfigs {
if c.Name == "test_custom_config" {
dynamicConfig = c
} else if c.Name == "test_experiment_with_targeting" {
experiment = c
} else if c.Name == "test_autotune" {
autotune = c
}
}
for _, l := range response.LayerConfigs {
if l.Name == "Basic_test_layer" {
layer = l
}
}
if featureGate.ConfigType != FeatureGateType {
t.Errorf("Expected GCIR config type of test_public to be %s, received %s", FeatureGateType, featureGate.ConfigType)
}
if dynamicConfig.ConfigType != DynamicConfigType {
t.Errorf("Expected GCIR config type of test_custom_config to be %s, received %s", DynamicConfigType, dynamicConfig.ConfigType)
}
if experiment.ConfigType != ExperimentType {
t.Errorf("Expected GCIR config type of test_experiment_with_targeting to be %s, received %s", ExperimentType, experiment.ConfigType)
}
if autotune.ConfigType != AutotuneType {
t.Errorf("Expected GCIR config type of test_custom_config to be %s, received %s", AutotuneType, autotune.ConfigType)
}
if layer.ConfigType != LayerType {
t.Errorf("Expected GCIR config type of Basic_test_layer to be %s, received %s", LayerType, layer.ConfigType)
}
}
func TestClientInitializeResponseFilterOptions(t *testing.T) {
user := User{
UserID: "123",
Email: "[email protected]",
Country: "US",
Custom: map[string]interface{}{"test": "123"},
CustomIDs: map[string]string{"stableID": "12345"},
}
InitializeWithOptions(secret, &Options{
OutputLoggerOptions: getOutputLoggerOptionsForTest(t),
StatsigLoggerOptions: getStatsigLoggerOptionsForTest(t),
})
defer ShutdownAndDangerouslyClearInstance()
tests := []struct {
name string
filter *GCIROptions
expectGate bool
expectDC bool
expectExp bool
expectAT bool
expectLayer bool
}{
{
name: "Include all entities",
filter: &GCIROptions{HashAlgorithm: "none"},
expectGate: true, expectDC: true, expectExp: true, expectAT: true, expectLayer: true,
},
{
name: "Only feature gates",
filter: &GCIROptions{HashAlgorithm: "none", ConfigTypesToInclude: []ConfigType{FeatureGateType}},
expectGate: true, expectDC: false, expectExp: false, expectAT: false, expectLayer: false,
},
{
name: "Only dynamic configs",
filter: &GCIROptions{HashAlgorithm: "none", ConfigTypesToInclude: []ConfigType{DynamicConfigType}},
expectGate: false, expectDC: true, expectExp: false, expectAT: false, expectLayer: false,
},
{
name: "Only experiments",
filter: &GCIROptions{HashAlgorithm: "none", ConfigTypesToInclude: []ConfigType{ExperimentType}},
expectGate: false, expectDC: false, expectExp: true, expectAT: false, expectLayer: false,
},
{
name: "Only autotune configs",
filter: &GCIROptions{HashAlgorithm: "none", ConfigTypesToInclude: []ConfigType{AutotuneType}},
expectGate: false, expectDC: false, expectExp: false, expectAT: true, expectLayer: false,
},
{
name: "Only layers",
filter: &GCIROptions{HashAlgorithm: "none", ConfigTypesToInclude: []ConfigType{LayerType}},
expectGate: false, expectDC: false, expectExp: false, expectAT: false, expectLayer: true,
},
{
name: "Include multiple entities (feature gates and experiments)",
filter: &GCIROptions{HashAlgorithm: "none", ConfigTypesToInclude: []ConfigType{FeatureGateType, ExperimentType}},
expectGate: true, expectDC: false, expectExp: true, expectAT: false, expectLayer: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
response := GetClientInitializeResponseWithOptions(user, tt.filter)
var featureGate GateInitializeResponse
var dynamicConfig ConfigInitializeResponse
var experiment ConfigInitializeResponse
var autotune ConfigInitializeResponse
var layer LayerInitializeResponse
for _, g := range response.FeatureGates {
if g.Name == "test_public" {
featureGate = g
}
}
for _, c := range response.DynamicConfigs {
if c.Name == "test_custom_config" {
dynamicConfig = c
} else if c.Name == "test_experiment_with_targeting" {
experiment = c
} else if c.Name == "test_autotune" {
autotune = c
}
}
for _, l := range response.LayerConfigs {
if l.Name == "Basic_test_layer" {
layer = l
}
}
if (featureGate.Name != "") != tt.expectGate {
t.Errorf("Feature gate presence mismatch: got %v, want %v", featureGate.Name != "", tt.expectGate)
}
if (dynamicConfig.Name != "") != tt.expectDC {
t.Errorf("Dynamic config presence mismatch: got %v, want %v", dynamicConfig.Name != "", tt.expectDC)
}
if (experiment.Name != "") != tt.expectExp {
t.Errorf("Experiment presence mismatch: got %v, want %v", experiment.Name != "", tt.expectExp)
}
if (autotune.Name != "") != tt.expectAT {
t.Errorf("Autotune config presence mismatch: got %v, want %v", autotune.Name != "", tt.expectAT)
}
if (layer.Name != "") != tt.expectLayer {
t.Errorf("Layer presence mismatch: got %v, want %v", layer.Name != "", tt.expectLayer)
}
})
}
}
func filterHttpResponseAndReadBody(httpResponse *http.Response) ([]byte, error) {
var interfaceBody ClientInitializeResponse
// Initialize nullable fields so that JSON Unmarshal doesn't convert to null
interfaceBody.FeatureGates = make(map[string]GateInitializeResponse)
interfaceBody.DynamicConfigs = make(map[string]ConfigInitializeResponse)
interfaceBody.LayerConfigs = make(map[string]LayerInitializeResponse)
interfaceBody.SdkParams = make(map[string]string)
interfaceBody.EvaluatedKeys = make(map[string]interface{})
err := json.NewDecoder(httpResponse.Body).Decode(&interfaceBody)
if err != nil {
return make([]byte, 0), err
}
filterClientInitializeResponse(&interfaceBody)
return json.Marshal(interfaceBody)
}
func filterClientInitializeResponse(clientInitializeResponse *ClientInitializeResponse) {
for i := range clientInitializeResponse.FeatureGates {
for j := range clientInitializeResponse.FeatureGates[i].SecondaryExposures {
clientInitializeResponse.FeatureGates[i].SecondaryExposures[j].Gate = "__REMOVED_FOR_TEST__"
}
}
for i := range clientInitializeResponse.DynamicConfigs {
for j := range clientInitializeResponse.DynamicConfigs[i].SecondaryExposures {
clientInitializeResponse.DynamicConfigs[i].SecondaryExposures[j].Gate = "__REMOVED_FOR_TEST__"
}
}
for i := range clientInitializeResponse.LayerConfigs {
for j := range clientInitializeResponse.LayerConfigs[i].SecondaryExposures {
clientInitializeResponse.LayerConfigs[i].SecondaryExposures[j].Gate = "__REMOVED_FOR_TEST__"
}
for j := range clientInitializeResponse.LayerConfigs[i].UndelegatedSecondaryExposures {
clientInitializeResponse.LayerConfigs[i].UndelegatedSecondaryExposures[j].Gate = "__REMOVED_FOR_TEST__"
}
}
clientInitializeResponse.Generator = "__REMOVED_FOR_TEST__"
clientInitializeResponse.Time = 0
clientInitializeResponse.SDKInfo = SDKInfo{}
clientInitializeResponse.User = User{}
}