1
18
19 package binarylog
20
21 import (
22 "testing"
23
24 "google.golang.org/grpc/internal/grpctest"
25 )
26
27 type s struct {
28 grpctest.Tester
29 }
30
31 func Test(t *testing.T) {
32 grpctest.RunSubTests(t, s{})
33 }
34
35
36 func (s) TestGetMethodLogger(t *testing.T) {
37 testCases := []struct {
38 in string
39 method string
40 hdr, msg uint64
41 }{
42
43 {
44 in: "*{h:12;m:23}",
45 method: "/s/m",
46 hdr: 12, msg: 23,
47 },
48
49 {
50 in: "*,s/*{h:12;m:23}",
51 method: "/s/m",
52 hdr: 12, msg: 23,
53 },
54
55 {
56 in: "*{h;m},s/m{h:12;m:23}",
57 method: "/s/m",
58 hdr: 12, msg: 23,
59 },
60 {
61 in: "*{h;m},s/*{h:314;m},s/m{h:12;m:23}",
62 method: "/s/m",
63 hdr: 12, msg: 23,
64 },
65 {
66 in: "*{h;m},s/*{h:12;m:23},s/m",
67 method: "/s/m",
68 hdr: maxUInt, msg: maxUInt,
69 },
70
71
72 {
73 in: "*{h;m},s/*{h:12;m:23},s/m1",
74 method: "/s/m",
75 hdr: 12, msg: 23,
76 },
77 {
78 in: "*{h;m},s1/*,s/m{h:12;m:23}",
79 method: "/s/m",
80 hdr: 12, msg: 23,
81 },
82
83
84 {
85 in: "*{h:12;m:23},-s/m1",
86 method: "/s/m",
87 hdr: 12, msg: 23,
88 },
89 }
90 for _, tc := range testCases {
91 l := NewLoggerFromConfigString(tc.in)
92 if l == nil {
93 t.Errorf("in: %q, failed to create logger from config string", tc.in)
94 continue
95 }
96 ml := l.GetMethodLogger(tc.method).(*TruncatingMethodLogger)
97 if ml == nil {
98 t.Errorf("in: %q, method logger is nil, want non-nil", tc.in)
99 continue
100 }
101 if ml.headerMaxLen != tc.hdr || ml.messageMaxLen != tc.msg {
102 t.Errorf("in: %q, want header: %v, message: %v, got header: %v, message: %v", tc.in, tc.hdr, tc.msg, ml.headerMaxLen, ml.messageMaxLen)
103 }
104 }
105 }
106
107
108 func (s) TestGetMethodLoggerOff(t *testing.T) {
109 testCases := []struct {
110 in string
111 method string
112 }{
113
114 {
115 in: "s1/m",
116 method: "/s/m",
117 },
118 {
119 in: "s/m1",
120 method: "/s/m",
121 },
122 {
123 in: "s1/*",
124 method: "/s/m",
125 },
126 {
127 in: "s1/*,s/m1",
128 method: "/s/m",
129 },
130
131
132 {
133 in: "*,-s/m",
134 method: "/s/m",
135 },
136 {
137 in: "s/*,-s/m",
138 method: "/s/m",
139 },
140 {
141 in: "-s/m,s/*",
142 method: "/s/m",
143 },
144 }
145 for _, tc := range testCases {
146 l := NewLoggerFromConfigString(tc.in)
147 if l == nil {
148 t.Errorf("in: %q, failed to create logger from config string", tc.in)
149 continue
150 }
151 ml := l.GetMethodLogger(tc.method)
152 if ml != nil {
153 t.Errorf("in: %q, method logger is non-nil, want nil", tc.in)
154 }
155 }
156 }
157
View as plain text