-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathlogger_test.go
More file actions
192 lines (162 loc) · 4.28 KB
/
Copy pathlogger_test.go
File metadata and controls
192 lines (162 loc) · 4.28 KB
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
package gg
import (
"bytes"
"context"
"log/slog"
"strings"
"sync"
"testing"
)
func TestNopHandler_Enabled(t *testing.T) {
h := nopHandler{}
for _, level := range []slog.Level{slog.LevelDebug, slog.LevelInfo, slog.LevelWarn, slog.LevelError} {
if h.Enabled(context.Background(), level) {
t.Errorf("nopHandler.Enabled(%v) = true, want false", level)
}
}
}
func TestNopHandler_Handle(t *testing.T) {
h := nopHandler{}
if err := h.Handle(context.Background(), slog.Record{}); err != nil {
t.Errorf("nopHandler.Handle() = %v, want nil", err)
}
}
func TestNopHandler_WithAttrs(t *testing.T) {
h := nopHandler{}
got := h.WithAttrs([]slog.Attr{slog.String("key", "val")})
if _, ok := got.(nopHandler); !ok {
t.Errorf("nopHandler.WithAttrs() returned %T, want nopHandler", got)
}
}
func TestNopHandler_WithGroup(t *testing.T) {
h := nopHandler{}
got := h.WithGroup("group")
if _, ok := got.(nopHandler); !ok {
t.Errorf("nopHandler.WithGroup() returned %T, want nopHandler", got)
}
}
func TestLoggerDefaultSilent(t *testing.T) {
l := Logger()
if l == nil {
t.Fatal("Logger() returned nil")
}
// Default logger must be disabled at all levels.
for _, level := range []slog.Level{slog.LevelDebug, slog.LevelInfo, slog.LevelWarn} {
if l.Enabled(context.Background(), level) {
t.Errorf("default logger should not be enabled for %v", level)
}
}
}
func TestSetLogger(t *testing.T) {
orig := Logger()
t.Cleanup(func() { SetLogger(orig) })
var buf bytes.Buffer
custom := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{
Level: slog.LevelDebug,
}))
SetLogger(custom)
got := Logger()
if got != custom {
t.Error("Logger() did not return the custom logger set via SetLogger")
}
// Verify output is captured.
got.Info("test message", "key", "value")
if !strings.Contains(buf.String(), "test message") {
t.Errorf("expected log output to contain 'test message', got: %s", buf.String())
}
}
func TestSetLoggerNilRestoresSilent(t *testing.T) {
orig := Logger()
t.Cleanup(func() { SetLogger(orig) })
// First set a real logger.
SetLogger(slog.Default())
// Then set nil to restore silence.
SetLogger(nil)
l := Logger()
if l == nil {
t.Fatal("SetLogger(nil) should set nop logger, not nil")
}
if l.Enabled(context.Background(), slog.LevelError) {
t.Error("SetLogger(nil) should produce a disabled logger")
}
}
func TestSetLoggerPropagatesToAccelerator(t *testing.T) {
orig := Logger()
t.Cleanup(func() {
SetLogger(orig)
resetAccelerator()
})
resetAccelerator()
mock := &mockAccelerator{name: "logger-test"}
accelMu.Lock()
accel = mock
accelMu.Unlock()
custom := slog.New(slog.NewTextHandler(&bytes.Buffer{}, nil))
SetLogger(custom)
if mock.logger != custom {
t.Error("SetLogger did not propagate to accelerator via loggerSetter")
}
}
func TestRegisterAcceleratorPropagatesCurrentLogger(t *testing.T) {
orig := Logger()
t.Cleanup(func() {
SetLogger(orig)
resetAccelerator()
})
resetAccelerator()
// Set a custom logger before registration.
custom := slog.New(slog.NewTextHandler(&bytes.Buffer{}, nil))
SetLogger(custom)
// Register accelerator — it should receive the current logger.
mock := &mockAccelerator{name: "propagation-test"}
if err := RegisterAccelerator(mock); err != nil {
t.Fatalf("RegisterAccelerator() = %v", err)
}
if mock.logger != custom {
t.Error("RegisterAccelerator did not propagate current logger to accelerator")
}
}
func TestLoggerConcurrentAccess(t *testing.T) {
orig := Logger()
t.Cleanup(func() { SetLogger(orig) })
var wg sync.WaitGroup
const goroutines = 100
// Concurrent readers.
for range goroutines {
wg.Add(1)
go func() {
defer wg.Done()
l := Logger()
if l == nil {
t.Error("Logger() returned nil during concurrent access")
}
// Exercise the logger — must not panic.
l.Debug("concurrent read")
}()
}
// Concurrent writers.
for range goroutines {
wg.Add(1)
go func() {
defer wg.Done()
SetLogger(slog.Default())
SetLogger(nil)
}()
}
wg.Wait()
}
func BenchmarkLoggerLoad(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
l := Logger()
_ = l
}
}
func BenchmarkLoggerDisabledLog(b *testing.B) {
// Benchmark the hot path: calling a log method on a disabled logger.
l := Logger()
b.ReportAllocs()
for b.Loop() {
l.Debug("message", "key", "value")
}
}