forked from bitdabbler/fluent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_test.go
73 lines (61 loc) · 1.63 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
package fluent
import (
"context"
"log/slog"
"reflect"
"testing"
"time"
)
func TestSlogger_SlogtestTestClient(t *testing.T) {
c := newTestClient()
p, err := NewEncoderPool(testTag, nil)
if err != nil {
t.Fatalf("failed to get EncoderPool: %v", err)
}
h := NewHandlerCustom(c, p, &HandlerOptions{Verbose: true})
results := func() []map[string]any {
return c.recordsWithTimeInlined()
}
if err := TestHandler(h, results); err != nil {
t.Fatalf("failed TestHandler: %v", err)
}
}
func TestSlogger_HandlesContextValues(t *testing.T) {
host := "127.0.0.1"
groupKey := "req"
ts, err := newTestServer(nil)
if err != nil {
t.Fatalf("failed to create test server: %v", err)
}
c, err := NewClient(host, &ClientOptions{
Port: ts.port,
Verbose: true,
})
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
p, err := NewEncoderPool(testTag, nil)
if err != nil {
t.Fatalf("failed to get EncoderPool: %v", err)
}
h := NewHandlerCustom(c, p, nil)
l := slog.New(h)
m := TestMessage{
Tag: testTag,
Time: time.Now(),
Record: map[string]any{
"msg": "this is a test message",
"level": "INFO",
"key-01": "value-01",
"req": map[string]any{"method": "GET"},
},
}
ctx := context.WithValue(context.Background(), ContextKey, slog.Group(groupKey, m.Record["req"]))
l.InfoContext(ctx, "message-1", "key-1", "value-1")
received := <-ts.messageCh
if ctxVal, ok := received.Record[groupKey]; !ok {
t.Fatalf("missing slog.Attr passed via context: '%s'", groupKey)
} else if reflect.DeepEqual(ctxVal, m.Record[groupKey]) {
t.Fatalf("expected: %+v, got: %+v", m.Record[groupKey], ctxVal)
}
}