forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 6
/
tabletconntest.go
620 lines (561 loc) · 20.7 KB
/
tabletconntest.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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
// Copyright 2015, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package tabletconntest provides the test methods to make sure a
// tabletconn/queryservice pair over RPC works correctly.
package tabletconntest
import (
"io"
"reflect"
"strings"
"testing"
"time"
"github.com/youtube/vitess/go/vt/callerid"
"github.com/youtube/vitess/go/vt/tabletserver"
"github.com/youtube/vitess/go/vt/tabletserver/tabletconn"
"github.com/youtube/vitess/go/vt/vterrors"
"golang.org/x/net/context"
topodatapb "github.com/youtube/vitess/go/vt/proto/topodata"
vtrpcpb "github.com/youtube/vitess/go/vt/proto/vtrpc"
)
// testErrorHelper will check one instance of each error type,
// to make sure we propagate the errors properly.
func testErrorHelper(t *testing.T, f *FakeQueryService, name string, ef func(context.Context) error) {
errors := []*tabletserver.TabletError{
// A few generic errors
tabletserver.NewTabletError(vtrpcpb.ErrorCode_BAD_INPUT, "generic error"),
tabletserver.NewTabletError(vtrpcpb.ErrorCode_UNKNOWN_ERROR, "uncaught panic"),
tabletserver.NewTabletError(vtrpcpb.ErrorCode_UNAUTHENTICATED, "missing caller id"),
tabletserver.NewTabletError(vtrpcpb.ErrorCode_PERMISSION_DENIED, "table acl error: nil acl"),
// Client will retry on this specific error
tabletserver.NewTabletError(vtrpcpb.ErrorCode_QUERY_NOT_SERVED, "Query disallowed due to rule: %v", "cool rule"),
// Client may retry on another server on this specific error
tabletserver.NewTabletError(vtrpcpb.ErrorCode_INTERNAL_ERROR, "Could not verify strict mode"),
// This is usually transaction pool full
tabletserver.NewTabletError(vtrpcpb.ErrorCode_RESOURCE_EXHAUSTED, "Transaction pool connection limit exceeded"),
// Transaction expired or was unknown
tabletserver.NewTabletError(vtrpcpb.ErrorCode_NOT_IN_TX, "Transaction 12"),
}
for _, e := range errors {
f.TabletError = e
ctx := context.Background()
err := ef(ctx)
if err == nil {
t.Errorf("error wasn't returned for %v?", name)
continue
}
// First we check the recoverable vtrpc code is right.
code := vterrors.RecoverVtErrorCode(err)
if code != e.ErrorCode {
t.Errorf("unexpected server code from %v: got %v, wanted %v", name, code, e.ErrorCode)
}
// Double-check we always get a ServerError, although
// we don't really care that much.
if !f.TestingGateway {
if _, ok := err.(*tabletconn.ServerError); !ok {
t.Errorf("error wasn't a tabletconn.ServerError for %v?", name)
continue
}
}
// and last we check we preserve the text, with the right prefix
if !strings.Contains(err.Error(), e.Prefix()+e.Message) {
t.Errorf("client error message '%v' for %v doesn't contain expected server text message '%v'", err.Error(), name, e.Prefix()+e.Message)
}
}
f.TabletError = nil
}
func testPanicHelper(t *testing.T, f *FakeQueryService, name string, pf func(context.Context) error) {
f.Panics = true
ctx := context.Background()
if err := pf(ctx); err == nil || !strings.Contains(err.Error(), "caught test panic") {
t.Fatalf("unexpected panic error for %v: %v", name, err)
}
f.Panics = false
}
func testBegin(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testBegin")
ctx := context.Background()
ctx = callerid.NewContext(ctx, TestCallerID, TestVTGateCallerID)
transactionID, err := conn.Begin(ctx)
if err != nil {
t.Fatalf("Begin failed: %v", err)
}
if transactionID != BeginTransactionID {
t.Errorf("Unexpected result from Begin: got %v wanted %v", transactionID, BeginTransactionID)
}
}
func testBeginError(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testBeginError")
f.HasBeginError = true
testErrorHelper(t, f, "Begin", func(ctx context.Context) error {
_, err := conn.Begin(ctx)
return err
})
f.HasBeginError = false
}
func testBeginPanics(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testBeginPanics")
testPanicHelper(t, f, "Begin", func(ctx context.Context) error {
_, err := conn.Begin(ctx)
return err
})
}
func testCommit(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testCommit")
ctx := context.Background()
ctx = callerid.NewContext(ctx, TestCallerID, TestVTGateCallerID)
err := conn.Commit(ctx, CommitTransactionID)
if err != nil {
t.Fatalf("Commit failed: %v", err)
}
}
func testCommitError(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testCommitError")
f.HasError = true
testErrorHelper(t, f, "Commit", func(ctx context.Context) error {
return conn.Commit(ctx, CommitTransactionID)
})
f.HasError = false
}
func testCommitPanics(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testCommitPanics")
testPanicHelper(t, f, "Commit", func(ctx context.Context) error {
return conn.Commit(ctx, CommitTransactionID)
})
}
func testRollback(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testRollback")
ctx := context.Background()
ctx = callerid.NewContext(ctx, TestCallerID, TestVTGateCallerID)
err := conn.Rollback(ctx, RollbackTransactionID)
if err != nil {
t.Fatalf("Rollback failed: %v", err)
}
}
func testRollbackError(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testRollbackError")
f.HasError = true
testErrorHelper(t, f, "Rollback", func(ctx context.Context) error {
return conn.Rollback(ctx, CommitTransactionID)
})
f.HasError = false
}
func testRollbackPanics(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testRollbackPanics")
testPanicHelper(t, f, "Rollback", func(ctx context.Context) error {
return conn.Rollback(ctx, RollbackTransactionID)
})
}
func testExecute(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testExecute")
f.ExpectedTransactionID = ExecuteTransactionID
ctx := context.Background()
ctx = callerid.NewContext(ctx, TestCallerID, TestVTGateCallerID)
qr, err := conn.Execute(ctx, ExecuteQuery, ExecuteBindVars, ExecuteTransactionID)
if err != nil {
t.Fatalf("Execute failed: %v", err)
}
if !reflect.DeepEqual(*qr, ExecuteQueryResult) {
t.Errorf("Unexpected result from Execute: got %v wanted %v", qr, ExecuteQueryResult)
}
}
func testExecuteError(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testExecuteError")
f.HasError = true
testErrorHelper(t, f, "Execute", func(ctx context.Context) error {
_, err := conn.Execute(ctx, ExecuteQuery, ExecuteBindVars, ExecuteTransactionID)
return err
})
f.HasError = false
}
func testExecutePanics(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testExecutePanics")
testPanicHelper(t, f, "Execute", func(ctx context.Context) error {
_, err := conn.Execute(ctx, ExecuteQuery, ExecuteBindVars, ExecuteTransactionID)
return err
})
}
func testBeginExecute(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testBeginExecute")
f.ExpectedTransactionID = BeginTransactionID
ctx := context.Background()
ctx = callerid.NewContext(ctx, TestCallerID, TestVTGateCallerID)
qr, transactionID, err := conn.BeginExecute(ctx, ExecuteQuery, ExecuteBindVars)
if err != nil {
t.Fatalf("BeginExecute failed: %v", err)
}
if transactionID != BeginTransactionID {
t.Errorf("Unexpected result from BeginExecute: got %v wanted %v", transactionID, BeginTransactionID)
}
if !reflect.DeepEqual(*qr, ExecuteQueryResult) {
t.Errorf("Unexpected result from BeginExecute: got %v wanted %v", qr, ExecuteQueryResult)
}
}
func testBeginExecuteErrorInBegin(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testBeginExecuteErrorInBegin")
f.HasBeginError = true
testErrorHelper(t, f, "BeginExecute.Begin", func(ctx context.Context) error {
_, transactionID, err := conn.BeginExecute(ctx, ExecuteQuery, ExecuteBindVars)
if transactionID != 0 {
t.Errorf("Unexpected transactionID from BeginExecute: got %v wanted 0", transactionID)
}
return err
})
f.HasBeginError = false
}
func testBeginExecuteErrorInExecute(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testBeginExecuteErrorInExecute")
f.HasError = true
testErrorHelper(t, f, "BeginExecute.Execute", func(ctx context.Context) error {
ctx = callerid.NewContext(ctx, TestCallerID, TestVTGateCallerID)
_, transactionID, err := conn.BeginExecute(ctx, ExecuteQuery, ExecuteBindVars)
if transactionID != BeginTransactionID {
t.Errorf("Unexpected transactionID from BeginExecute: got %v wanted %v", transactionID, BeginTransactionID)
}
return err
})
f.HasError = false
}
func testBeginExecutePanics(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testBeginExecutePanics")
testPanicHelper(t, f, "BeginExecute", func(ctx context.Context) error {
_, _, err := conn.BeginExecute(ctx, ExecuteQuery, ExecuteBindVars)
return err
})
}
func testStreamExecute(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testStreamExecute")
ctx := context.Background()
ctx = callerid.NewContext(ctx, TestCallerID, TestVTGateCallerID)
stream, err := conn.StreamExecute(ctx, StreamExecuteQuery, StreamExecuteBindVars)
if err != nil {
t.Fatalf("StreamExecute failed: %v", err)
}
qr, err := stream.Recv()
if err != nil {
t.Fatalf("StreamExecute failed: cannot read result1: %v", err)
}
if len(qr.Rows) == 0 {
qr.Rows = nil
}
if !reflect.DeepEqual(*qr, StreamExecuteQueryResult1) {
t.Errorf("Unexpected result1 from StreamExecute: got %v wanted %v", qr, StreamExecuteQueryResult1)
}
qr, err = stream.Recv()
if err != nil {
t.Fatalf("StreamExecute failed: cannot read result2: %v", err)
}
if len(qr.Fields) == 0 {
qr.Fields = nil
}
if !reflect.DeepEqual(*qr, StreamExecuteQueryResult2) {
t.Errorf("Unexpected result2 from StreamExecute: got %v wanted %v", qr, StreamExecuteQueryResult2)
}
qr, err = stream.Recv()
if err != io.EOF {
t.Fatalf("StreamExecute errFunc failed: %v", err)
}
}
func testStreamExecuteError(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testStreamExecuteError")
f.HasError = true
testErrorHelper(t, f, "StreamExecute", func(ctx context.Context) error {
f.ErrorWait = make(chan struct{})
ctx = callerid.NewContext(ctx, TestCallerID, TestVTGateCallerID)
stream, err := conn.StreamExecute(ctx, StreamExecuteQuery, StreamExecuteBindVars)
if err != nil {
t.Fatalf("StreamExecute failed: %v", err)
}
qr, err := stream.Recv()
if err != nil {
t.Fatalf("StreamExecute failed: cannot read result1: %v", err)
}
if len(qr.Rows) == 0 {
qr.Rows = nil
}
if !reflect.DeepEqual(*qr, StreamExecuteQueryResult1) {
t.Errorf("Unexpected result1 from StreamExecute: got %v wanted %v", qr, StreamExecuteQueryResult1)
}
// signal to the server that the first result has been received
close(f.ErrorWait)
// After 1 result, we expect to get an error (no more results).
qr, err = stream.Recv()
if err == nil {
t.Fatalf("StreamExecute channel wasn't closed")
}
return err
})
f.HasError = false
}
func testStreamExecutePanics(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testStreamExecutePanics")
// early panic is before sending the Fields, that is returned
// by the StreamExecute call itself, or as the first error
// by ErrFunc
f.StreamExecutePanicsEarly = true
testPanicHelper(t, f, "StreamExecute.Early", func(ctx context.Context) error {
ctx = callerid.NewContext(ctx, TestCallerID, TestVTGateCallerID)
stream, err := conn.StreamExecute(ctx, StreamExecuteQuery, StreamExecuteBindVars)
if err != nil {
return err
}
_, err = stream.Recv()
return err
})
// late panic is after sending Fields
f.StreamExecutePanicsEarly = false
testPanicHelper(t, f, "StreamExecute.Late", func(ctx context.Context) error {
f.PanicWait = make(chan struct{})
ctx = callerid.NewContext(ctx, TestCallerID, TestVTGateCallerID)
stream, err := conn.StreamExecute(ctx, StreamExecuteQuery, StreamExecuteBindVars)
if err != nil {
t.Fatalf("StreamExecute failed: %v", err)
}
qr, err := stream.Recv()
if err != nil {
t.Fatalf("StreamExecute failed: cannot read result1: %v", err)
}
if len(qr.Rows) == 0 {
qr.Rows = nil
}
if !reflect.DeepEqual(*qr, StreamExecuteQueryResult1) {
t.Errorf("Unexpected result1 from StreamExecute: got %v wanted %v", qr, StreamExecuteQueryResult1)
}
close(f.PanicWait)
_, err = stream.Recv()
return err
})
}
func testExecuteBatch(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testExecuteBatch")
f.ExpectedTransactionID = ExecuteBatchTransactionID
ctx := context.Background()
ctx = callerid.NewContext(ctx, TestCallerID, TestVTGateCallerID)
qrl, err := conn.ExecuteBatch(ctx, ExecuteBatchQueries, TestAsTransaction, ExecuteBatchTransactionID)
if err != nil {
t.Fatalf("ExecuteBatch failed: %v", err)
}
if !reflect.DeepEqual(qrl, ExecuteBatchQueryResultList) {
t.Errorf("Unexpected result from ExecuteBatch: got %v wanted %v", qrl, ExecuteBatchQueryResultList)
}
}
func testExecuteBatchError(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testExecuteBatchError")
f.HasError = true
testErrorHelper(t, f, "ExecuteBatch", func(ctx context.Context) error {
_, err := conn.ExecuteBatch(ctx, ExecuteBatchQueries, TestAsTransaction, ExecuteBatchTransactionID)
return err
})
f.HasError = true
}
func testExecuteBatchPanics(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testExecuteBatchPanics")
testPanicHelper(t, f, "ExecuteBatch", func(ctx context.Context) error {
_, err := conn.ExecuteBatch(ctx, ExecuteBatchQueries, TestAsTransaction, ExecuteBatchTransactionID)
return err
})
}
func testBeginExecuteBatch(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testBeginExecuteBatch")
f.ExpectedTransactionID = BeginTransactionID
ctx := context.Background()
ctx = callerid.NewContext(ctx, TestCallerID, TestVTGateCallerID)
qrl, transactionID, err := conn.BeginExecuteBatch(ctx, ExecuteBatchQueries, true)
if err != nil {
t.Fatalf("BeginExecuteBatch failed: %v", err)
}
if transactionID != BeginTransactionID {
t.Errorf("Unexpected result from BeginExecuteBatch: got %v wanted %v", transactionID, BeginTransactionID)
}
if !reflect.DeepEqual(qrl, ExecuteBatchQueryResultList) {
t.Errorf("Unexpected result from ExecuteBatch: got %v wanted %v", qrl, ExecuteBatchQueryResultList)
}
}
func testBeginExecuteBatchErrorInBegin(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testBeginExecuteBatchErrorInBegin")
f.HasBeginError = true
testErrorHelper(t, f, "BeginExecuteBatch.Begin", func(ctx context.Context) error {
_, transactionID, err := conn.BeginExecuteBatch(ctx, ExecuteBatchQueries, true)
if transactionID != 0 {
t.Errorf("Unexpected transactionID from BeginExecuteBatch: got %v wanted 0", transactionID)
}
return err
})
f.HasBeginError = false
}
func testBeginExecuteBatchErrorInExecuteBatch(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testBeginExecuteBatchErrorInExecuteBatch")
f.HasError = true
testErrorHelper(t, f, "BeginExecute.ExecuteBatch", func(ctx context.Context) error {
ctx = callerid.NewContext(ctx, TestCallerID, TestVTGateCallerID)
_, transactionID, err := conn.BeginExecuteBatch(ctx, ExecuteBatchQueries, true)
if transactionID != BeginTransactionID {
t.Errorf("Unexpected transactionID from BeginExecuteBatch: got %v wanted %v", transactionID, BeginTransactionID)
}
return err
})
f.HasError = false
}
func testBeginExecuteBatchPanics(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testBeginExecuteBatchPanics")
testPanicHelper(t, f, "BeginExecuteBatch", func(ctx context.Context) error {
_, _, err := conn.BeginExecuteBatch(ctx, ExecuteBatchQueries, true)
return err
})
}
func testSplitQuery(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testSplitQuery")
ctx := context.Background()
ctx = callerid.NewContext(ctx, TestCallerID, TestVTGateCallerID)
qsl, err := conn.SplitQuery(ctx, SplitQueryBoundQuery, SplitQuerySplitColumn, SplitQuerySplitCount)
if err != nil {
t.Fatalf("SplitQuery failed: %v", err)
}
if !reflect.DeepEqual(qsl, SplitQueryQuerySplitList) {
t.Errorf("Unexpected result from SplitQuery: got %v wanted %v", qsl, SplitQueryQuerySplitList)
}
}
// TODO(erez): Rename to SplitQuery after migration to SplitQuery V2 is done.
func testSplitQueryV2(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testSplitQueryV2")
ctx := context.Background()
ctx = callerid.NewContext(ctx, TestCallerID, TestVTGateCallerID)
qsl, err := conn.SplitQueryV2(
ctx,
SplitQueryV2BoundQuery,
SplitQueryV2SplitColumns,
SplitQueryV2SplitCount,
SplitQueryV2NumRowsPerQueryPart,
SplitQueryV2Algorithm,
)
if err != nil {
t.Fatalf("SplitQuery failed: %v", err)
}
if !reflect.DeepEqual(qsl, SplitQueryQueryV2SplitList) {
t.Errorf("Unexpected result from SplitQuery: got %v wanted %v", qsl, SplitQueryQuerySplitList)
}
}
func testSplitQueryError(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testSplitQueryError")
f.HasError = true
testErrorHelper(t, f, "SplitQuery", func(ctx context.Context) error {
_, err := conn.SplitQuery(ctx, SplitQueryBoundQuery, SplitQuerySplitColumn, SplitQuerySplitCount)
return err
})
f.HasError = false
}
func testSplitQueryPanics(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testSplitQueryPanics")
testPanicHelper(t, f, "SplitQuery", func(ctx context.Context) error {
_, err := conn.SplitQuery(ctx, SplitQueryBoundQuery, SplitQuerySplitColumn, SplitQuerySplitCount)
return err
})
}
// this test is a bit of a hack: we write something on the channel
// upon registration, and we also return an error, so the streaming query
// ends right there. Otherwise we have no real way to trigger a real
// communication error, that ends the streaming.
func testStreamHealth(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testStreamHealth")
ctx := context.Background()
stream, err := conn.StreamHealth(ctx)
if err != nil {
t.Fatalf("StreamHealth failed: %v", err)
}
// channel should have one response, then closed
shr, err := stream.Recv()
if err != nil {
t.Fatalf("StreamHealth got no response")
}
if !reflect.DeepEqual(*shr, *TestStreamHealthStreamHealthResponse) {
t.Errorf("invalid StreamHealthResponse: got %v expected %v", *shr, *TestStreamHealthStreamHealthResponse)
}
}
func testStreamHealthError(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testStreamHealthError")
f.HasError = true
ctx := context.Background()
stream, err := conn.StreamHealth(ctx)
if err != nil {
t.Fatalf("StreamHealth failed: %v", err)
}
_, err = stream.Recv()
if err == nil || !strings.Contains(err.Error(), TestStreamHealthErrorMsg) {
t.Fatalf("StreamHealth failed with the wrong error: %v", err)
}
f.HasError = false
}
func testStreamHealthPanics(t *testing.T, conn tabletconn.TabletConn, f *FakeQueryService) {
t.Log("testStreamHealthPanics")
testPanicHelper(t, f, "StreamHealth", func(ctx context.Context) error {
stream, err := conn.StreamHealth(ctx)
if err != nil {
t.Fatalf("StreamHealth failed: %v", err)
}
_, err = stream.Recv()
return err
})
}
// TestSuite runs all the tests.
// If fake.TestingGateway is set, we only test the calls that can go through
// a gateway.
func TestSuite(t *testing.T, protocol string, tablet *topodatapb.Tablet, fake *FakeQueryService) {
tests := []func(*testing.T, tabletconn.TabletConn, *FakeQueryService){
// positive test cases
testBegin,
testCommit,
testRollback,
testExecute,
testBeginExecute,
testStreamExecute,
testExecuteBatch,
testBeginExecuteBatch,
testSplitQuery,
// error test cases
testBeginError,
testCommitError,
testRollbackError,
testExecuteError,
testBeginExecuteErrorInBegin,
testBeginExecuteErrorInExecute,
testStreamExecuteError,
testExecuteBatchError,
testBeginExecuteBatchErrorInBegin,
testBeginExecuteBatchErrorInExecuteBatch,
testSplitQueryError,
// panic test cases
testBeginPanics,
testCommitPanics,
testRollbackPanics,
testExecutePanics,
testBeginExecutePanics,
testStreamExecutePanics,
testExecuteBatchPanics,
testBeginExecuteBatchPanics,
testSplitQueryPanics,
}
if !fake.TestingGateway {
tests = append(tests, []func(*testing.T, tabletconn.TabletConn, *FakeQueryService){
// positive test cases
testStreamHealth,
// error test cases
testStreamHealthError,
// panic test cases
testStreamHealthPanics,
}...)
}
// make sure we use the right client
*tabletconn.TabletProtocol = protocol
// create a connection
ctx := context.Background()
conn, err := tabletconn.GetDialer()(ctx, tablet, 30*time.Second)
if err != nil {
t.Fatalf("dial failed: %v", err)
}
// run the tests
for _, c := range tests {
c(t, conn, fake)
}
// and we're done
conn.Close()
}