-
Notifications
You must be signed in to change notification settings - Fork 2
/
handler_test.go
102 lines (79 loc) · 2.1 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
package sqlog
import (
"context"
"log/slog"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// Mock do Ingester
type testMockIngester struct {
close func() error
ingest func(time time.Time, level int8, data []byte) error
}
func (m *testMockIngester) Close() error {
if m.close == nil {
return nil
}
return m.close()
}
func (m *testMockIngester) Ingest(time time.Time, level int8, data []byte) error {
if m.ingest == nil {
return nil
}
return m.ingest(time, level, data)
}
func Test_Handler_Handle(t *testing.T) {
var (
ingested bool
)
ingester := &testMockIngester{
ingest: func(time time.Time, level int8, data []byte) error {
ingested = true
return nil
},
}
logger := slog.New(newHandler(ingester, nil))
logger.Info("test message")
assert.True(t, ingested)
}
func Test_Handler_Enabled(t *testing.T) {
ingester := new(testMockIngester)
config := &HandlerConfig{
Options: &slog.HandlerOptions{
Level: slog.LevelWarn,
},
}
handler := newHandler(ingester, config)
ctx := context.Background()
assert.True(t, handler.Enabled(ctx, slog.LevelWarn))
assert.False(t, handler.Enabled(ctx, slog.LevelInfo))
}
func Test_Handler_Fanout(t *testing.T) {
var (
ingested int
ingestCount = func(time time.Time, level int8, data []byte) error {
ingested++
return nil
}
)
handler := newHandler(&testMockIngester{ingest: ingestCount}, &HandlerConfig{
Options: &slog.HandlerOptions{
Level: slog.LevelWarn, // will not ingest info msg
},
})
logger := slog.New(handler)
ctx := context.Background()
assert.True(t, handler.Enabled(ctx, slog.LevelWarn))
assert.False(t, handler.Enabled(ctx, slog.LevelInfo))
handler1 := newHandler(&testMockIngester{ingest: ingestCount}, nil)
handler2 := newHandler(&testMockIngester{ingest: ingestCount}, nil)
handler.fanout(handler1, handler2)
assert.Equal(t, 2, len(handler.handlers))
assert.True(t, handler.Enabled(ctx, slog.LevelWarn))
assert.True(t, handler.Enabled(ctx, slog.LevelInfo))
logger.Info("test message")
assert.Equal(t, 2, ingested) // only fanout
logger.Warn("test message")
assert.Equal(t, 5, ingested)
}