-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhandler_test.go
196 lines (176 loc) · 4.26 KB
/
handler_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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package splunk
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"strings"
"testing"
"testing/slogtest"
)
func TestSplunkHandler(t *testing.T) {
emptyLines := 0
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
buf := new(bytes.Buffer)
buf.ReadFrom(r.Body)
lines := strings.Split(buf.String(), "\n")
for _, line := range lines {
if line == "" {
emptyLines++
continue
}
if !json.Valid([]byte(line)) {
t.Fatalf("invalid json: %s", line)
}
}
}))
defer srv.Close()
tests := []struct {
name string
f func(*slog.Logger)
maxBufSize int
events int
batches int
}{
{
name: "1 batch 1 event",
f: func(l *slog.Logger) {
l.Debug("message", "k1", "v1")
},
maxBufSize: DefaultMaximumSize,
events: 1,
batches: 1,
},
{
name: "1 quoted batch 1 event",
f: func(l *slog.Logger) {
l.Debug(`msg: "'@#$~^&*}{ęLukáš`, "k1", "v1")
},
maxBufSize: DefaultMaximumSize,
events: 1,
batches: 1,
},
{
name: "1 batch 3 events",
f: func(l *slog.Logger) {
l.Debug("m1")
l.Debug("m2")
l.Debug("m3")
},
maxBufSize: DefaultMaximumSize,
events: 3,
batches: 1,
},
{
name: "10 batches",
f: func(l *slog.Logger) {
for i := 0; i < 10; i++ {
l.Debug("m", "i", i)
}
},
maxBufSize: 1,
events: 10,
batches: 10,
},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%v", tt.name), func(t *testing.T) {
emptyLines = 0
c := SplunkConfig{
Level: slog.LevelDebug,
URL: srv.URL,
Source: "s",
Hostname: "h",
DefaultMaximumSize: tt.maxBufSize,
}
h := NewSplunkHandler(context.Background(), c)
logger := slog.New(h)
tt.f(logger)
h.Close()
stats := h.Statistics()
if int(stats.EventCount) != tt.events {
t.Fatalf("expected %d events, got %d", tt.events, stats.EventCount)
}
if int(stats.BatchCount) != tt.batches {
t.Fatalf("expected %d batches, got %d", tt.batches, stats.BatchCount)
}
// one emtpy line per batch
if emptyLines != tt.batches {
t.Fatalf("expected %d empty lines, got %d", tt.batches, emptyLines)
}
})
}
}
func TestSplunkHandlerBatching(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
buf := new(bytes.Buffer)
buf.ReadFrom(r.Body)
lines := strings.Split(buf.String(), "\n")
for _, line := range lines {
if line == "" {
continue
}
if !json.Valid([]byte(line)) {
t.Fatalf("invalid json: %s", line)
}
}
}))
defer srv.Close()
c := SplunkConfig{
Level: slog.LevelDebug,
URL: srv.URL,
Source: "s",
Hostname: "h",
DefaultMaximumSize: 1000,
}
h := NewSplunkHandler(context.Background(), c)
logger := slog.New(h).WithGroup("g").With("kg1", "kv1")
for i := 0; i < 4000; i++ {
logger.Debug("msg", "i", i)
}
h.Close()
stats := h.Statistics()
t.Logf("events: %d, batches: %d", stats.EventCount, stats.BatchCount)
if stats.EventCount != 4000 {
t.Fatalf("expected 4000 events, got %d", stats.BatchCount)
}
// This can depend on call stack (event length)
if stats.BatchCount == 0 || stats.BatchCount == stats.EventCount {
t.Fatalf("expected 1000 batches, got %d", stats.BatchCount)
}
}
func TestSlogtest(t *testing.T) {
messages := make(chan []byte)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
buf, err := io.ReadAll(r.Body)
if err != nil {
t.Fatalf("read error: %v", err)
}
messages <- buf
}))
defer srv.Close()
defer close(messages)
c := SplunkConfig{
Level: slog.LevelDebug,
URL: srv.URL,
Source: "s",
Hostname: "h",
DefaultMaximumSize: 1, // force batching
}
slogtest.Run(t, func(t *testing.T) slog.Handler {
return NewSplunkHandler(context.Background(), c)
}, func(t *testing.T) map[string]any {
m := make(map[string]any)
buf := <-messages
err := json.Unmarshal(buf, &m)
if err != nil {
t.Fatalf("invalid json: %v\n%s", err, string(buf))
}
// unwrap the event
return m["event"].(map[string]any)["message"].(map[string]any)
})
}