-
Notifications
You must be signed in to change notification settings - Fork 10
/
init_timeout_test.go
88 lines (82 loc) · 2.57 KB
/
init_timeout_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
package statsig
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func TestInitTimeout(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if strings.Contains(req.URL.Path, "download_config_specs") {
time.Sleep(100 * time.Millisecond)
}
res.WriteHeader(http.StatusOK)
}))
defer testServer.Close()
user := User{UserID: "some_user_id"}
initTimeBuffer := 2 * time.Millisecond // expected runtime buffer for initialize with timeout
t.Run("No timeout option", func(t *testing.T) {
options := &Options{
API: testServer.URL,
OutputLoggerOptions: getOutputLoggerOptionsForTest(t),
StatsigLoggerOptions: getStatsigLoggerOptionsForTest(t),
}
start := time.Now()
InitializeWithOptions("secret-key", options)
elapsed := time.Since(start)
if elapsed < (100 * time.Millisecond) {
t.Errorf("Expected initalize to take at least 1 second")
}
defer func() {
if err := recover(); err != nil {
t.Errorf("Expected initialize to succeed")
}
}()
CheckGate(user, "nonexistent-gate")
ShutdownAndDangerouslyClearInstance()
})
t.Run("Initalize finish before timeout", func(t *testing.T) {
options := &Options{
API: testServer.URL,
InitTimeout: 5 * time.Second,
OutputLoggerOptions: getOutputLoggerOptionsForTest(t),
StatsigLoggerOptions: getStatsigLoggerOptionsForTest(t),
}
start := time.Now()
InitializeWithOptions("secret-key", options)
elapsed := time.Since(start)
if elapsed < (100 * time.Millisecond) {
t.Errorf("Expected initalize to take at least 1 second")
}
if elapsed > (options.InitTimeout + initTimeBuffer) {
t.Errorf("Initalize exceeded timeout")
}
defer func() {
if err := recover(); err != nil {
t.Errorf("Expected initialize to succeed")
}
}()
CheckGate(user, "nonexistent-gate")
ShutdownAndDangerouslyClearInstance()
})
t.Run("Initialize timed out", func(t *testing.T) {
options := &Options{
API: testServer.URL,
InitTimeout: 100 * time.Millisecond,
OutputLoggerOptions: getOutputLoggerOptionsForTest(t),
StatsigLoggerOptions: getStatsigLoggerOptionsForTest(t),
}
start := time.Now()
InitializeWithOptions("secret-key", options)
elapsed := time.Since(start)
if elapsed > (options.InitTimeout + initTimeBuffer) {
t.Errorf("Initalize exceeded timeout %s", elapsed)
}
gate := CheckGate(user, "always_on_gate")
if gate != false {
t.Errorf("Expected gate to be default off")
}
ShutdownAndDangerouslyClearInstance()
})
}