forked from statsig-io/go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_logger_test.go
69 lines (59 loc) · 1.55 KB
/
output_logger_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
package statsig
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
)
func TestLogEventErrors(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(http.StatusBadRequest)
}))
defer testServer.Close()
errs := make([]error, 0)
opts := &Options{
API: testServer.URL,
StatsigLoggerOptions: StatsigLoggerOptions{
DisableInitDiagnostics: true,
DisableSyncDiagnostics: true,
DisableApiDiagnostics: true,
},
OutputLoggerOptions: OutputLoggerOptions{
EnableDebug: true,
LogCallback: func(message string, err error) {
errs = append(errs, err)
},
},
}
diagnostics := newDiagnostics(opts)
transport := newTransport("secret", opts)
errorBoundary := newErrorBoundary("secret", opts, nil)
logger := newLogger(transport, opts, diagnostics, errorBoundary)
user := User{
UserID: "123",
}
event := Event{
EventName: "test_event",
User: user,
Value: "3",
}
stderrLogs := swallow_stderr(func() {
logger.logCustom(event)
logger.flush(true)
})
if stderrLogs == "" {
t.Errorf("Expected output to stderr")
}
InitializeGlobalOutputLogger(opts.OutputLoggerOptions)
logger.logCustom(event)
logger.flush(true)
if len(errs) == 0 {
t.Errorf("Expected output to callback")
}
if !errors.Is(errs[0], ErrFailedLogEvent) {
t.Errorf("Expected error to match ErrFailedLogEvent")
}
if errs[0].Error() != "Failed to log 1 events: Failed request to /log_event after 0 retries: 400 Bad Request" {
t.Errorf("Expected error message")
}
}