-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogcontext_test.go
428 lines (375 loc) · 13.3 KB
/
logcontext_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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package ulog
import (
"context"
"errors"
"fmt"
"strings"
"testing"
"time"
"github.com/blugnu/test"
)
func (lc *logcontext) String() string {
return fmt.Sprintf("ctx=%v, logger=%v, dispatcher=%v, fields=%v, xfields=%v, exitCode=%v", lc.ctx, lc.logger, lc.dispatcher, lc.fields, lc.xfields, lc.exitCode)
}
func TestLogContext(t *testing.T) {
// ARRANGE
type key int
var (
ctx = context.Background()
sut = &logcontext{}
)
testcases := []struct {
scenario string
exec func(t *testing.T)
}{
// fromContext tests
{scenario: "fromContext/same context",
exec: func(t *testing.T) {
// ACT
result := sut.fromContext(ctx)
// ASSERT
test.Value(t, result).Equals(sut)
},
},
{scenario: "fromContext/new context",
exec: func(t *testing.T) {
// ARRANGE
ctx = context.WithValue(ctx, key(1), 0)
// ACT
result := sut.fromContext(ctx)
// ASSERT
test.Value(t, result).DoesNotEqual(sut)
},
},
// makeEntry tests
{scenario: "makeEntry/level disabled",
exec: func(t *testing.T) {
// ARRANGE
sut.logger.Level = InfoLevel
// ACT
result := sut.makeEntry(DebugLevel, "debug")
// ASSERT
test.Value(t, result).Equals(noop.entry)
},
},
{scenario: "makeEntry/no fields",
exec: func(t *testing.T) {
// ACT
result := sut.makeEntry(InfoLevel, "info")
// ASSERT
test.IsTrue(t, result.logcontext == sut, "same logcontext")
test.That(t, result.logcontext.fields, "entry fields").IsNil()
test.That(t, result.Level, "level").Equals(InfoLevel)
test.That(t, result.Message, "message").Equals("info")
},
},
{scenario: "makeEntry/merges unmerged fields",
exec: func(t *testing.T) {
// ARRANGE
sut.xfields = map[string]any{"key": "value"}
// ACT
result := sut.makeEntry(InfoLevel, "info")
// ASSERT
test.IsTrue(t, result.logcontext == sut, "same logcontext")
test.That(t, result.logcontext.fields.m, "entry fields").Equals(map[string]any{"key": "value"})
test.That(t, result.Level, "level").Equals(InfoLevel)
test.That(t, result.Message, "message").Equals("info")
},
},
// new test
{scenario: "new",
exec: func(t *testing.T) {
// ARRANGE
mux := &mux{targets: []*target{}}
sut.dispatcher = mux
// ACT
result := sut.new(ctx, sut.dispatcher, map[string]any{"key": "value"}, 42)
// ASSERT
test.That(t, result).Equals(&logcontext{
ctx: ctx,
logger: sut.logger,
dispatcher: sut.dispatcher,
xfields: map[string]any{"key": "value"},
exitCode: 42,
})
},
},
{scenario: "new/adds to existing fields",
exec: func(t *testing.T) {
// ARRANGE
sut.xfields = map[string]any{"key": "value"}
// ACT
result := sut.new(ctx, sut.dispatcher, map[string]any{"key-1": "value"}, 42)
// ASSERT
test.That(t, result).Equals(&logcontext{
ctx: ctx,
logger: sut.logger,
dispatcher: sut.dispatcher,
xfields: map[string]any{
"key": "value",
"key-1": "value",
},
exitCode: 42,
})
},
},
// AtLevel tests
{scenario: "AtLevel/level disabled",
exec: func(t *testing.T) {
// ARRANGE
sut.logger.Level = InfoLevel
// ACT
result := sut.AtLevel(DebugLevel)
// ASSERT
test.Value(t, result).Equals(noop.levellogger)
},
},
{scenario: "AtLevel/level enabled",
exec: func(t *testing.T) {
// ACT
result := sut.AtLevel(InfoLevel)
// ASSERT
test.That(t, result).Equals(&levelLogger{
logcontext: sut,
Level: InfoLevel,
})
},
},
// LogTo tests
{scenario: "LogTo/with valid target",
exec: func(t *testing.T) {
// ARRANGE
mux := &mux{
targets: []*target{{id: "id"}},
}
og := sut.dispatcher
defer func() { sut.dispatcher = og }()
sut.dispatcher = mux
// ACT
result, ok := sut.LogTo("id")
// ASSERT
test.IsTrue(t, ok, "found target")
test.That(t, result.(*logcontext).dispatcher, "dispatcher").Equals(mux.targets[0])
},
},
{scenario: "LogTo/with invalid target",
exec: func(t *testing.T) {
// ARRANGE
mux := &mux{targets: []*target{}}
og := sut.dispatcher
defer func() { sut.dispatcher = og }()
sut.dispatcher = mux
// ACT
result, ok := sut.LogTo("xid")
// ASSERT
test.IsFalse(t, ok, "found target")
test.Value(t, result, "logger").Equals(noop.logger)
},
},
// WithContext tests
{scenario: "WithContext",
exec: func(t *testing.T) {
// ARRANGE
ctx := context.WithValue(ctx, key(1), 0)
// ACT
result := sut.WithContext(ctx)
// ASSERT
test.That(t, result).Equals(&logcontext{
ctx: ctx,
logger: sut.logger,
dispatcher: sut.dispatcher,
})
},
},
// WithField tests
{scenario: "WithField",
exec: func(t *testing.T) {
// ACT
result := sut.WithField("key", "value")
// ASSERT
test.That(t, result).Equals(&logcontext{
ctx: sut.ctx,
logger: sut.logger,
dispatcher: sut.dispatcher,
xfields: map[string]any{"key": "value"},
})
},
},
{scenario: "WithField/logcontext fields are merged uniquely",
exec: func(t *testing.T) {
// ACT
result1 := sut.WithField("key1", "value1").(*logcontext)
result1.Info("log")
result2 := sut.WithField("key2", "value2").(*logcontext)
result2.Info("log")
// ASSERT
test.That(t, result1.fields.m).Equals(map[string]any{"key1": "value1"})
test.That(t, result1.xfields).IsNil()
test.That(t, result2.fields.m).Equals(map[string]any{"key2": "value2"})
test.That(t, result2.xfields).IsNil()
},
},
// WithFields tests
{scenario: "WithFields",
exec: func(t *testing.T) {
// ACT
result := sut.WithFields(map[string]any{
"key-1": "value",
"key-2": "value",
})
// ASSERT
test.That(t, result).Equals(&logcontext{
ctx: sut.ctx,
logger: sut.logger,
dispatcher: sut.dispatcher,
xfields: map[string]any{
"key-1": "value",
"key-2": "value",
},
})
},
},
}
for _, tc := range testcases {
t.Run(tc.scenario, func(t *testing.T) {
// ARRANGE
sut = &logcontext{ctx: ctx,
logger: &logger{
Level: InfoLevel,
enabled: sut.isLevelEnabled,
getCallsite: noCaller,
backend: noop,
},
}
sut.logger.enrich = sut.logger.noEnrichment // TODO: this is horrible - can this be refactored to avoid it? a nil receiver method maybe?
// ACT
tc.exec(t)
})
}
}
func TestLogContext_Logging(t *testing.T) {
// ARRANGE
var (
ctx = context.Background()
mock = &mockdispatcher{}
logger = &logger{backend: mock}
et = time.Date(2010, 9, 8, 7, 6, 5, 432100, time.UTC)
closeWasCalled = false
exitWasCalled = false
)
sut, _ := logger.init(ctx)
logger.closeFn = func() { closeWasCalled = true }
ognow := now
defer func() { now = ognow }()
now = func() time.Time { return et }
// logs emitted at FatalLevel are expected to close the logger and call exit
ogexit := ExitFn
defer func() { ExitFn = ogexit }()
ExitFn = func(int) { exitWasCalled = true }
// run testcases covering Log and Logf at each log level, when the level is
// both enabled and disabled
{
for _, level := range Levels {
testcases := []struct {
scenario string
logf bool // set true for a Logf() test case, otherwise test Log()
entry
}{
{scenario: "Log/%s/level disabled", logf: false, entry: noop.entry},
{scenario: "Logf/%s/level disabled", logf: true, entry: noop.entry},
{scenario: "Log/%s/level enabled", logf: false, entry: entry{logcontext: sut, Time: et, Level: level, Message: "log"}},
{scenario: "Logf/%s/level enabled", logf: true, entry: entry{logcontext: sut, Time: et, Level: level, Message: "log with arg"}},
}
for _, tc := range testcases {
t.Run(fmt.Sprintf(tc.scenario, level), func(t *testing.T) {
// ARRANGE
closeWasCalled = false
exitWasCalled = false
mock.Reset()
logger.enabled = func(context.Context, Level) bool { return strings.Contains(tc.scenario, "level enabled") }
// ACT
switch tc.logf {
case false:
sut.Log(level, "log")
case true:
sut.Logf(level, "log with %s", "arg")
}
// ASSERT
test.That(t, mock.entry, "emitted").Equals(tc.entry)
// using Log() or Logf() even at FatalLevel should NOT call exit
test.IsFalse(t, exitWasCalled, "calls exit")
test.IsFalse(t, closeWasCalled, "closes the logger")
})
}
}
}
// the remaining tests cover the different logging methods, and the different
// behaviours when the level is enabled and disabled
{
// ARRANGE
testcases := []struct {
scenario string
act func()
entry
}{
// logging with simple messages or errors
// level disabled
{scenario: "Trace/level disabled", act: func() { sut.Trace("log") }, entry: noop.entry},
{scenario: "Debug/level disabled", act: func() { sut.Debug("log") }, entry: noop.entry},
{scenario: "Info/level disabled", act: func() { sut.Info("log") }, entry: noop.entry},
{scenario: "Warn/level disabled", act: func() { sut.Warn("log") }, entry: noop.entry},
{scenario: "Error/level disabled", act: func() { sut.Error("msg") }, entry: noop.entry},
{scenario: "Fatal/level disabled", act: func() { sut.Fatal("msg") }, entry: noop.entry},
// level enabled
{scenario: "Trace/level enabled", act: func() { sut.Trace("log") }, entry: entry{logcontext: sut, Time: et, Level: TraceLevel, Message: "log"}},
{scenario: "Debug/level enabled", act: func() { sut.Debug("log") }, entry: entry{logcontext: sut, Time: et, Level: DebugLevel, Message: "log"}},
{scenario: "Info/level enabled", act: func() { sut.Info("log") }, entry: entry{logcontext: sut, Time: et, Level: InfoLevel, Message: "log"}},
{scenario: "Warn/level enabled", act: func() { sut.Warn("log") }, entry: entry{logcontext: sut, Time: et, Level: WarnLevel, Message: "log"}},
{scenario: "Error(error)/level enabled", act: func() { sut.Error(errors.New("error")) }, entry: entry{logcontext: sut, Time: et, Level: ErrorLevel, Message: "error"}},
{scenario: "Error(string)/level enabled", act: func() { sut.Error("error") }, entry: entry{logcontext: sut, Time: et, Level: ErrorLevel, Message: "error"}},
{scenario: "Error(int)/level enabled", act: func() { sut.Error(1) }, entry: entry{logcontext: sut, Time: et, Level: ErrorLevel, Message: "1"}},
{scenario: "Fatal(error)/level enabled", act: func() { sut.Fatal(errors.New("error")) }, entry: entry{logcontext: sut, Time: et, Level: FatalLevel, Message: "error"}},
{scenario: "Fatal(string)/level enabled", act: func() { sut.Fatal("error") }, entry: entry{logcontext: sut, Time: et, Level: FatalLevel, Message: "error"}},
{scenario: "Fatal(int)/level enabled", act: func() { sut.Fatal(1) }, entry: entry{logcontext: sut, Time: et, Level: FatalLevel, Message: "1"}},
// logging with format verbs and args
// level disabled
{scenario: "Tracef/level disabled", act: func() { sut.Tracef("log with %s", "arg") }, entry: noop.entry},
{scenario: "Debugf/level disabled", act: func() { sut.Debugf("log with %s", "arg") }, entry: noop.entry},
{scenario: "Infof/level disabled", act: func() { sut.Infof("log with %s", "arg") }, entry: noop.entry},
{scenario: "Warnf/level disabled", act: func() { sut.Warnf("log with %s", "arg") }, entry: noop.entry},
{scenario: "Errorf/level disabled", act: func() { sut.Errorf("msg with %s", "arg") }, entry: noop.entry},
{scenario: "Fatalf/level disabled", act: func() { sut.Fatalf("msg with %s", "arg") }, entry: noop.entry},
// level enabled
{scenario: "Tracef/level enabled", act: func() { sut.Tracef("log with %s", "arg") }, entry: entry{logcontext: sut, Time: et, Level: TraceLevel, Message: "log with arg"}},
{scenario: "Debugf/level enabled", act: func() { sut.Debugf("log with %s", "arg") }, entry: entry{logcontext: sut, Time: et, Level: DebugLevel, Message: "log with arg"}},
{scenario: "Infof/level enabled", act: func() { sut.Infof("log with %s", "arg") }, entry: entry{logcontext: sut, Time: et, Level: InfoLevel, Message: "log with arg"}},
{scenario: "Warnf/level enabled", act: func() { sut.Warnf("log with %s", "arg") }, entry: entry{logcontext: sut, Time: et, Level: WarnLevel, Message: "log with arg"}},
{scenario: "Errorf/level enabled", act: func() { sut.Errorf("msg with %s", "arg") }, entry: entry{logcontext: sut, Time: et, Level: ErrorLevel, Message: "msg with arg"}},
{scenario: "Fatalf/level enabled", act: func() { sut.Fatalf("msg with %s", "arg") }, entry: entry{logcontext: sut, Time: et, Level: FatalLevel, Message: "msg with arg"}},
}
for _, tc := range testcases {
t.Run(tc.scenario, func(t *testing.T) {
// ARRANGE
closeWasCalled = false
exitWasCalled = false
logger.enabled = func(context.Context, Level) bool { return strings.Contains(tc.scenario, "level enabled") }
mock.Reset()
expectedToExit := strings.HasPrefix(tc.scenario, "Fatal")
// ACT
tc.act()
// ASSERT
test.That(t, mock.entry).Equals(tc.entry)
test.That(t, exitWasCalled, "calls exit").Equals(expectedToExit)
test.That(t, closeWasCalled, "closes the logger").Equals(expectedToExit)
})
}
}
}
func TestLogContext_ExitCode(t *testing.T) {
// ARRANGE
lc := &logcontext{}
// ACT
result := lc.WithExitCode(42).(*logcontext)
// ASSERT
test.That(t, result).Equals(&logcontext{exitCode: 42})
}