forked from statsig-io/go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatsig_test.go
More file actions
185 lines (165 loc) · 6.08 KB
/
statsig_test.go
File metadata and controls
185 lines (165 loc) · 6.08 KB
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
package statsig
import (
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)
func TestBootstrap(t *testing.T) {
if IsInitialized() {
t.Errorf("expected statsig to not be initialized yet")
}
bytes, _ := os.ReadFile("download_config_specs.json")
InitializeWithOptions("secret-key", &Options{
OutputLoggerOptions: getOutputLoggerOptionsForTest(t),
StatsigLoggerOptions: getStatsigLoggerOptionsForTest(t),
})
if !IsInitialized() {
t.Errorf("expected statsig to be initialized")
}
if CheckGate(User{UserID: "123"}, "always_on_gate") {
t.Errorf("always_on_gate should return false when there is no bootstrap value")
}
ShutdownAndDangerouslyClearInstance()
opt := &Options{
BootstrapValues: string(bytes[:]),
OutputLoggerOptions: getOutputLoggerOptionsForTest(t),
StatsigLoggerOptions: getStatsigLoggerOptionsForTest(t),
}
InitializeWithOptions("secret-key", opt)
if !CheckGate(User{UserID: "123"}, "always_on_gate") {
t.Errorf("always_on_gate should return true bootstrap value is provided")
}
ShutdownAndDangerouslyClearInstance()
}
func TestRulesUpdatedCallback(t *testing.T) {
// First, verify that rules updated callback is called and returns the rules string
bytes, _ := os.ReadFile("download_config_specs.json")
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(http.StatusOK)
if strings.Contains(req.URL.Path, "download_config_specs") {
_, _ = res.Write(bytes)
}
}))
callbacked := false
rules := ""
opt := &Options{
API: testServer.URL,
RulesUpdatedCallback: func(rulesString string, time int64) {
rules = rulesString
if time == 1631638014811 {
callbacked = true
}
},
OutputLoggerOptions: getOutputLoggerOptionsForTest(t),
StatsigLoggerOptions: getStatsigLoggerOptionsForTest(t),
}
InitializeWithOptions("secret-key", opt)
if !callbacked {
t.Errorf("rules updated callback did not happen")
}
if !CheckGate(User{UserID: "136"}, "fractional_gate") {
t.Errorf("fractional_gate should return true for the given user")
}
ShutdownAndDangerouslyClearInstance()
// Now use rules from the previous update callback to bootstrap, and validate values
opt_bootstrap := &Options{
BootstrapValues: rules,
LocalMode: true,
OutputLoggerOptions: getOutputLoggerOptionsForTest(t),
StatsigLoggerOptions: getStatsigLoggerOptionsForTest(t),
}
InitializeWithOptions("secret-key", opt_bootstrap)
if !CheckGate(User{UserID: "123"}, "always_on_gate") {
t.Errorf("always_on_gate should return true bootstrap value is provided")
}
ShutdownAndDangerouslyClearInstance()
}
func TestLogImmediate(t *testing.T) {
env := ""
testServer := getTestServer(testServerOptions{
onLogEvent: func(newEvents []map[string]interface{}) {
eventTyped := convertToExposureEvent(newEvents[0])
env = eventTyped.User.StatsigEnvironment["tier"]
},
})
defer testServer.Close()
opt := &Options{
API: testServer.URL,
Environment: Environment{Tier: "test"},
OutputLoggerOptions: getOutputLoggerOptionsForTest(t),
StatsigLoggerOptions: getStatsigLoggerOptionsForTest(t),
}
InitializeWithOptions("secret-key", opt)
event := Event{EventName: "test_event", User: User{UserID: "123"}}
response, err := LogImmediate([]Event{event})
if response.StatusCode != http.StatusOK {
t.Errorf("Status should be OK")
}
if err != nil {
t.Errorf("Error should be nil")
}
if env != "test" {
t.Errorf("Environment not set on user")
}
ShutdownAndDangerouslyClearInstance()
}
func TestGetExperimentByGroupName(t *testing.T) {
testServer := getTestServer(testServerOptions{
})
defer testServer.Close()
opt := &Options{
API: testServer.URL,
OutputLoggerOptions: getOutputLoggerOptionsForTest(t),
StatsigLoggerOptions: getStatsigLoggerOptionsForTest(t),
}
InitializeWithOptions("secret-key", opt)
experiment := GetExperimentByGroupName("sample_experiment", "Control")
if experiment.GroupName != "Control" {
t.Errorf("Expected group name Control. Received: %s", experiment.GroupName)
}
if experiment.Value["experiment_param"] != "control" {
t.Errorf("Expected experiment param control. Received: %s", experiment.Value["experiment_param"])
}
naExperiment := GetExperimentByGroupName("sample_experiment", "Not a group")
if naExperiment.Name != "sample_experiment" {
t.Errorf("Expected experiment name sample_experiment. Received: %s", naExperiment.Name)
}
if naExperiment.Value["experiment_param"] != nil {
t.Errorf("Expected experiment param to be nil. Received: %s", naExperiment.Value["experiment_param"])
}
if naExperiment.EvaluationDetails.Reason != ReasonUnrecognized {
t.Errorf("Expected evaluation details reason Unrecognized. Received: %s", naExperiment.EvaluationDetails.Reason)
}
ShutdownAndDangerouslyClearInstance()
}
func TestGetExperimentByGroupIDAdvanced(t *testing.T) {
testServer := getTestServer(testServerOptions{
})
defer testServer.Close()
opt := &Options{
API: testServer.URL,
OutputLoggerOptions: getOutputLoggerOptionsForTest(t),
StatsigLoggerOptions: getStatsigLoggerOptionsForTest(t),
}
InitializeWithOptions("secret-key", opt)
experiment := GetExperimentByGroupIDAdvanced("sample_experiment", "2RamGsERWbWMIMnSfOlQuX")
if experiment.GroupName != "Control" {
t.Errorf("Expected group name Control. Received: %s", experiment.GroupName)
}
if experiment.Value["experiment_param"] != "control" {
t.Errorf("Expected experiment param control. Received: %s", experiment.Value["experiment_param"])
}
naExperiment := GetExperimentByGroupIDAdvanced("sample_experiment", "Not a group")
if naExperiment.Name != "sample_experiment" {
t.Errorf("Expected experiment name sample_experiment. Received: %s", naExperiment.Name)
}
if naExperiment.Value["experiment_param"] != nil {
t.Errorf("Expected experiment param to be nil. Received: %s", naExperiment.Value["experiment_param"])
}
if naExperiment.EvaluationDetails.Reason != ReasonUnrecognized {
t.Errorf("Expected evaluation details reason Unrecognized. Received: %s", naExperiment.EvaluationDetails.Reason)
}
ShutdownAndDangerouslyClearInstance()
}