-
Notifications
You must be signed in to change notification settings - Fork 10
/
statsig_test.go
121 lines (107 loc) · 3.5 KB
/
statsig_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
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()
}