-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy patherror_boundary_test.go
142 lines (136 loc) · 4.04 KB
/
error_boundary_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
package statsig
import (
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
)
func mock_server(t *testing.T, expectedError error, hit *bool, statsigOptions *map[string]interface{}) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if strings.Contains(req.URL.Path, "/download_config_specs") {
res.WriteHeader(500)
return
}
if strings.Contains(req.URL.Path, "/sdk_exception") {
var body *logExceptionRequestBody
_ = json.NewDecoder(req.Body).Decode(&body)
for key, value := range body.StatsigOptions {
(*statsigOptions)[key] = value
}
if body.Exception != "" && (expectedError == nil || body.Exception == expectedError.Error()) {
*hit = true
success := &logExceptionResponse{Success: true}
json, _ := json.Marshal(success)
_, _ = res.Write(json)
} else {
t.Error("Failed to log exception")
}
return
}
}))
}
func TestLogException(t *testing.T) {
err := errors.New("test error boundary log exception")
hit := false
statsigOption := make(map[string]interface{})
testServer := mock_server(t, err, &hit, &statsigOption)
defer testServer.Close()
opt := &Options{
API: testServer.URL,
}
diagnostics := newDiagnostics(opt)
errorBoundary := newErrorBoundary("client-key", opt, diagnostics)
errorBoundary.logException(err)
if !hit {
t.Error("Expected sdk_exception endpoint to be hit")
}
if statsigOption["API"] != testServer.URL {
t.Error("Expected statsigOptions to be set")
}
}
func TestDCSError(t *testing.T) {
hit := false
statsigOption := make(map[string]interface{})
testServer := mock_server(t, nil, &hit, &statsigOption)
defer testServer.Close()
dataadapter := dataAdapterExample{}
opt := &Options{
API: testServer.URL,
OutputLoggerOptions: getOutputLoggerOptionsForTest(t),
StatsigLoggerOptions: getStatsigLoggerOptionsForTest(t),
DataAdapter: &dataadapter,
}
InitializeWithOptions("secret-key", opt)
defer ShutdownAndDangerouslyClearInstance()
if !hit {
t.Error("Expected sdk_exception endpoint to be hit")
}
expectedStatsigLoggerOptions := map[string]interface{}{
"DisableInitDiagnostics": true,
"DisableSyncDiagnostics": true,
"DisableApiDiagnostics": true,
"DisableAllLogging": false,
}
if !reflect.DeepEqual(statsigOption["StatsigLoggerOptions"], expectedStatsigLoggerOptions) {
t.Error("Expected StatsigLoggerOptions in statsigOptions to be set")
}
}
func TestRepeatedError(t *testing.T) {
err := errors.New("common error")
hit := false
statsigOption := make(map[string]interface{})
testServer := mock_server(t, err, &hit, &statsigOption)
defer testServer.Close()
opt := &Options{
API: testServer.URL,
}
diagnostics := newDiagnostics(opt)
errorBoundary := newErrorBoundary("client-key", opt, diagnostics)
errorBoundary.logException(err)
if !hit {
t.Error("Expected sdk_exception endpoint to be hit")
}
hit = false
errorBoundary.logException(err)
if hit {
t.Error("Expected sdk_exception endpoint to NOT be hit")
}
}
func TestStatsigOptions(t *testing.T) {
hit := false
statsigOption := make(map[string]interface{})
testServer := mock_server(t, nil, &hit, &statsigOption)
defer testServer.Close()
opt := &Options{
API: testServer.URL,
APIOverrides: APIOverrides{
DownloadConfigSpecs: testServer.URL,
},
DataAdapter: &dataAdapterExample{},
IDListSyncInterval: 1,
IPCountryOptions: IPCountryOptions{
Disabled: true,
LazyLoad: true,
},
}
diagnostics := newDiagnostics(opt)
errorBoundary := newErrorBoundary("client-key", opt, diagnostics)
errorBoundary.logException(errors.New("test error"))
expectedOptions := map[string]interface{}{
"API": testServer.URL,
"APIOverrides": map[string]interface{}{
"DownloadConfigSpecs": testServer.URL,
"GetIDLists": "",
"LogEvent": "",
},
"DataAdapter": "set",
"IDListSyncInterval": 1.0,
"IPCountryOptions": "set",
}
if !reflect.DeepEqual(statsigOption, expectedOptions) {
t.Error("Expected statsigOptions to be set")
}
}