-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger_test.go
372 lines (325 loc) · 9.35 KB
/
logger_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
package grip
import (
"errors"
"fmt"
"os"
"os/exec"
"testing"
"github.com/tychoish/fun/assert/check"
"github.com/tychoish/grip/level"
"github.com/tychoish/grip/message"
"github.com/tychoish/grip/send"
)
func TestLogger(t *testing.T) {
const name = "gripTest"
testSender := func(t *testing.T) send.Sender {
t.Helper()
sender := send.MakePlain()
sender.SetName(name)
sender.SetPriority(level.Trace)
if sender.Name() != name {
t.Errorf("sender is named %q not %q", sender.Name(), name)
}
return sender
}
t.Run("PanicSenderPanics", func(t *testing.T) {
// both of these are in anonymous functions so that the defers
// cover the correct area.
func() {
// first make sure that the default send method doesn't panic
defer func() {
if r := recover(); r != nil {
t.Fatal(r)
}
}()
gripImpl := NewLogger(testSender(t))
gripImpl.sendPanic(level.Invalid, message.MakeLines("foo"))
gripImpl.Log(level.Critical, message.MakeLines("foo"))
}()
func() {
// call a panic function with a recoverer set.
defer func() {
if r := recover(); r == nil {
t.Fatal("did not panic in expected situation")
}
}()
gripImpl := NewLogger(testSender(t))
gripImpl.sendPanic(level.Info, message.MakeLines("foo"))
}()
func() {
// call a panic function with a recoverer set.
defer func() {
if r := recover(); r == nil {
t.Fatal("did not panic in expected situation")
}
}()
EmergencyPanic(message.MakeLines("bar"))
}()
})
t.Run("PanicRespectsThreshold", func(t *testing.T) {
grip := NewLogger(testSender(t))
if level.Debug < grip.Sender().Priority() {
t.Fatal("level ordering is not correct")
}
grip.Sender().SetPriority(level.Notice)
if level.Debug > grip.Sender().Priority() {
t.Fatal("level ordering is not correct")
}
// test that there is a no panic if the message isn't "logabble"
defer func() {
if r := recover(); r != nil {
t.Fatal("panic doesn't respect level")
}
}()
grip.sendPanic(level.Debug, message.MakeLines("foo"))
})
t.Run("PriorityIsSet", func(t *testing.T) {
m := message.MakeLines("hello")
m.SetPriority(level.Info)
logger := NewLogger(testSender(t))
check.Equal(t, m.Priority(), level.Info)
logger.send(95, m)
check.Equal(t, m.Priority(), 95)
logger.send(0, m)
check.Equal(t, m.Priority(), 0)
logger.send(level.Warning, m)
check.Equal(t, m.Priority(), level.Warning)
logger.send(level.Priority(103), m)
logger.send(level.Warning, m)
})
t.Run("ConditionalSend", func(t *testing.T) {
// because sink is an internal type (implementation of
// sender,) and "GetMessage" isn't in the interface, though it
// is exported, we can't pass the sink between functions.
sink := send.MakeInternal()
sink.SetName("sink")
sink.SetPriority(level.Info)
grip := NewLogger(sink)
msg := message.MakeLines("foo")
msg.SetPriority(level.Info)
msgTwo := message.MakeLines("bar")
msgTwo.SetPriority(level.Notice)
// when the conditional argument is true, it should work
grip.Log(msg.Priority(), message.When(true, msg))
if msg.Raw() != sink.GetMessage().Message.Raw() {
t.Fatal("messages is not propagated")
}
// when the conditional argument is true, it should work, and the channel is fifo
grip.Log(msgTwo.Priority(), message.When(false, msgTwo))
grip.Log(msg.Priority(), message.When(true, msg))
result := sink.GetMessage().Message
if result.Loggable() {
if msg.Raw() != result.Raw() {
t.Fatal("message is not propagated")
}
} else {
if msgTwo.Raw() != result.Raw() {
t.Fatal("message is not propagated")
}
}
// change the order
grip.Log(msg.Priority(), message.When(true, msg))
grip.Log(msgTwo.Priority(), message.When(false, msgTwo))
result = sink.GetMessage().Message
if result.Loggable() {
if msg.Raw() != result.Raw() {
t.Fatal("message is not propagated")
}
} else {
if msgTwo.Raw() != result.Raw() {
t.Fatal("message is not propagated")
}
}
})
t.Run("CatchMethods", func(t *testing.T) {
sink := send.MakeInternal()
sink.SetName("sink")
sink.SetPriority(level.Trace)
grip := NewLogger(sink)
cases := []any{
grip.Alert,
grip.Critical,
grip.Debug,
grip.Emergency,
grip.Error,
grip.Info,
grip.Notice,
grip.Warning,
grip.Alertf,
grip.Criticalf,
grip.Debugf,
grip.Emergencyf,
grip.Errorf,
grip.Infof,
grip.Noticef,
grip.Warningf,
grip.AlertWhen,
grip.CriticalWhen,
grip.DebugWhen,
grip.EmergencyWhen,
grip.ErrorWhen,
grip.InfoWhen,
grip.NoticeWhen,
grip.WarningWhen,
func(w bool, m any) { grip.LogWhen(w, level.Info, m) },
func(m any) { grip.Log(level.Info, m) },
func(m string, a ...any) { grip.Logf(level.Info, m, a...) },
func(m ...message.Composer) { grip.Log(level.Info, m) },
func(m []message.Composer) { grip.Log(level.Info, m) },
func(w bool, m ...message.Composer) { grip.LogWhen(w, level.Info, m) },
func(w bool, m []message.Composer) { grip.LogWhen(w, level.Info, m) },
func(in any) { grip.Build().Any(in).Level(level.Info).Send() },
}
const msg = "hello world!"
multiMessage := []message.Composer{
message.Convert[message.Composer](nil),
message.Convert(msg),
}
for idx, logger := range cases {
if sink.Len() != 0 {
t.Fatalf("sink has %d", sink.Len())
}
if sink.HasMessage() {
t.Fatal("messages exist in sink before test")
}
switch log := logger.(type) {
case func(error):
log(errors.New(msg))
case func(any):
log(msg)
case func(...any):
log(msg, "", nil)
case func(string, ...any):
log("%s", msg)
case func(bool, any):
log(false, msg)
log(true, msg)
case func(bool, ...any):
log(false, msg, "", nil)
log(true, msg, "", nil)
case func(bool, string, ...any):
log(false, "%s", msg)
log(true, "%s", msg)
case func(...message.Composer):
log(multiMessage...)
case func(bool, ...message.Composer):
log(false, multiMessage...)
log(true, multiMessage...)
case func([]message.Composer):
log(multiMessage)
case func(bool, []message.Composer):
log(false, multiMessage)
log(true, multiMessage)
default:
panic(fmt.Sprintf("%T is not supported\n", log))
}
if sink.Len() > 1 {
// this is the many case
var numLogged int
out := sink.GetMessage()
for i := 0; i < sink.Len(); i++ {
out = sink.GetMessage()
if out.Logged {
numLogged++
if out.Rendered != msg {
t.Fatal("message rendered incorrectly")
}
}
}
if numLogged != 1 {
t.Fatalf("[id=%d] %T: %d %s", idx, logger, numLogged, out.Priority)
}
continue
}
if sink.Len() != 1 {
t.Fatal("sink has incorrect number of messages", sink.Len())
}
if !sink.HasMessage() {
t.Fatal("sink does not have any messages")
}
out := sink.GetMessage()
if out.Rendered != msg {
t.Fatal("message rendered incorrectly")
}
if !out.Logged {
t.Fatalf("[id=%d] %T %s", idx, logger, out.Priority)
}
}
})
t.Run("DefaultJournalerIsBootstrap", func(t *testing.T) {
grip := NewLogger(testSender(t))
firstName := grip.Sender().Name()
// the bootstrap sender is a bit special because you can't
// change it's name, therefore:
const secondName = "something_else"
grip.Sender().SetName(secondName)
if grip.Sender().Name() != secondName {
t.Fatal("name incorrect")
}
if grip.Sender().Name() == firstName {
t.Fatal("name incorrect")
}
if firstName == secondName {
t.Fatal("names should not be equal")
}
})
t.Run("NameControler", func(t *testing.T) {
grip := NewLogger(testSender(t))
for _, name := range []string{"a", "a39df", "a@)(*E)"} {
grip.Sender().SetName(name)
if grip.Sender().Name() != name {
t.Fatal("name was not correctly set")
}
}
})
t.Run("StandardName", func(t *testing.T) {
check.Equal(t, "grip", std.impl.Get().Name())
prev := os.Args[0]
defer func() { os.Args[0] = prev }()
os.Args[0] = "merlin"
setupDefault()
check.Equal(t, "merlin", std.impl.Get().Name())
})
}
// This testing method uses the technique outlined in:
// http://stackoverflow.com/a/33404435 to test a function that exits
// since it's impossible to "catch" an os.Exit
func TestSendFatalExits(t *testing.T) {
t.Run("Exit", func(t *testing.T) {
grip := NewLogger(send.MakeStdOutput())
if os.Getenv("SHOULD_CRASH") == "1" {
grip.sendFatal(level.Error, message.MakeLines("foo"))
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestSendFatalExits")
cmd.Env = append(os.Environ(), "SHOULD_CRASH=1")
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
err := cmd.Run()
if err == nil {
t.Errorf("sendFatal should have exited 0, instead: %+v", err)
}
})
t.Run("EmergencyFatal", func(t *testing.T) {
if os.Getenv("SHOULD_CRASH") == "1" {
EmergencyFatal("bar")
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestSendFatalExits")
cmd.Env = append(os.Environ(), "SHOULD_CRASH=1")
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
err := cmd.Run()
if err == nil {
t.Errorf("sendFatal should have exited 0, instead: %+v", err)
}
})
t.Run("RespectsPriority", func(t *testing.T) {
grip := NewLogger(send.MakeStdOutput())
grip.impl.Get().SetErrorHandler(send.ErrorHandlerFromSender(std.Sender()))
grip.impl.Get().SetPriority(level.Warning)
// shouldn't fail
grip.sendFatal(level.Debug, message.Convert("hello world"))
grip.sendFatal(0, message.Convert("hello world"))
})
}