OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / go-kit / kit / log / value_test.go
1 package log_test
2
3 import (
4         "encoding"
5         "fmt"
6         "reflect"
7         "testing"
8         "time"
9
10         "github.com/go-kit/kit/log"
11 )
12
13 func TestValueBinding(t *testing.T) {
14         t.Parallel()
15         var output []interface{}
16
17         logger := log.Logger(log.LoggerFunc(func(keyvals ...interface{}) error {
18                 output = keyvals
19                 return nil
20         }))
21
22         start := time.Date(2015, time.April, 25, 0, 0, 0, 0, time.UTC)
23         now := start
24         mocktime := func() time.Time {
25                 now = now.Add(time.Second)
26                 return now
27         }
28
29         lc := log.With(logger, "ts", log.Timestamp(mocktime), "caller", log.DefaultCaller)
30
31         lc.Log("foo", "bar")
32         timestamp, ok := output[1].(time.Time)
33         if !ok {
34                 t.Fatalf("want time.Time, have %T", output[1])
35         }
36         if want, have := start.Add(time.Second), timestamp; want != have {
37                 t.Errorf("output[1]: want %v, have %v", want, have)
38         }
39         if want, have := "value_test.go:31", fmt.Sprint(output[3]); want != have {
40                 t.Errorf("output[3]: want %s, have %s", want, have)
41         }
42
43         // A second attempt to confirm the bindings are truly dynamic.
44         lc.Log("foo", "bar")
45         timestamp, ok = output[1].(time.Time)
46         if !ok {
47                 t.Fatalf("want time.Time, have %T", output[1])
48         }
49         if want, have := start.Add(2*time.Second), timestamp; want != have {
50                 t.Errorf("output[1]: want %v, have %v", want, have)
51         }
52         if want, have := "value_test.go:44", fmt.Sprint(output[3]); want != have {
53                 t.Errorf("output[3]: want %s, have %s", want, have)
54         }
55 }
56
57 func TestValueBinding_loggingZeroKeyvals(t *testing.T) {
58         t.Parallel()
59         var output []interface{}
60
61         logger := log.Logger(log.LoggerFunc(func(keyvals ...interface{}) error {
62                 output = keyvals
63                 return nil
64         }))
65
66         start := time.Date(2015, time.April, 25, 0, 0, 0, 0, time.UTC)
67         now := start
68         mocktime := func() time.Time {
69                 now = now.Add(time.Second)
70                 return now
71         }
72
73         logger = log.With(logger, "ts", log.Timestamp(mocktime))
74
75         logger.Log()
76         timestamp, ok := output[1].(time.Time)
77         if !ok {
78                 t.Fatalf("want time.Time, have %T", output[1])
79         }
80         if want, have := start.Add(time.Second), timestamp; want != have {
81                 t.Errorf("output[1]: want %v, have %v", want, have)
82         }
83
84         // A second attempt to confirm the bindings are truly dynamic.
85         logger.Log()
86         timestamp, ok = output[1].(time.Time)
87         if !ok {
88                 t.Fatalf("want time.Time, have %T", output[1])
89         }
90         if want, have := start.Add(2*time.Second), timestamp; want != have {
91                 t.Errorf("output[1]: want %v, have %v", want, have)
92         }
93 }
94
95 func TestTimestampFormat(t *testing.T) {
96         t.Parallel()
97
98         start := time.Date(2015, time.April, 25, 0, 0, 0, 0, time.UTC)
99         now := start
100         mocktime := func() time.Time {
101                 now = now.Add(time.Second)
102                 return now
103         }
104
105         tv := log.TimestampFormat(mocktime, time.RFC822)
106
107         if want, have := now.Add(time.Second).Format(time.RFC822), fmt.Sprint(tv()); want != have {
108                 t.Errorf("wrong time format: want %v, have %v", want, have)
109         }
110
111         if want, have := now.Add(2*time.Second).Format(time.RFC822), fmt.Sprint(tv()); want != have {
112                 t.Errorf("wrong time format: want %v, have %v", want, have)
113         }
114
115         mustMarshal := func(v interface{}) []byte {
116                 b, err := v.(encoding.TextMarshaler).MarshalText()
117                 if err != nil {
118                         t.Fatal("error marshaling text:", err)
119                 }
120                 return b
121         }
122
123         if want, have := now.Add(3*time.Second).AppendFormat(nil, time.RFC822), mustMarshal(tv()); !reflect.DeepEqual(want, have) {
124                 t.Errorf("wrong time format: want %s, have %s", want, have)
125         }
126
127         if want, have := now.Add(4*time.Second).AppendFormat(nil, time.RFC822), mustMarshal(tv()); !reflect.DeepEqual(want, have) {
128                 t.Errorf("wrong time format: want %s, have %s", want, have)
129         }
130 }
131
132 func BenchmarkValueBindingTimestamp(b *testing.B) {
133         logger := log.NewNopLogger()
134         lc := log.With(logger, "ts", log.DefaultTimestamp)
135         b.ReportAllocs()
136         b.ResetTimer()
137         for i := 0; i < b.N; i++ {
138                 lc.Log("k", "v")
139         }
140 }
141
142 func BenchmarkValueBindingCaller(b *testing.B) {
143         logger := log.NewNopLogger()
144         lc := log.With(logger, "caller", log.DefaultCaller)
145         b.ReportAllocs()
146         b.ResetTimer()
147         for i := 0; i < b.N; i++ {
148                 lc.Log("k", "v")
149         }
150 }