forked from statsig-io/go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_boundary_test.go
85 lines (80 loc) · 2.19 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
package statsig
import (
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func mock_server(t *testing.T, expectedError error, hit *bool) *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)
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
testServer := mock_server(t, err, &hit)
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")
}
}
func TestDCSError(t *testing.T) {
hit := false
testServer := mock_server(t, nil, &hit)
defer testServer.Close()
opt := &Options{
API: testServer.URL,
OutputLoggerOptions: getOutputLoggerOptionsForTest(t),
StatsigLoggerOptions: getStatsigLoggerOptionsForTest(t),
}
InitializeWithOptions("secret-key", opt)
defer ShutdownAndDangerouslyClearInstance()
if !hit {
t.Error("Expected sdk_exception endpoint to be hit")
}
}
func TestRepeatedError(t *testing.T) {
err := errors.New("common error")
hit := false
testServer := mock_server(t, err, &hit)
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")
}
}