-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLog_test.go
101 lines (79 loc) · 1.73 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
package log_test
import (
"errors"
"io"
"os"
"testing"
"time"
"github.com/aerogo/log"
"github.com/akyoto/assert"
)
// writerWithError errors the Write call after `successfulWrites` writes.
type writerWithError struct {
io.Writer
countWrites int
successfulWrites int
}
func (writer *writerWithError) Write(buffer []byte) (int, error) {
if writer.countWrites == writer.successfulWrites {
return 0, errors.New("Artificial error")
}
writer.countWrites++
return writer.Write(buffer)
}
// zeroWriter always writes zero bytes.
type zeroWriter struct {
io.Writer
}
func (writer *zeroWriter) Write(buffer []byte) (int, error) {
return 0, nil
}
func TestBasicLogging(t *testing.T) {
fileWriter := log.File("hello.log")
defer os.Remove("hello.log")
defer fileWriter.Close()
fileWriter2 := log.File("hello2.log")
defer os.Remove("hello2.log")
defer fileWriter2.Close()
fileWriter3 := log.File("hello3.log")
defer os.Remove("hello3.log")
defer fileWriter3.Close()
errorWriter := &writerWithError{
Writer: fileWriter2,
}
zero := &zeroWriter{
Writer: fileWriter3,
}
hello := log.New()
hello.AddWriter(fileWriter)
hello.AddWriter(errorWriter)
hello.AddWriter(zero)
hello.Info(
"Info message %d %f %f %s",
1,
float32(3.14),
3.14,
"some text",
)
hello.Error("Oh noes %s", "Something went wrong")
time.Sleep(500 * time.Millisecond)
}
func TestInvalidFilePath(t *testing.T) {
defer func() {
err := recover()
assert.NotNil(t, err)
}()
log.File("")
}
func BenchmarkInfo(b *testing.B) {
defer os.Remove("hello.log")
hello := log.New()
hello.AddWriter(log.File("hello.log"))
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
hello.Info("Hello World")
}
})
}