-
Notifications
You must be signed in to change notification settings - Fork 669
Expand file tree
/
Copy pathconn_http_exception_test.go
More file actions
310 lines (291 loc) · 10.3 KB
/
Copy pathconn_http_exception_test.go
File metadata and controls
310 lines (291 loc) · 10.3 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package clickhouse
import (
"bytes"
"errors"
"strings"
"testing"
chproto "github.com/ClickHouse/ch-go/proto"
)
func TestParseExceptionFromBytes(t *testing.T) {
tests := []struct {
name string
data []byte
expectError bool
errorMsg string
}{
{
name: "valid exception with complete format",
data: []byte("\r\n__exception__\r\n1234567890123456\r\nDB::Exception: Table default.test_table doesn't exist\n42 1234567890123456\r\n__exception__\r\n"),
expectError: true,
errorMsg: "DB::Exception: Table default.test_table doesn't exist",
},
{
name: "exception with multiline error message",
data: []byte("\r\n__exception__\r\n1234567890123456\r\nDB::Exception: Syntax error\nExpected identifier\n50 1234567890123456\r\n__exception__\r\n"),
expectError: true,
errorMsg: "DB::Exception: Syntax error\nExpected identifier",
},
{
name: "exception without second marker",
data: []byte("\r\n__exception__\r\n1234567890123456\r\nDB::Exception: Connection timeout"),
expectError: true,
errorMsg: "DB::Exception: Connection timeout",
},
{
name: "no exception marker",
data: []byte("some random data without exception marker"),
expectError: true,
errorMsg: "exception marker not found",
},
{
name: "exception marker only",
data: []byte("__exception__\r\n\r\n\r\n__exception__"),
expectError: true,
errorMsg: "ClickHouse exception occurred but message is empty",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := parseExceptionFromBytes(tt.data)
if !tt.expectError {
if err != nil {
t.Errorf("expected no error, got: %v", err)
}
return
}
if err == nil {
t.Error("expected error, got nil")
return
}
if !strings.Contains(err.Error(), tt.errorMsg) {
t.Errorf("expected error to contain '%s', got: %v", tt.errorMsg, err)
}
})
}
}
// Real servers prefix the exception payload with "Code: NNN.", which the
// parser turns into a typed *Exception. The code-less fixtures above stay on
// the legacy plain-error fallback.
func TestParseExceptionFromBytesTyped(t *testing.T) {
tests := []struct {
name string
data []byte
wantCode int32
wantName string
wantMessage string
wantCodeName string
}{
{
name: "typed exception with complete format",
data: []byte("\r\n__exception__\r\n1234567890123456\r\nCode: 395. DB::Exception: Value passed to 'throwIf' function is non-zero: there is an exception. (FUNCTION_THROW_IF_VALUE_IS_NON_ZERO) (version 25.1.5.31 (official build))\n42 1234567890123456\r\n__exception__\r\n"),
wantCode: 395,
wantName: "DB::Exception",
wantMessage: "Value passed to 'throwIf' function is non-zero: there is an exception. (FUNCTION_THROW_IF_VALUE_IS_NON_ZERO) (version 25.1.5.31 (official build))",
wantCodeName: "FUNCTION_THROW_IF_VALUE_IS_NON_ZERO",
},
{
name: "typed exception without second marker",
data: []byte("\r\n__exception__\r\n1234567890123456\r\nCode: 60. DB::Exception: Unknown table expression identifier 'foo'. (UNKNOWN_TABLE)"),
wantCode: 60,
wantName: "DB::Exception",
wantMessage: "Unknown table expression identifier 'foo'. (UNKNOWN_TABLE)",
wantCodeName: "UNKNOWN_TABLE",
},
{
// Older servers (e.g. 25.8): bare message after the marker — no
// tag, no trailer line, no closing marker.
name: "unframed exception (25.8 layout)",
data: []byte("UInt8\x00__exception__\r\nCode: 395. DB::Exception: boom: while executing 'FUNCTION throwIf'. (FUNCTION_THROW_IF_VALUE_IS_NON_ZERO) (version 25.8.28.1 (official build))\n"),
wantCode: 395,
wantName: "DB::Exception",
wantMessage: "boom: while executing 'FUNCTION throwIf'. (FUNCTION_THROW_IF_VALUE_IS_NON_ZERO) (version 25.8.28.1 (official build))",
wantCodeName: "FUNCTION_THROW_IF_VALUE_IS_NON_ZERO",
},
{
// The block may contain more than one dump of the message (e.g. a
// truncated one consumed by block decode, then a complete one);
// the last dump of the SAME code wins.
name: "double dump takes the last complete message",
data: []byte("__exception__\r\nCode: 395. DB::Exception: truncat\nexception__\nCode: 395. DB::Exception: complete message. (FUNCTION_THROW_IF_VALUE_IS_NON_ZERO)\n"),
wantCode: 395,
wantName: "DB::Exception",
wantMessage: "complete message. (FUNCTION_THROW_IF_VALUE_IS_NON_ZERO)",
wantCodeName: "FUNCTION_THROW_IF_VALUE_IS_NON_ZERO",
},
{
// Error messages echo user strings, which can embed a fake
// "Code: N." — it must not hijack the anchor: the code and the
// full message come from the real, first occurrence.
name: "embedded fake code does not hijack the anchor",
data: []byte("__exception__\r\nCode: 395. DB::Exception: Value passed to 'throwIf' function is non-zero: fake Code: 60. DB::Exception: injected. (FUNCTION_THROW_IF_VALUE_IS_NON_ZERO)\n"),
wantCode: 395,
wantName: "DB::Exception",
wantMessage: "Value passed to 'throwIf' function is non-zero: fake Code: 60. DB::Exception: injected. (FUNCTION_THROW_IF_VALUE_IS_NON_ZERO)",
wantCodeName: "FUNCTION_THROW_IF_VALUE_IS_NON_ZERO",
},
{
// Error messages echo user strings, which can embed the literal
// "__exception__" marker. In a framed block the message must be
// cut at the *closing* marker (preceded by the trailer line), not
// at the embedded one — otherwise the message and CodeName are
// truncated.
name: "embedded marker in framed message is not truncated",
data: []byte("\r\n__exception__\r\n1234567890123456\r\nCode: 395. DB::Exception: throwIf failed on '__exception__' value. (FUNCTION_THROW_IF_VALUE_IS_NON_ZERO)\n42 1234567890123456\r\n__exception__\r\n"),
wantCode: 395,
wantName: "DB::Exception",
wantMessage: "throwIf failed on '__exception__' value. (FUNCTION_THROW_IF_VALUE_IS_NON_ZERO)",
wantCodeName: "FUNCTION_THROW_IF_VALUE_IS_NON_ZERO",
},
{
// Same, for the unframed (25.8) layout: no trailer and no closing
// marker, so the embedded marker is the only occurrence and must
// stay in the message rather than truncating it.
name: "embedded marker in unframed message is not truncated",
data: []byte("UInt8\x00__exception__\r\nCode: 60. DB::Exception: table '__exception__' not found. (UNKNOWN_TABLE) (version 25.8.28.1 (official build))\n"),
wantCode: 60,
wantName: "DB::Exception",
wantMessage: "table '__exception__' not found. (UNKNOWN_TABLE) (version 25.8.28.1 (official build))",
wantCodeName: "UNKNOWN_TABLE",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := parseExceptionFromBytes(tt.data)
if err == nil {
t.Fatal("expected error, got nil")
}
var ex *Exception
if !errors.As(err, &ex) {
t.Fatalf("expected errors.As to find *Exception, got: %v", err)
}
if ex.Code != tt.wantCode {
t.Errorf("Code: expected %d, got %d", tt.wantCode, ex.Code)
}
if ex.Name != tt.wantName {
t.Errorf("Name: expected %q, got %q", tt.wantName, ex.Name)
}
if ex.Message != tt.wantMessage {
t.Errorf("Message: expected %q, got %q", tt.wantMessage, ex.Message)
}
if ex.CodeName != tt.wantCodeName {
t.Errorf("CodeName: expected %q, got %q", tt.wantCodeName, ex.CodeName)
}
var httpErr *HTTPError
if errors.As(err, &httpErr) {
t.Error("mid-stream exception must not be wrapped in *HTTPError")
}
})
}
}
func TestIsFramedException(t *testing.T) {
tests := []struct {
name string
buf string
want bool
}{
{
name: "tagged framed layout",
buf: "\r\n__exception__\r\n1234567890123456\r\nCode: 60. DB::Exception: boom. (UNKNOWN_TABLE)\n42 1234567890123456\r\n__exception__\r\n",
want: true,
},
{
name: "bare 25.8 layout",
buf: "UInt8\x00__exception__\r\nCode: 395. DB::Exception: boom. (FUNCTION_THROW_IF_VALUE_IS_NON_ZERO)\n",
want: true,
},
{
// A Native block can carry the literal token verbatim in result
// data; without the CRLF framing it must not be taken as an
// exception (this is the residual truncated-stream false positive).
name: "bare token in result data, no CRLF",
buf: "\x0d__exception__\x07payload more binary data",
want: false,
},
{
name: "token followed by LF only, not CRLF",
buf: "value __exception__\nnot a frame",
want: false,
},
{
name: "no marker at all",
buf: "\x01\x00\x02ordinary block bytes",
want: false,
},
{
name: "empty",
buf: "",
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isFramedException([]byte(tt.buf)); got != tt.want {
t.Errorf("isFramedException(%q) = %v, want %v", tt.buf, got, tt.want)
}
})
}
}
func TestCapturingReader(t *testing.T) {
tests := []struct {
name string
data string
readSize int
}{
{
name: "capture small data",
data: "test data",
readSize: 4,
},
{
name: "capture large data",
data: strings.Repeat("x", 1000),
readSize: 100,
},
{
name: "capture empty data",
data: "",
readSize: 10,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buf := bytes.NewBufferString(tt.data)
cr := &capturingReader{reader: buf}
// Read data in chunks
chunk := make([]byte, tt.readSize)
totalRead := 0
for {
n, err := cr.Read(chunk)
totalRead += n
if err != nil {
break
}
}
// Verify that all data was captured
captured := cr.buffer.String()
if captured != tt.data {
t.Errorf("expected captured data to be %q, got %q", tt.data, captured)
}
if totalRead != len(tt.data) {
t.Errorf("expected to read %d bytes, got %d", len(tt.data), totalRead)
}
})
}
}
func TestHTTPReadDataEOFDoesNotLogDecodeError(t *testing.T) {
var logBuf bytes.Buffer
h := &httpConnect{
logger: slog.New(slog.NewTextHandler(&logBuf, &slog.HandlerOptions{Level: slog.LevelDebug})),
}
reader := chproto.NewReader(bytes.NewReader(nil))
block, err := h.readData(reader, nil, &bytes.Buffer{})
if !errors.Is(err, io.EOF) {
t.Fatalf("expected io.EOF, got %v", err)
}
if block != nil {
t.Fatalf("expected nil block, got %#v", block)
}
if got := logBuf.String(); got != "" {
t.Fatalf("expected no error log, got %q", got)
}
}