forked from zajann/easylog
-
Notifications
You must be signed in to change notification settings - Fork 1
/
easylog_test.go
172 lines (152 loc) · 3.89 KB
/
easylog_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
package easylog
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type AReader struct {
max int
soFar int
}
func (ar *AReader) Read(b []byte) (int, error) {
var i int
for i = 0; i < len(b) && i+ar.soFar < ar.max; i++ {
b[i] = 'A'
}
ar.soFar += i
if ar.soFar >= ar.max {
return i, io.EOF
}
return i, nil
}
func setUpTest(t *testing.T) {
// easylog prints to stdout, but that's just noise in the test output,
// so redirect to DevNull.
devNull, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0666)
require.NoError(t, err)
stdout = devNull
t.Cleanup(func() {
level = defaultLevel
logger = nil
fp = nil
maxSize = defaultMaxSizeBytes
filePath = ""
stdout = os.Stdout
})
logFilePath := filepath.Join(t.TempDir(), fmt.Sprint(t.Name(), ".log"))
assert.NoError(t, Init(logFilePath))
}
func assertPanics(t *testing.T, f func()) {
defer func() {
assert.NotNil(t, recover(), "The code did not panic")
}()
f()
}
func TestBasic(t *testing.T) {
setUpTest(t)
assert.Equal(t, logger, Logger())
SetLevel(INFO)
assert.Equal(t, INFO, GetLevel())
Debug("AAA")
Debugf("%s", "aaa")
Info("BBB")
Infof("%s", "bbb")
Warn("CCC")
Warnf("%s", "ccc")
Error("DDD")
Errorf("%s", "ddd")
assertPanics(t, func() { Fatal("EEE") })
assertPanics(t, func() { Fatalf("%s", "eee") })
logFile, err := os.Open(filePath)
require.NoError(t, err)
var foundInfo, foundWarn, foundError, foundFatal bool
var foundInfof, foundWarnf, foundErrorf, foundFatalf bool
for scnr := bufio.NewScanner(logFile); scnr.Scan(); {
line := scnr.Text()
assert.NotContains(t, line, "AAA")
assert.NotContains(t, line, "aaa")
assert.Contains(t, line, "easylog_test.go")
if strings.Contains(line, "BBB") {
foundInfo = true
assert.Contains(t, line, "Info")
} else if strings.Contains(line, "CCC") {
foundWarn = true
assert.Contains(t, line, "Warn")
} else if strings.Contains(line, "DDD") {
foundError = true
assert.Contains(t, line, "Error")
} else if strings.Contains(line, "EEE") {
foundFatal = true
assert.Contains(t, line, "Fatal")
} else if strings.Contains(line, "bbb") {
foundInfof = true
assert.Contains(t, line, "Info")
} else if strings.Contains(line, "ccc") {
foundWarnf = true
assert.Contains(t, line, "Warn")
} else if strings.Contains(line, "ddd") {
foundErrorf = true
assert.Contains(t, line, "Error")
} else if strings.Contains(line, "eee") {
foundFatalf = true
assert.Contains(t, line, "Fatal")
}
}
assert.True(t, foundInfo)
assert.True(t, foundWarn)
assert.True(t, foundError)
assert.True(t, foundFatal)
assert.True(t, foundInfof)
assert.True(t, foundWarnf)
assert.True(t, foundErrorf)
assert.True(t, foundFatalf)
require.NoError(t, logFile.Close())
}
func TestMaxSize(t *testing.T) {
setUpTest(t)
SetMaxSize(0)
assert.Equal(t, defaultMaxSizeBytes, maxSize)
// Check that the log file is rotated when it reaches the max size.
logFile, err := os.OpenFile(filePath, os.O_WRONLY, 0666)
require.NoError(t, err)
const maxSizeBytes = 1 << (10 * 2)
SetMaxSize(maxSizeBytes >> (10 * 2))
written, err := io.Copy(logFile, &AReader{max: maxSizeBytes})
require.NoError(t, err)
assert.Equal(t, int64(maxSizeBytes), written)
info, err := logFile.Stat()
require.NoError(t, err)
assert.Equal(t, int64(maxSizeBytes), info.Size())
require.NoError(t, logFile.Close())
Info("FFF")
logFile, err = os.Open(filePath)
require.NoError(t, err)
var numLines int
for scnr := bufio.NewScanner(logFile); scnr.Scan(); {
line := scnr.Text()
numLines += 1
assert.Contains(t, line, "FFF")
}
assert.Equal(t, 1, numLines)
}
func TestRace(t *testing.T) {
setUpTest(t)
go func() {
for i := 0; i < 1000; i++ {
Info("AAA")
}
}()
for i := 0; i < 1000; i++ {
Info("BBB")
}
}
func TestCheck(t *testing.T) {
assertPanics(t, func() { check(errors.New("yo")) })
}