/
log_test.go
148 lines (122 loc) · 3.39 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
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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016 Datadog, Inc.
package log
import (
"fmt"
"strings"
"sync"
"testing"
"time"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
"github.com/stretchr/testify/assert"
)
// testLogger implements a mock ddtrace.Logger.
type testLogger struct {
mu sync.RWMutex
lines []string
}
// Print implements ddtrace.Logger.
func (tp *testLogger) Log(msg string) {
tp.mu.Lock()
defer tp.mu.Unlock()
tp.lines = append(tp.lines, msg)
}
// Lines returns the lines that were printed using this logger.
func (tp *testLogger) Lines() []string {
tp.mu.RLock()
defer tp.mu.RUnlock()
return tp.lines
}
// Reset resets the logger's internal buffer.
func (tp *testLogger) Reset() {
tp.mu.Lock()
tp.lines = tp.lines[:0]
tp.mu.Unlock()
}
func TestLog(t *testing.T) {
defer func(old ddtrace.Logger) { UseLogger(old) }(logger)
tp := &testLogger{}
UseLogger(tp)
t.Run("Warn", func(t *testing.T) {
tp.Reset()
Warn("message %d", 1)
assert.Equal(t, msg("WARN", "message 1"), tp.Lines()[0])
})
t.Run("Debug", func(t *testing.T) {
t.Run("on", func(t *testing.T) {
tp.Reset()
defer func(old Level) { level = old }(level)
SetLevel(LevelDebug)
assert.True(t, DebugEnabled())
Debug("message %d", 3)
assert.Equal(t, msg("DEBUG", "message 3"), tp.Lines()[0])
})
t.Run("off", func(t *testing.T) {
tp.Reset()
assert.False(t, DebugEnabled())
Debug("message %d", 2)
assert.Len(t, tp.Lines(), 0)
})
})
t.Run("Error", func(t *testing.T) {
t.Run("auto", func(t *testing.T) {
defer func(old time.Duration) { errrate = old }(errrate)
errrate = 10 * time.Millisecond
tp.Reset()
Error("a message %d", 1)
Error("a message %d", 2)
Error("a message %d", 3)
Error("b message")
time.Sleep(2 * errrate)
assert.True(t, hasMsg("ERROR", "a message 1, 2 additional messages skipped", tp.Lines()), tp.Lines())
assert.True(t, hasMsg("ERROR", "b message", tp.Lines()), tp.Lines())
assert.Len(t, tp.Lines(), 2)
})
t.Run("flush", func(t *testing.T) {
tp.Reset()
Error("fourth message %d", 4)
Flush()
assert.True(t, hasMsg("ERROR", "fourth message 4", tp.Lines()), tp.Lines())
assert.Len(t, tp.Lines(), 1)
Flush()
Flush()
assert.Len(t, tp.Lines(), 1)
})
t.Run("limit", func(t *testing.T) {
tp.Reset()
for i := 0; i < defaultErrorLimit+1; i++ {
Error("fifth message %d", i)
}
Flush()
assert.True(t, hasMsg("ERROR", "fifth message 0, 200+ additional messages skipped", tp.Lines()), tp.Lines())
assert.Len(t, tp.Lines(), 1)
})
t.Run("instant", func(t *testing.T) {
tp.Reset()
defer func(old time.Duration) { errrate = old }(errrate)
errrate = time.Duration(0) * time.Second // mimic the env. var.
Error("fourth message %d", 4)
assert.True(t, hasMsg("ERROR", "fourth message 4", tp.Lines()), tp.Lines())
assert.Len(t, tp.Lines(), 1)
})
})
}
func BenchmarkError(b *testing.B) {
Error("k %s", "a") // warm up cache
for i := 0; i < b.N; i++ {
Error("k %s", "a")
}
}
func hasMsg(lvl, m string, lines []string) bool {
for _, line := range lines {
if strings.HasPrefix(line, msg(lvl, m)) {
return true
}
}
return false
}
func msg(lvl, msg string) string {
return fmt.Sprintf("%s %s: %s", prefixMsg, lvl, msg)
}