-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger_test.go
188 lines (156 loc) · 4.84 KB
/
logger_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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package logger
import (
"bytes"
"fmt"
re "regexp"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
)
const (
_Rprio = `<(?<prio>[0-9]+)>:`
_Rdate = `(?<date>[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9])`
_Rtime = `(?<time>[0-9][0-9]:[0-9][0-9]:[0-9][0-9].(?<frac>[0-9]+))`
_Rline = `(?<line>[0-9][0-9]*)`
_Rlongfile = `\((?<fpath>.*/[A-Za-z0-9_\-]+\.go):` + _Rline + `\)`
_Rshortfile = `\((?<fname>[A-Za-z0-9_\-]+\.go):` + _Rline + `\)`
_Rreltime = `\+(?<reltime>[0-9]+.?[0-9]+)[^\s]+`
_Rspace = `\s+`
_Rprefix = `\[(?<pref>[\w-]+)\] `
_Rlogmsg = `(?<msg>.+)`
)
type expGroups uint
type testcase struct {
flag int // logger flags
prefix string // log prefix
msg string // message to log
pat string // regex pattern to match the output
}
var tests = []testcase{
{0, "foo", "hello world", _Rprio + _Rdate + _Rspace + _Rtime + _Rspace + _Rprefix + _Rlogmsg},
{Ldate, "foo", "date", _Rprio + _Rdate + _Rspace + _Rprefix + _Rlogmsg},
{Ltime, "foo", "time", _Rprio + _Rtime + _Rspace + _Rprefix + _Rlogmsg},
{Ltime | Lmicroseconds, "foo", "time+us", _Rprio + _Rtime + _Rspace + _Rprefix + _Rlogmsg},
{Ldate | Ltime | Lfileloc, "foo", "file trace", _Rprio + _Rdate + _Rspace + _Rtime + _Rspace + _Rprefix + _Rlogmsg},
{Lreltime, "foo", "reltime", _Rprio + _Rreltime + _Rspace + _Rprefix + _Rlogmsg},
}
func makeSubMap(rx *re.Regexp, s string) (map[string]string, error) {
m := make(map[string]string)
mx := rx.FindAllStringSubmatch(s, -1)
if len(mx) != 1 {
return nil, fmt.Errorf("rx-mat exp 1; saw %d", len(mx))
}
subs := mx[0]
names := rx.SubexpNames()
for i := 1; i < len(subs); i++ {
k := names[i]
m[k] = subs[i]
}
return m, nil
}
func doTest(t *testing.T, tc *testcase) {
assert := newAsserter(t, tc.msg)
rx, err := re.Compile(tc.pat)
assert(err == nil, "re-compile: %s: %s", tc.pat, err)
var wr bytes.Buffer
ll, err := New(&wr, LOG_INFO, tc.prefix, tc.flag)
assert(err == nil, "can't create log: %s", err)
ll.Info(tc.msg)
ll.Close()
// skip the first line of logging; it's informational
_, err = wr.ReadString('\n')
assert(err == nil, "read hdr string: %s", err)
out, err := wr.ReadString('\n')
assert(err == nil, "read string: %s", err)
m, err := makeSubMap(rx, out)
assert(err == nil, "<%s>: %s", out, err)
for k, v := range m {
switch k {
case "prio":
want := fmt.Sprintf("%d", LOG_INFO)
assert(want == m["prio"], "match: prio: exp %s, saw %s", want, m["prio"])
case "date":
if tc.flag == 0 || tc.flag&Ldate > 0 {
assert(len(v) > 0, "match: date: exp value; saw nil")
} else {
assert(len(v) == 0, "match: date: saw %s, exp empty", v)
}
case "time":
if tc.flag == 0 || tc.flag&Ltime > 0 {
frac := m["frac"]
assert(len(v) > 0, "match: time: exp value; saw nil")
assert(len(frac) >= 3, "match: time: frac explen min 3, saw %d", len(frac))
if tc.flag&Lmicroseconds > 0 {
assert(len(frac) == 6, "match: time us frac: explen 6, saw %d", len(frac))
}
} else {
assert(len(v) == 0, "match: time: saw %s, exp empty", v)
}
case "pref":
assert(v == tc.prefix, "match: pref exp '%s', saw '%s'", tc.prefix, v)
case "msg":
assert(v == tc.msg, "match: msg exp '%s', saw '%s'", tc.msg, v)
case "reltime":
if tc.flag&Lreltime > 0 {
assert(len(v) > 0, "match: reltime: exp non empty ts")
} else {
assert(len(v) == 0, "match: reltime: saw %s, exp empty", v)
}
case "fname":
if tc.flag&Lfileloc > 0 {
line := m["line"]
assert(len(v) > 0, "match: fname: exp value; saw nil")
assert(len(line) > 0, "match: fname; exp line; saw nil")
} else {
assert(len(v) == 0, "match: fname: saw %s, exp empty", v)
}
case "fpath":
if tc.flag&Lfileloc > 0 {
line := m["line"]
assert(len(v) > 0, "match: fpath: exp value; saw nil")
assert(len(line) > 0, "match: fpath; exp line; saw nil")
} else {
assert(len(v) == 0, "match: fpath: saw %s, exp empty", v)
}
}
}
}
func TestLog(t *testing.T) {
for i := range tests {
tc := &tests[i]
doTest(t, tc)
}
}
func TestConcurrent(t *testing.T) {
const maxG int = 5000
assert := newAsserter(t, "")
var wr bytes.Buffer
var wg sync.WaitGroup
var i int
ll, err := New(&wr, LOG_INFO, "", Ldate|Ltime|Lmicroseconds|Lfileloc)
assert(err == nil, "can't make logger: %s", err)
var c atomic.Uint64
fn := func(i int, l Logger, wg *sync.WaitGroup) {
for j := 0; j < 100; j++ {
ll.Info("go-%d: Log message %d", i, j)
c.Add(1)
time.Sleep(5 * time.Microsecond)
}
wg.Done()
}
for i = 0; i < min(100, maxG); i++ {
wg.Add(1)
go fn(i, ll, &wg)
}
time.Sleep(2 * time.Second)
ll.Close()
wg.Wait()
logs := wr.String()
exp := uint64(strings.Count(logs, "\n"))
// the first and last lines are from the logger itself.
// XXX We don't have a test for log rotation.
saw := 2 + c.Load()
assert(exp == saw, "log lines: exp %d, saw %d", exp, saw)
}