-
Notifications
You must be signed in to change notification settings - Fork 0
/
logmanager_test.go
105 lines (85 loc) · 2.5 KB
/
logmanager_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
package logmanager
import (
"errors"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// doesn't actually test the logger, just lets us see what it looks like
func TestLookAtTheLogFormat(t *testing.T) {
logmodules := []Logger{
GetLogger("MODULEred"),
GetLogger("MODUleYellow"),
GetLogger("ModuLECYAN"),
GetLogger("ModuLEGREEN"),
GetLogger("ModuleBLue"),
GetLogger("ModuleMagenTA"),
GetLogger("ModuleWhite"),
}
// also read-add some of the log modules to test race conditions
for i := 0; i <= 10; i++ {
logmodules = append(logmodules, logmodules[0])
}
var wg sync.WaitGroup
for _, log := range logmodules {
wg.Add(1)
go func(logger Logger) {
logger.Trace("test trace")
logger.Debug("test debug")
logger.Info("test info")
logger.Warn("test warning")
_ = logger.Error("test error")
logger.Critical("test critical")
wg.Done()
}(log)
}
wg.Wait()
err := errors.New("test error")
if logmodules[0].IsError(err) == false {
t.Fail()
}
retErr := logmodules[0].Error("testError %s", err)
assert.EqualError(t, retErr, err.Error())
retErr = logmodules[0].Error("testError %s %s", "foo", err)
assert.EqualError(t, retErr, err.Error())
retErr = logmodules[0].Error("testError %s %s %s", "different thing", err, "foobar")
assert.EqualError(t, retErr, "testError different thing test error foobar")
}
func TestIsError(t *testing.T) {
logger := GetLogger("fuck.you.gord")
assert.False(t, logger.IsError(func() error {
return nil
}()))
assert.True(t, logger.IsError(func() error {
return errors.New("fuck")
}()))
}
func TestRecover(t *testing.T) {
logger := GetLogger("no.fuck.you")
assert := assert.New(t)
require := require.New(t)
check := func(panicfunc func()) (err error) {
defer func() {
if r := logger.Recover(recover()); r != nil {
err = r
}
}()
panicfunc()
return
}
checkPanicErr := func() error { return check(func() { panic(errors.New("oh no")) }) }
checkPanicString := func() error { return check(func() { panic("oh no") }) }
checkNoPanic := func() error { return check(func() {}) }
require.NotPanics(func() { _ = checkPanicErr() })
assert.EqualError(checkPanicErr(), "oh no")
assert.EqualError(checkPanicString(), "oh no")
require.NotPanics(func() { _ = checkNoPanic() })
assert.NoError(checkNoPanic())
}
func TestLogAndExtend(t *testing.T) {
logger := GetLogger("panic-test")
logger.Info("Logging happily")
SetCustomWriters(NewConsoleWriter())
logger.Info("Crashing... nah, got fixed")
}