/
tabletconntest.go
886 lines (796 loc) · 27.4 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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
// 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 (
"fmt"
"reflect"
"strings"
"testing"
"time"
"github.com/youtube/vitess/go/sqltypes"
"github.com/youtube/vitess/go/vt/callerid"
"github.com/youtube/vitess/go/vt/tabletserver"
"github.com/youtube/vitess/go/vt/tabletserver/querytypes"
"github.com/youtube/vitess/go/vt/tabletserver/tabletconn"
"github.com/youtube/vitess/go/vt/vterrors"
"golang.org/x/net/context"
querypb "github.com/youtube/vitess/go/vt/proto/query"
topodatapb "github.com/youtube/vitess/go/vt/proto/topodata"
vtrpcpb "github.com/youtube/vitess/go/vt/proto/vtrpc"
)
// FakeQueryService has the server side of this fake
type FakeQueryService struct {
t *testing.T
hasError bool
panics bool
streamExecutePanicsEarly bool
panicWait chan struct{}
errorWait chan struct{}
// if set, we check target, if not set we check sessionId
checkTarget bool
}
// HandlePanic is part of the queryservice.QueryService interface
func (f *FakeQueryService) HandlePanic(err *error) {
if x := recover(); x != nil {
*err = fmt.Errorf("caught test panic: %v", x)
}
}
const expectedErrMatch string = "error: generic error"
const expectedCode vtrpcpb.ErrorCode = vtrpcpb.ErrorCode_BAD_INPUT
var testTabletError = tabletserver.NewTabletError(tabletserver.ErrFail, expectedCode, "generic error")
// Verifies the returned error has the properties that we expect.
func verifyError(t *testing.T, err error, method string) {
if err == nil {
t.Errorf("%s was expecting an error, didn't get one", method)
return
}
code := vterrors.RecoverVtErrorCode(err)
if code != expectedCode {
t.Errorf("Unexpected server code from %s: got %v, wanted %v", method, code, expectedCode)
}
verifyErrorExceptServerCode(t, err, method)
}
func verifyErrorExceptServerCode(t *testing.T, err error, method string) {
if err == nil {
t.Errorf("%s was expecting an error, didn't get one", method)
return
}
if se, ok := err.(*tabletconn.ServerError); ok {
if se.Code != tabletconn.ERR_NORMAL {
t.Errorf("Unexpected error code from %s: got %v, wanted %v", method, se.Code, tabletconn.ERR_NORMAL)
}
} else {
t.Errorf("Unexpected error type from %s: got %v, wanted *tabletconn.ServerError", method, reflect.TypeOf(err))
}
if !strings.Contains(err.Error(), expectedErrMatch) {
t.Errorf("Unexpected error from %s: got %v, wanted err containing %v", method, err, expectedErrMatch)
}
}
// testTarget is the target we use for this test
var testTarget = &querypb.Target{
Keyspace: "test_keyspace",
Shard: "test_shard",
TabletType: topodatapb.TabletType_REPLICA,
}
var testCallerID = &vtrpcpb.CallerID{
Principal: "test_principal",
Component: "test_component",
Subcomponent: "test_subcomponent",
}
var testVTGateCallerID = &querypb.VTGateCallerID{
Username: "test_username",
}
const testAsTransaction bool = true
const testSessionID int64 = 5678
func (f *FakeQueryService) checkSessionTargetCallerID(ctx context.Context, name string, target *querypb.Target, sessionID int64) {
if f.checkTarget {
if !reflect.DeepEqual(target, testTarget) {
f.t.Errorf("invalid Target for %v: got %#v expected %#v", name, target, testTarget)
}
} else {
if sessionID != testSessionID {
f.t.Errorf("invalid sessionID for %v: got %v expected %v", name, sessionID, testSessionID)
}
}
ef := callerid.EffectiveCallerIDFromContext(ctx)
if ef == nil {
f.t.Errorf("no effective caller id for %v", name)
} else {
if !reflect.DeepEqual(ef, testCallerID) {
f.t.Errorf("invalid effective caller id for %v: got %v expected %v", name, ef, testCallerID)
}
}
im := callerid.ImmediateCallerIDFromContext(ctx)
if im == nil {
f.t.Errorf("no immediate caller id for %v", name)
} else {
if !reflect.DeepEqual(im, testVTGateCallerID) {
f.t.Errorf("invalid immediate caller id for %v: got %v expected %v", name, im, testVTGateCallerID)
}
}
}
// GetSessionId is part of the queryservice.QueryService interface
func (f *FakeQueryService) GetSessionId(keyspace, shard string) (int64, error) {
if keyspace != testTarget.Keyspace {
f.t.Errorf("invalid keyspace: got %v expected %v", keyspace, testTarget.Keyspace)
}
if shard != testTarget.Shard {
f.t.Errorf("invalid shard: got %v expected %v", shard, testTarget.Shard)
}
return testSessionID, nil
}
// Begin is part of the queryservice.QueryService interface
func (f *FakeQueryService) Begin(ctx context.Context, target *querypb.Target, sessionID int64) (int64, error) {
if f.hasError {
return 0, testTabletError
}
if f.panics {
panic(fmt.Errorf("test-triggered panic"))
}
f.checkSessionTargetCallerID(ctx, "Begin", target, sessionID)
return beginTransactionID, nil
}
const beginTransactionID int64 = 9990
func testBegin(t *testing.T, conn tabletconn.TabletConn) {
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) {
ctx := context.Background()
_, err := conn.Begin(ctx)
verifyError(t, err, "Begin")
}
func testBeginPanics(t *testing.T, conn tabletconn.TabletConn) {
ctx := context.Background()
if _, err := conn.Begin(ctx); err == nil || !strings.Contains(err.Error(), "caught test panic") {
t.Fatalf("unexpected panic error: %v", err)
}
}
// Commit is part of the queryservice.QueryService interface
func (f *FakeQueryService) Commit(ctx context.Context, target *querypb.Target, sessionID, transactionID int64) error {
if f.hasError {
return testTabletError
}
if f.panics {
panic(fmt.Errorf("test-triggered panic"))
}
f.checkSessionTargetCallerID(ctx, "Commit", target, sessionID)
if transactionID != commitTransactionID {
f.t.Errorf("Commit: invalid TransactionId: got %v expected %v", transactionID, commitTransactionID)
}
return nil
}
const commitTransactionID int64 = 999044
func testCommit(t *testing.T, conn tabletconn.TabletConn) {
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) {
ctx := context.Background()
err := conn.Commit(ctx, commitTransactionID)
verifyError(t, err, "Commit")
}
func testCommitPanics(t *testing.T, conn tabletconn.TabletConn) {
ctx := context.Background()
if err := conn.Commit(ctx, commitTransactionID); err == nil || !strings.Contains(err.Error(), "caught test panic") {
t.Fatalf("unexpected panic error: %v", err)
}
}
// Rollback is part of the queryservice.QueryService interface
func (f *FakeQueryService) Rollback(ctx context.Context, target *querypb.Target, sessionID, transactionID int64) error {
if f.hasError {
return testTabletError
}
if f.panics {
panic(fmt.Errorf("test-triggered panic"))
}
f.checkSessionTargetCallerID(ctx, "Rollback", target, sessionID)
if transactionID != rollbackTransactionID {
f.t.Errorf("Rollback: invalid TransactionId: got %v expected %v", transactionID, rollbackTransactionID)
}
return nil
}
const rollbackTransactionID int64 = 999044
func testRollback(t *testing.T, conn tabletconn.TabletConn) {
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) {
ctx := context.Background()
err := conn.Rollback(ctx, commitTransactionID)
verifyError(t, err, "Rollback")
}
func testRollbackPanics(t *testing.T, conn tabletconn.TabletConn) {
ctx := context.Background()
if err := conn.Rollback(ctx, rollbackTransactionID); err == nil || !strings.Contains(err.Error(), "caught test panic") {
t.Fatalf("unexpected panic error: %v", err)
}
}
// Execute is part of the queryservice.QueryService interface
func (f *FakeQueryService) Execute(ctx context.Context, target *querypb.Target, sql string, bindVariables map[string]interface{}, sessionID, transactionID int64) (*sqltypes.Result, error) {
if f.hasError {
return nil, testTabletError
}
if f.panics {
panic(fmt.Errorf("test-triggered panic"))
}
if sql != executeQuery {
f.t.Errorf("invalid Execute.Query.Sql: got %v expected %v", sql, executeQuery)
}
if !reflect.DeepEqual(bindVariables, executeBindVars) {
f.t.Errorf("invalid Execute.BindVariables: got %v expected %v", bindVariables, executeBindVars)
}
f.checkSessionTargetCallerID(ctx, "Execute", target, sessionID)
if transactionID != executeTransactionID {
f.t.Errorf("invalid Execute.TransactionId: got %v expected %v", transactionID, executeTransactionID)
}
return &executeQueryResult, nil
}
const executeQuery = "executeQuery"
var executeBindVars = map[string]interface{}{
"bind1": int64(1114444),
}
const executeTransactionID int64 = 678
var executeQueryResult = sqltypes.Result{
Fields: []*querypb.Field{
{
Name: "field1",
Type: sqltypes.Int8,
},
{
Name: "field2",
Type: sqltypes.Char,
},
},
RowsAffected: 123,
InsertID: 72,
Rows: [][]sqltypes.Value{
{
sqltypes.MakeTrusted(sqltypes.Int8, []byte("1")),
sqltypes.NULL,
},
{
sqltypes.MakeTrusted(sqltypes.Int8, []byte("2")),
sqltypes.MakeTrusted(sqltypes.Char, []byte("row2 value2")),
},
},
}
func testExecute(t *testing.T, conn tabletconn.TabletConn) {
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) {
ctx := context.Background()
_, err := conn.Execute(ctx, executeQuery, executeBindVars, executeTransactionID)
verifyError(t, err, "Execute")
}
func testExecutePanics(t *testing.T, conn tabletconn.TabletConn) {
ctx := context.Background()
if _, err := conn.Execute(ctx, executeQuery, executeBindVars, executeTransactionID); err == nil || !strings.Contains(err.Error(), "caught test panic") {
t.Fatalf("unexpected panic error: %v", err)
}
}
// StreamExecute is part of the queryservice.QueryService interface
func (f *FakeQueryService) StreamExecute(ctx context.Context, target *querypb.Target, sql string, bindVariables map[string]interface{}, sessionID int64, sendReply func(*sqltypes.Result) error) error {
if f.panics && f.streamExecutePanicsEarly {
panic(fmt.Errorf("test-triggered panic early"))
}
if sql != streamExecuteQuery {
f.t.Errorf("invalid StreamExecute.Sql: got %v expected %v", sql, streamExecuteQuery)
}
if !reflect.DeepEqual(bindVariables, streamExecuteBindVars) {
f.t.Errorf("invalid StreamExecute.BindVariables: got %v expected %v", bindVariables, streamExecuteBindVars)
}
f.checkSessionTargetCallerID(ctx, "StreamExecute", target, sessionID)
if err := sendReply(&streamExecuteQueryResult1); err != nil {
f.t.Errorf("sendReply1 failed: %v", err)
}
if f.panics && !f.streamExecutePanicsEarly {
// wait until the client gets the response, then panics
<-f.panicWait
f.panicWait = make(chan struct{}) // for next test
panic(fmt.Errorf("test-triggered panic late"))
}
if f.hasError {
// wait until the client has the response, since all streaming implementation may not
// send previous messages if an error has been triggered.
<-f.errorWait
f.errorWait = make(chan struct{}) // for next test
return testTabletError
}
if err := sendReply(&streamExecuteQueryResult2); err != nil {
f.t.Errorf("sendReply2 failed: %v", err)
}
return nil
}
const streamExecuteQuery = "streamExecuteQuery"
var streamExecuteBindVars = map[string]interface{}{
"bind1": int64(93848000),
}
const streamExecuteTransactionID int64 = 6789992
var streamExecuteQueryResult1 = sqltypes.Result{
Fields: []*querypb.Field{
{
Name: "field1",
Type: sqltypes.Int8,
},
{
Name: "field2",
Type: sqltypes.Char,
},
},
}
var streamExecuteQueryResult2 = sqltypes.Result{
Rows: [][]sqltypes.Value{
{
sqltypes.MakeTrusted(sqltypes.Int8, []byte("1")),
sqltypes.MakeTrusted(sqltypes.Char, []byte("row1 value2")),
},
{
sqltypes.MakeTrusted(sqltypes.Int8, []byte("2")),
sqltypes.MakeTrusted(sqltypes.Char, []byte("row2 value2")),
},
},
}
func testStreamExecute(t *testing.T, conn tabletconn.TabletConn) {
ctx := context.Background()
ctx = callerid.NewContext(ctx, testCallerID, testVTGateCallerID)
stream, errFunc, err := conn.StreamExecute(ctx, streamExecuteQuery, streamExecuteBindVars, streamExecuteTransactionID)
if err != nil {
t.Fatalf("StreamExecute failed: %v", err)
}
qr, ok := <-stream
if !ok {
t.Fatalf("StreamExecute failed: cannot read result1")
}
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, ok = <-stream
if !ok {
t.Fatalf("StreamExecute failed: cannot read result2")
}
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, ok = <-stream
if ok {
t.Fatalf("StreamExecute channel wasn't closed")
}
if err := errFunc(); err != nil {
t.Fatalf("StreamExecute errFunc failed: %v", err)
}
}
func testStreamExecuteError(t *testing.T, conn tabletconn.TabletConn, fake *FakeQueryService) {
ctx := context.Background()
ctx = callerid.NewContext(ctx, testCallerID, testVTGateCallerID)
stream, errFunc, err := conn.StreamExecute(ctx, streamExecuteQuery, streamExecuteBindVars, streamExecuteTransactionID)
if err != nil {
t.Fatalf("StreamExecute failed: %v", err)
}
qr, ok := <-stream
if !ok {
t.Fatalf("StreamExecute failed: cannot read result1")
}
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(fake.errorWait)
// After 1 result, we expect to get an error (no more results).
qr, ok = <-stream
if ok {
t.Fatalf("StreamExecute channel wasn't closed")
}
err = errFunc()
verifyError(t, err, "StreamExecute")
}
func testStreamExecutePanics(t *testing.T, conn tabletconn.TabletConn, fake *FakeQueryService) {
// early panic is before sending the Fields, that is returned
// by the StreamExecute call itself, or as the first error
// by ErrFunc
ctx := context.Background()
ctx = callerid.NewContext(ctx, testCallerID, testVTGateCallerID)
fake.streamExecutePanicsEarly = true
stream, errFunc, err := conn.StreamExecute(ctx, streamExecuteQuery, streamExecuteBindVars, streamExecuteTransactionID)
if err != nil {
if !strings.Contains(err.Error(), "caught test panic") {
t.Fatalf("unexpected panic error: %v", err)
}
} else {
_, ok := <-stream
if ok {
t.Fatalf("StreamExecute early panic should not return anything")
}
err = errFunc()
if err == nil || !strings.Contains(err.Error(), "caught test panic") {
t.Fatalf("unexpected panic error: %v", err)
}
}
// late panic is after sending Fields
fake.streamExecutePanicsEarly = false
stream, errFunc, err = conn.StreamExecute(ctx, streamExecuteQuery, streamExecuteBindVars, streamExecuteTransactionID)
if err != nil {
t.Fatalf("StreamExecute failed: %v", err)
}
qr, ok := <-stream
if !ok {
t.Fatalf("StreamExecute failed: cannot read result1")
}
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(fake.panicWait)
if _, ok := <-stream; ok {
t.Fatalf("StreamExecute returned more results")
}
if err := errFunc(); err == nil || !strings.Contains(err.Error(), "caught test panic") {
t.Fatalf("unexpected panic error: %v", err)
}
}
// ExecuteBatch is part of the queryservice.QueryService interface
func (f *FakeQueryService) ExecuteBatch(ctx context.Context, target *querypb.Target, queries []querytypes.BoundQuery, sessionID int64, asTransaction bool, transactionID int64) ([]sqltypes.Result, error) {
if f.hasError {
return nil, testTabletError
}
if f.panics {
panic(fmt.Errorf("test-triggered panic"))
}
if !reflect.DeepEqual(queries, executeBatchQueries) {
f.t.Errorf("invalid ExecuteBatch.Queries: got %v expected %v", queries, executeBatchQueries)
}
f.checkSessionTargetCallerID(ctx, "ExecuteBatch", target, sessionID)
if asTransaction != testAsTransaction {
f.t.Errorf("invalid ExecuteBatch.AsTransaction: got %v expected %v", asTransaction, testAsTransaction)
}
if transactionID != executeBatchTransactionID {
f.t.Errorf("invalid ExecuteBatch.TransactionId: got %v expected %v", transactionID, executeBatchTransactionID)
}
return executeBatchQueryResultList, nil
}
var executeBatchQueries = []querytypes.BoundQuery{
{
Sql: "executeBatchQueries1",
BindVariables: map[string]interface{}{
"bind1": int64(43),
},
},
{
Sql: "executeBatchQueries2",
BindVariables: map[string]interface{}{
"bind2": int64(72),
},
},
}
const executeBatchTransactionID int64 = 678
var executeBatchQueryResultList = []sqltypes.Result{
{
Fields: []*querypb.Field{
{
Name: "field1",
Type: sqltypes.Int8,
},
},
RowsAffected: 1232,
InsertID: 712,
Rows: [][]sqltypes.Value{
{
sqltypes.MakeTrusted(sqltypes.Int8, []byte("1")),
},
{
sqltypes.MakeTrusted(sqltypes.Int8, []byte("2")),
},
},
},
{
Fields: []*querypb.Field{
{
Name: "field1",
Type: sqltypes.VarBinary,
},
},
RowsAffected: 12333,
InsertID: 74442,
Rows: [][]sqltypes.Value{
{
sqltypes.MakeString([]byte("row1 value1")),
},
{
sqltypes.MakeString([]byte("row1 value2")),
},
},
},
}
func testExecuteBatch(t *testing.T, conn tabletconn.TabletConn) {
ctx := context.Background()
ctx = callerid.NewContext(ctx, testCallerID, testVTGateCallerID)
qrl, err := conn.ExecuteBatch(ctx, executeBatchQueries, true, 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) {
ctx := context.Background()
_, err := conn.ExecuteBatch(ctx, executeBatchQueries, true, executeBatchTransactionID)
verifyError(t, err, "ExecuteBatch")
}
func testExecuteBatchPanics(t *testing.T, conn tabletconn.TabletConn) {
ctx := context.Background()
if _, err := conn.ExecuteBatch(ctx, executeBatchQueries, true, executeBatchTransactionID); err == nil || !strings.Contains(err.Error(), "caught test panic") {
t.Fatalf("unexpected panic error: %v", err)
}
}
// SplitQuery is part of the queryservice.QueryService interface
func (f *FakeQueryService) SplitQuery(ctx context.Context, target *querypb.Target, sql string, bindVariables map[string]interface{}, splitColumn string, splitCount int64, sessionID int64) ([]querytypes.QuerySplit, error) {
if f.hasError {
return nil, testTabletError
}
if f.panics {
panic(fmt.Errorf("test-triggered panic"))
}
f.checkSessionTargetCallerID(ctx, "SplitQuery", target, sessionID)
if !reflect.DeepEqual(querytypes.BoundQuery{
Sql: sql,
BindVariables: bindVariables,
}, splitQueryBoundQuery) {
f.t.Errorf("invalid SplitQuery.SplitQueryRequest.Query: got %v expected %v", querytypes.QueryAsString(sql, bindVariables), splitQueryBoundQuery)
}
if splitColumn != splitQuerySplitColumn {
f.t.Errorf("invalid SplitQuery.SplitColumn: got %v expected %v", splitColumn, splitQuerySplitColumn)
}
if splitCount != splitQuerySplitCount {
f.t.Errorf("invalid SplitQuery.SplitCount: got %v expected %v", splitCount, splitQuerySplitCount)
}
return splitQueryQuerySplitList, nil
}
var splitQueryBoundQuery = querytypes.BoundQuery{
Sql: "splitQuery",
BindVariables: map[string]interface{}{
"bind1": int64(43),
},
}
const splitQuerySplitColumn = "nice_column_to_split"
const splitQuerySplitCount = 372
var splitQueryQuerySplitList = []querytypes.QuerySplit{
{
Sql: "splitQuery",
BindVariables: map[string]interface{}{
"bind1": int64(43),
"keyspace_id": int64(3333),
},
RowCount: 4456,
},
}
func testSplitQuery(t *testing.T, conn tabletconn.TabletConn) {
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)
}
}
func testSplitQueryError(t *testing.T, conn tabletconn.TabletConn) {
ctx := context.Background()
_, err := conn.SplitQuery(ctx, splitQueryBoundQuery, splitQuerySplitColumn, splitQuerySplitCount)
verifyError(t, err, "SplitQuery")
}
func testSplitQueryPanics(t *testing.T, conn tabletconn.TabletConn) {
ctx := context.Background()
if _, err := conn.SplitQuery(ctx, splitQueryBoundQuery, splitQuerySplitColumn, splitQuerySplitCount); err == nil || !strings.Contains(err.Error(), "caught test panic") {
t.Fatalf("unexpected panic error: %v", 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.
var testStreamHealthStreamHealthResponse = &querypb.StreamHealthResponse{
Target: &querypb.Target{
Keyspace: "test_keyspace",
Shard: "test_shard",
TabletType: topodatapb.TabletType_RDONLY,
},
Serving: true,
TabletExternallyReparentedTimestamp: 1234589,
RealtimeStats: &querypb.RealtimeStats{
HealthError: "random error",
SecondsBehindMaster: 234,
BinlogPlayersCount: 1,
SecondsBehindMasterFilteredReplication: 2,
CpuUsage: 1.0,
},
}
var testStreamHealthError = "to trigger a server error"
// The server side should write the response to the stream, then wait for
// this channel to close, then return the error
var streamHealthSynchronization chan struct{}
// StreamHealthRegister is part of the queryservice.QueryService interface
func (f *FakeQueryService) StreamHealthRegister(c chan<- *querypb.StreamHealthResponse) (int, error) {
if f.panics {
panic(fmt.Errorf("test-triggered panic"))
}
c <- testStreamHealthStreamHealthResponse
<-streamHealthSynchronization
return 0, fmt.Errorf(testStreamHealthError)
}
// StreamHealthUnregister is part of the queryservice.QueryService interface
func (f *FakeQueryService) StreamHealthUnregister(int) error {
return nil
}
func testStreamHealth(t *testing.T, conn tabletconn.TabletConn) {
streamHealthSynchronization = make(chan struct{})
ctx := context.Background()
c, errFunc, err := conn.StreamHealth(ctx)
if err != nil {
t.Fatalf("StreamHealth failed: %v", err)
}
// channel should have one response, then closed
shr, ok := <-c
if !ok {
t.Fatalf("StreamHealth got no response")
}
if !reflect.DeepEqual(*shr, *testStreamHealthStreamHealthResponse) {
t.Errorf("invalid StreamHealthResponse: got %v expected %v", *shr, *testStreamHealthStreamHealthResponse)
}
// close streamHealthSynchronization so server side knows we
// got the response, and it can send the error
close(streamHealthSynchronization)
_, ok = <-c
if ok {
t.Fatalf("StreamHealth wasn't closed")
}
err = errFunc()
if !strings.Contains(err.Error(), testStreamHealthError) {
t.Fatalf("StreamHealth failed with the wrong error: %v", err)
}
}
func testStreamHealthPanics(t *testing.T, conn tabletconn.TabletConn) {
ctx := context.Background()
c, errFunc, err := conn.StreamHealth(ctx)
if err != nil {
t.Fatalf("StreamHealth failed: %v", err)
}
// channel should have no response, just closed
_, ok := <-c
if ok {
t.Fatalf("StreamHealth wasn't closed")
}
err = errFunc()
if err == nil || !strings.Contains(err.Error(), "caught test panic") {
t.Fatalf("unexpected panic error: %v", err)
}
}
// CreateFakeServer returns the fake server for the tests
func CreateFakeServer(t *testing.T) *FakeQueryService {
return &FakeQueryService{
t: t,
panics: false,
streamExecutePanicsEarly: false,
panicWait: make(chan struct{}),
errorWait: make(chan struct{}),
}
}
// TestSuite runs all the tests
func TestSuite(t *testing.T, protocol string, endPoint *topodatapb.EndPoint, fake *FakeQueryService) {
// make sure we use the right client
*tabletconn.TabletProtocol = protocol
// create a connection, using sessionId
ctx := context.Background()
conn, err := tabletconn.GetDialer()(ctx, endPoint, testTarget.Keyspace, testTarget.Shard, topodatapb.TabletType_UNKNOWN, 30*time.Second)
if err != nil {
t.Fatalf("dial failed: %v", err)
}
fake.checkTarget = false
// run the tests
testBegin(t, conn)
testCommit(t, conn)
testRollback(t, conn)
testExecute(t, conn)
testStreamExecute(t, conn)
testExecuteBatch(t, conn)
testSplitQuery(t, conn)
testStreamHealth(t, conn)
// fake should return an error, make sure errors are handled properly
fake.hasError = true
testBeginError(t, conn)
testCommitError(t, conn)
testRollbackError(t, conn)
testExecuteError(t, conn)
testStreamExecuteError(t, conn, fake)
testExecuteBatchError(t, conn)
testSplitQueryError(t, conn)
fake.hasError = false
// force panics, make sure they're caught
fake.panics = true
testBeginPanics(t, conn)
testCommitPanics(t, conn)
testRollbackPanics(t, conn)
testExecutePanics(t, conn)
testStreamExecutePanics(t, conn, fake)
testExecuteBatchPanics(t, conn)
testSplitQueryPanics(t, conn)
testStreamHealthPanics(t, conn)
fake.panics = false
// create a new connection that expects the target
conn.Close()
conn, err = tabletconn.GetDialer()(ctx, endPoint, testTarget.Keyspace, testTarget.Shard, topodatapb.TabletType_REPLICA, 30*time.Second)
if err != nil {
t.Fatalf("dial failed: %v", err)
}
fake.checkTarget = true
// run the tests
testBegin(t, conn)
testCommit(t, conn)
testRollback(t, conn)
testExecute(t, conn)
testStreamExecute(t, conn)
testExecuteBatch(t, conn)
testSplitQuery(t, conn)
testStreamHealth(t, conn)
// fake should return an error, make sure errors are handled properly
fake.hasError = true
testBeginError(t, conn)
testCommitError(t, conn)
testRollbackError(t, conn)
testExecuteError(t, conn)
testStreamExecuteError(t, conn, fake)
testExecuteBatchError(t, conn)
testSplitQueryError(t, conn)
fake.hasError = false
// force panics, make sure they're caught
fake.panics = true
testBeginPanics(t, conn)
testCommitPanics(t, conn)
testRollbackPanics(t, conn)
testExecutePanics(t, conn)
testStreamExecutePanics(t, conn, fake)
testExecuteBatchPanics(t, conn)
testSplitQueryPanics(t, conn)
testStreamHealthPanics(t, conn)
fake.panics = false
// and we're done
conn.Close()
}