-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllog_test.go
160 lines (131 loc) · 3.57 KB
/
llog_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
package llog
import (
"bytes"
"io/ioutil"
"regexp"
"sync/atomic"
. "testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTruncate(t *T) {
assert.Equal(t, "abc", Truncate("abc", 4))
assert.Equal(t, "abc", Truncate("abc", 3))
assert.Equal(t, "ab...", Truncate("abc", 2))
}
func TestKV(t *T) {
var kv KV
assert.NotNil(t, kv.Copy())
assert.Empty(t, kv.Copy())
kv = KV{"foo": "a"}
kv2 := kv.Copy()
kv["bar"] = "b"
kv2["bar"] = "bb"
assert.Equal(t, KV{"foo": "a", "bar": "b"}, kv)
assert.Equal(t, KV{"foo": "a", "bar": "bb"}, kv2)
kv = KV{"foo": "a"}
kv2 = kv.Set("bar", "wat")
assert.Equal(t, KV{"foo": "a"}, kv)
assert.Equal(t, KV{"foo": "a", "bar": "wat"}, kv2)
kv = Merge(
KV{"foo": "aaaaa"},
KV{"foo": "a", "bar": "bbbbb"},
KV{"bar": "b"},
)
assert.Equal(t, KV{"foo": "a", "bar": "b"}, kv)
}
func TestLLog(t *T) {
// Unfortunately due to the nature of the package all testing involving Out
// must be syncronous
buf := bytes.NewBuffer(make([]byte, 0, 128))
Out = buf
assertOut := func(expected string) {
out, err := buf.ReadString('\n')
require.Nil(t, err)
assert.Equal(t, expected, out)
}
SetLevelFromString("INFO")
Debug("foo")
Info("bar")
Warn("baz")
Error("buz")
Flush()
assertOut("~ INFO -- bar\n")
assertOut("~ WARN -- baz\n")
assertOut("~ ERROR -- buz\n")
SetLevelFromString("WARN")
Debug("foo")
Info("bar")
Warn("baz")
Error("buz", KV{"a": "b"})
Flush()
assertOut("~ WARN -- baz\n")
assertOut("~ ERROR -- buz -- a=\"b\"\n")
}
func TestEntryPrintOut(t *T) {
assertEntry := func(postfix string, e entry) {
expectedRegex := regexp.MustCompile(`^~ ` + postfix + `\n$`)
expectedRegexTS := regexp.MustCompile(`^~ \[[^\]]+\] ` + postfix + `\n$`)
buf := bytes.NewBuffer(make([]byte, 0, 128))
require.Nil(t, e.printOut(buf, false))
require.Nil(t, e.printOut(buf, true))
noTS, err := buf.ReadString('\n')
require.Nil(t, err)
assert.True(t, expectedRegex.MatchString(noTS), "regex: %q line: %q", expectedRegex.String(), noTS)
withTS, err := buf.ReadString('\n')
require.Nil(t, err)
assert.True(t, expectedRegexTS.MatchString(withTS), "regex: %q line: %q", expectedRegexTS.String(), withTS)
}
e := entry{
level: InfoLevel,
msg: "this is a test",
}
assertEntry("INFO -- this is a test", e)
e.kvSlice = KV{}.StringSlice()
assertEntry("INFO -- this is a test", e)
e.kvSlice = KV{"foo": "a"}.StringSlice()
assertEntry("INFO -- this is a test -- foo=\"a\"", e)
e.kvSlice = KV{"foo": "a", "bar": "b"}.StringSlice()
assertEntry("INFO -- this is a test -- bar=\"b\" foo=\"a\"", e)
e.kvSlice = Merge(
KV{"foo": "aaaaa"},
KV{"foo": "a"},
KV{"bar": "b"},
).StringSlice()
assertEntry("INFO -- this is a test -- bar=\"b\" foo=\"a\"", e)
}
type sleepingWriter chan bool
// Write implements the io.Writer interface and sleeps until the underlying
// channel is closed
func (ch sleepingWriter) Write(p []byte) (n int, err error) {
<-ch
return len(p), nil
}
func TestBlocking(t *T) {
oldOut := Out
defer func() {
Out = oldOut
}()
sleep := make(chan bool)
Out = sleepingWriter(sleep)
SetLevelFromString("INFO")
var done int64
go func() {
logEntry(InfoLevel, "test", nil, true)
atomic.AddInt64(&done, 1)
}()
// make sure it hasn't returned yet
time.Sleep(10 * time.Millisecond)
assert.Zero(t, atomic.LoadInt64(&done))
// now make sure it has
close(sleep)
time.Sleep(10 * time.Millisecond)
assert.NotZero(t, atomic.LoadInt64(&done))
}
func BenchmarkLLog(b *B) {
Out = ioutil.Discard
for n := 0; n < b.N; n++ {
Info("This is a generic message", KV{"foo": "bar"})
}
}