-
Notifications
You must be signed in to change notification settings - Fork 1
/
triggeredloghandler_test.go
230 lines (218 loc) · 6.02 KB
/
triggeredloghandler_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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package triggeredloghandler
import (
"context"
"errors"
"log/slog"
"testing"
"time"
)
func TestNew(t *testing.T) {
target := &mockHandler{}
const streamID = "STREAM_ID"
const triggerLevel = slog.LevelDebug
tlh := NewTriggeredLogHandler(target, streamID, triggerLevel)
if tlh.base.triggerLevel != triggerLevel {
t.Errorf(
"Expect trigger level %s but %s",
triggerLevel.String(),
tlh.base.triggerLevel.String(),
)
}
if len(target.attrs) != 1 {
t.Errorf("Expect 1 attribute but %+v", target.attrs)
} else {
if target.attrs[0].Key != TriggeredLogStreamIDKey {
t.Errorf(
"Expect attribute key %s but %s",
TriggeredLogStreamIDKey,
target.attrs[0].Key,
)
}
if target.attrs[0].Value.String() != streamID {
t.Errorf(
"Expect attribute value %s but %s",
streamID,
target.attrs[0].Value.String(),
)
}
}
}
func TestWithAttrs(t *testing.T) {
target := &mockHandler{}
const streamID = "STREAM_ID"
const triggerLevel = slog.LevelDebug
const attrKey = "TEST_ATTR_KEY"
const attrValue = "TEST_ATTR_VALUE"
tlh := NewTriggeredLogHandler(target, streamID, triggerLevel)
tlh2 := tlh.WithAttrs(
[]slog.Attr{{Key: attrKey, Value: slog.StringValue(attrValue)}},
).(*TriggeredLogHandler)
if tlh.base != tlh2.base {
t.Error("Base data was altered by WithAttrs()")
}
if len(target.attrs) != 1 {
t.Errorf("Expect 1 attribute but %+v", target.attrs)
} else {
if target.attrs[0].Key != attrKey {
t.Errorf("Expect key %s but got %s", attrKey, target.attrs[0].Key)
}
if target.attrs[0].Value.String() != attrValue {
t.Errorf(
"Expect value %s but got %s",
attrValue,
target.attrs[0].Value.String(),
)
}
}
}
func TestWithGroup(t *testing.T) {
target := &mockHandler{}
const streamID = "STREAM_ID"
const triggerLevel = slog.LevelDebug
const group = "GROUP_NAME"
tlh := NewTriggeredLogHandler(target, streamID, triggerLevel)
tlh2 := tlh.WithGroup(group).(*TriggeredLogHandler)
if tlh.base != tlh2.base {
t.Error("Base data was altered by WithGroup()")
}
if target.group != group {
t.Errorf("Expected group to be %s but %s", group, target.group)
}
}
func TestHandle(t *testing.T) {
target := &mockHandler{}
const streamID = "STREAM_ID"
const triggerLevel = slog.LevelWarn
tlh := NewTriggeredLogHandler(target, streamID, triggerLevel)
type contextType string
const contextKey = contextType("CONTEXT_KEY")
const contextValue1 = "CONTEXT_VALUE_1"
ctx := context.WithValue(context.Background(), contextKey, contextValue1)
const message1 = "MESSAGE 1"
record := slog.NewRecord(time.Now(), slog.LevelDebug, message1, 0)
err := tlh.Handle(ctx, record)
if err != nil {
t.Fatal(err)
}
if len(target.records) != 0 {
t.Fatalf("No records should be logged but %+v", target.records)
}
if len(tlh.base.backlog) != 1 {
t.Fatalf("Expect 1 record in backlog but %+v", tlh.base.backlog)
}
if tlh.base.backlog[0].record.Message != message1 {
t.Fatalf(
"Expect message %s but %s",
message1,
tlh.base.backlog[0].record.Message,
)
}
const contextValue2 = "CONTEXT_VALUE_2"
ctx = context.WithValue(context.Background(), contextKey, contextValue2)
const message2 = "MESSAGE 2"
record = slog.NewRecord(time.Now(), slog.LevelError, message2, 0)
err = tlh.Handle(ctx, record)
if err != nil {
t.Fatal(err)
}
if len(tlh.base.backlog) != 0 {
t.Fatalf("Expect empty backlog but %+v", tlh.base.backlog)
}
if len(target.records) != 2 {
t.Fatalf("Should have logged 2 records but %+v", target.records)
}
if target.records[0].record.Message != message1 {
t.Errorf(
"Record 0 should be %s but %s",
message1,
target.records[0].record.Message,
)
}
if target.records[0].ctx.Value(contextKey) != contextValue1 {
t.Errorf(
"Record 0 should have context value %s but %s",
contextValue1,
target.records[0].ctx.Value(contextKey),
)
}
if target.records[1].record.Message != message2 {
t.Errorf(
"Record 1 should be %s but %s",
message2,
target.records[1].record.Message,
)
}
if target.records[1].ctx.Value(contextKey) != contextValue2 {
t.Errorf(
"Record 1 should have context value %s but %s",
contextValue2,
target.records[1].ctx.Value(contextKey),
)
}
}
func TestFailurePreservesMessages(t *testing.T) {
target := &mockHandler{}
const streamID = "STREAM_ID"
const triggerLevel = slog.LevelWarn
tlh := NewTriggeredLogHandler(target, streamID, triggerLevel)
const message1 = "MESSAGE 1"
record := slog.NewRecord(time.Now(), slog.LevelDebug, message1, 0)
err := tlh.Handle(context.Background(), record)
if err != nil {
t.Fatal(err)
}
if len(tlh.base.backlog) != 1 {
t.Fatalf("Should have 1 record in backlog, but %+v", tlh.base.backlog)
}
target.handleError = true
const message2 = "MESSAGE 2"
record = slog.NewRecord(time.Now(), slog.LevelError, message2, 0)
err = tlh.Handle(context.Background(), record)
if err == nil {
t.Fatal("Error was not bubbled up")
}
if len(tlh.base.backlog) != 2 {
t.Fatalf("Should have 2 records in backlog, but %+v", tlh.base.backlog)
}
target.handleError = false
const message3 = "MESSAGE 3"
record = slog.NewRecord(time.Now(), slog.LevelError, message3, 0)
err = tlh.Handle(context.Background(), record)
if err != nil {
t.Fatal(err)
}
if len(tlh.base.backlog) != 0 {
t.Fatalf("backlog should be empty, but %+v", tlh.base.backlog)
}
if len(target.records) != 3 {
t.Fatalf("Should be 3 logged records, but %+v", target.records)
}
}
type mockHandler struct {
handleError bool
attrs []slog.Attr
group string
records []logRecord
}
type logRecord struct {
ctx context.Context
record slog.Record
}
func (mh *mockHandler) Enabled(_ context.Context, _ slog.Level) bool {
return true
}
func (mh *mockHandler) Handle(ctx context.Context, record slog.Record) error {
if mh.handleError {
return errors.New("mock handler error")
}
mh.records = append(mh.records, logRecord{ctx, record})
return nil
}
func (mh *mockHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
mh.attrs = attrs
return mh
}
func (mh *mockHandler) WithGroup(name string) slog.Handler {
mh.group = name
return mh
}