-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathvalue_test.go
150 lines (126 loc) · 3.84 KB
/
value_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
package log_test
import (
"encoding"
"fmt"
"reflect"
"testing"
"time"
"github.com/go-kit/log"
)
func TestValueBinding(t *testing.T) {
t.Parallel()
var output []interface{}
logger := log.Logger(log.LoggerFunc(func(keyvals ...interface{}) error {
output = keyvals
return nil
}))
start := time.Date(2015, time.April, 25, 0, 0, 0, 0, time.UTC)
now := start
mocktime := func() time.Time {
now = now.Add(time.Second)
return now
}
lc := log.With(logger, "ts", log.Timestamp(mocktime), "caller", log.DefaultCaller)
lc.Log("foo", "bar")
timestamp, ok := output[1].(time.Time)
if !ok {
t.Fatalf("want time.Time, have %T", output[1])
}
if want, have := start.Add(time.Second), timestamp; want != have {
t.Errorf("output[1]: want %v, have %v", want, have)
}
if want, have := "value_test.go:31", fmt.Sprint(output[3]); want != have {
t.Errorf("output[3]: want %s, have %s", want, have)
}
// A second attempt to confirm the bindings are truly dynamic.
lc.Log("foo", "bar")
timestamp, ok = output[1].(time.Time)
if !ok {
t.Fatalf("want time.Time, have %T", output[1])
}
if want, have := start.Add(2*time.Second), timestamp; want != have {
t.Errorf("output[1]: want %v, have %v", want, have)
}
if want, have := "value_test.go:44", fmt.Sprint(output[3]); want != have {
t.Errorf("output[3]: want %s, have %s", want, have)
}
}
func TestValueBinding_loggingZeroKeyvals(t *testing.T) {
t.Parallel()
var output []interface{}
logger := log.Logger(log.LoggerFunc(func(keyvals ...interface{}) error {
output = keyvals
return nil
}))
start := time.Date(2015, time.April, 25, 0, 0, 0, 0, time.UTC)
now := start
mocktime := func() time.Time {
now = now.Add(time.Second)
return now
}
logger = log.With(logger, "ts", log.Timestamp(mocktime))
logger.Log()
timestamp, ok := output[1].(time.Time)
if !ok {
t.Fatalf("want time.Time, have %T", output[1])
}
if want, have := start.Add(time.Second), timestamp; want != have {
t.Errorf("output[1]: want %v, have %v", want, have)
}
// A second attempt to confirm the bindings are truly dynamic.
logger.Log()
timestamp, ok = output[1].(time.Time)
if !ok {
t.Fatalf("want time.Time, have %T", output[1])
}
if want, have := start.Add(2*time.Second), timestamp; want != have {
t.Errorf("output[1]: want %v, have %v", want, have)
}
}
func TestTimestampFormat(t *testing.T) {
t.Parallel()
start := time.Date(2015, time.April, 25, 0, 0, 0, 0, time.UTC)
now := start
mocktime := func() time.Time {
now = now.Add(time.Second)
return now
}
tv := log.TimestampFormat(mocktime, time.RFC822)
if want, have := now.Add(time.Second).Format(time.RFC822), fmt.Sprint(tv()); want != have {
t.Errorf("wrong time format: want %v, have %v", want, have)
}
if want, have := now.Add(2*time.Second).Format(time.RFC822), fmt.Sprint(tv()); want != have {
t.Errorf("wrong time format: want %v, have %v", want, have)
}
mustMarshal := func(v interface{}) []byte {
b, err := v.(encoding.TextMarshaler).MarshalText()
if err != nil {
t.Fatal("error marshaling text:", err)
}
return b
}
if want, have := now.Add(3*time.Second).AppendFormat(nil, time.RFC822), mustMarshal(tv()); !reflect.DeepEqual(want, have) {
t.Errorf("wrong time format: want %s, have %s", want, have)
}
if want, have := now.Add(4*time.Second).AppendFormat(nil, time.RFC822), mustMarshal(tv()); !reflect.DeepEqual(want, have) {
t.Errorf("wrong time format: want %s, have %s", want, have)
}
}
func BenchmarkValueBindingTimestamp(b *testing.B) {
logger := log.NewNopLogger()
lc := log.With(logger, "ts", log.DefaultTimestamp)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
lc.Log("k", "v")
}
}
func BenchmarkValueBindingCaller(b *testing.B) {
logger := log.NewNopLogger()
lc := log.With(logger, "caller", log.DefaultCaller)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
lc.Log("k", "v")
}
}