-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_test.go
104 lines (89 loc) · 2.48 KB
/
log_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
package log
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGlobalLogger(t *testing.T) {
testCases := []struct {
name string
level Level
mode WriteMode
message string
expectedLine string
}{
{name: "ONE", level: Debug, mode: ModeNonBlocking, message: "hello", expectedLine: "| INFO | ONE | hello"},
{name: "TWO", level: Debug, mode: ModeBlocking, message: "hello", expectedLine: "| INFO | TWO | hello"},
{name: "THREE", level: Info, mode: ModeBlocking, message: "hello", expectedLine: "| INFO | THREE | hello"},
{name: "FOUR", level: Error, mode: ModeBlocking, message: "hello", expectedLine: ""},
{name: "FIVE", level: Fatal, mode: ModeBlocking, message: "hello", expectedLine: ""},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
w1 := &DummyWriter{}
w2 := &DummyWriter{}
l := New(
WithName(tc.name),
WithLevel(tc.level),
WithMode(tc.mode),
WithWriter(w1, FormatText),
WithWriter(w2, FormatText),
)
l.Info(tc.message)
l.Close()
if tc.mode == ModeNonBlocking {
time.Sleep(100 * time.Millisecond)
}
if tc.expectedLine != "" {
require.NotEmpty(t, w1.Lines)
require.NotEmpty(t, w2.Lines)
assert.Contains(t, w1.Lines[0], tc.expectedLine)
assert.Contains(t, w2.Lines[0], tc.expectedLine)
} else {
assert.Empty(t, w1.Lines)
assert.Empty(t, w2.Lines)
}
})
}
}
func TestLocalLogger(t *testing.T) {
testCases := []struct {
name string
level Level
message string
expectedLine string
}{
{name: "ONE", level: Debug, message: "world", expectedLine: "| INFO | ONE | world"},
{name: "TWO", message: "world", expectedLine: ""},
{name: "THREE", level: Fatal, message: "world", expectedLine: ""},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
w1 := &DummyWriter{}
w2 := &DummyWriter{}
g := New(
WithLevel(Error),
WithMode(ModeBlocking),
WithWriter(w1, FormatText),
)
l := g.NewLocal(
WithName(tc.name),
WithWriter(w2, FormatText),
)
if tc.level != "" {
l.SetLevel(tc.level)
}
l.Info(tc.message)
if tc.expectedLine != "" {
require.NotEmpty(t, w1.Lines)
require.NotEmpty(t, w2.Lines)
assert.Contains(t, w1.Lines[0], tc.expectedLine)
assert.Contains(t, w2.Lines[0], tc.expectedLine)
} else {
assert.Empty(t, w1.Lines)
assert.Empty(t, w2.Lines)
}
})
}
}