vitess.io/vitess@v0.16.2/go/vt/vtgate/executor_stream_test.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package vtgate 18 19 import ( 20 "testing" 21 "time" 22 23 querypb "vitess.io/vitess/go/vt/proto/query" 24 25 "vitess.io/vitess/go/cache" 26 "vitess.io/vitess/go/vt/discovery" 27 topodatapb "vitess.io/vitess/go/vt/proto/topodata" 28 29 "context" 30 31 "github.com/stretchr/testify/require" 32 33 "vitess.io/vitess/go/sqltypes" 34 _ "vitess.io/vitess/go/vt/vtgate/vindexes" 35 "vitess.io/vitess/go/vt/vttablet/sandboxconn" 36 ) 37 38 func TestStreamSQLUnsharded(t *testing.T) { 39 executor, _, _, _ := createExecutorEnv() 40 logChan := QueryLogger.Subscribe("Test") 41 defer QueryLogger.Unsubscribe(logChan) 42 43 sql := "stream * from user_msgs" 44 result, err := executorStreamMessages(executor, sql) 45 require.NoError(t, err) 46 wantResult := sandboxconn.StreamRowResult 47 if !result.Equal(wantResult) { 48 t.Errorf("result: %+v, want %+v", result, wantResult) 49 } 50 } 51 52 func TestStreamSQLSharded(t *testing.T) { 53 cell := "aa" 54 hc := discovery.NewFakeHealthCheck(nil) 55 s := createSandbox("TestExecutor") 56 s.VSchema = executorVSchema 57 getSandbox(KsTestUnsharded).VSchema = unshardedVSchema 58 serv := newSandboxForCells([]string{cell}) 59 resolver := newTestResolver(hc, serv, cell) 60 shards := []string{"-20", "20-40", "40-60", "60-80", "80-a0", "a0-c0", "c0-e0", "e0-"} 61 for _, shard := range shards { 62 _ = hc.AddTestTablet(cell, shard, 1, "TestExecutor", shard, topodatapb.TabletType_PRIMARY, true, 1, nil) 63 } 64 executor := NewExecutor(context.Background(), serv, cell, resolver, false, false, testBufferSize, cache.DefaultConfig, nil, false, querypb.ExecuteOptions_V3) 65 66 sql := "stream * from sharded_user_msgs" 67 result, err := executorStreamMessages(executor, sql) 68 require.NoError(t, err) 69 wantResult := &sqltypes.Result{ 70 Fields: sandboxconn.SingleRowResult.Fields, 71 Rows: [][]sqltypes.Value{ 72 sandboxconn.StreamRowResult.Rows[0], 73 sandboxconn.StreamRowResult.Rows[0], 74 sandboxconn.StreamRowResult.Rows[0], 75 sandboxconn.StreamRowResult.Rows[0], 76 sandboxconn.StreamRowResult.Rows[0], 77 sandboxconn.StreamRowResult.Rows[0], 78 sandboxconn.StreamRowResult.Rows[0], 79 sandboxconn.StreamRowResult.Rows[0], 80 }, 81 } 82 if !result.Equal(wantResult) { 83 t.Errorf("result: %+v, want %+v", result, wantResult) 84 } 85 } 86 87 func executorStreamMessages(executor *Executor, sql string) (qr *sqltypes.Result, err error) { 88 results := make(chan *sqltypes.Result, 100) 89 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) 90 defer cancel() 91 err = executor.StreamExecute( 92 ctx, 93 "TestExecuteStream", 94 NewSafeSession(primarySession), 95 sql, 96 nil, 97 func(qr *sqltypes.Result) error { 98 results <- qr 99 return nil 100 }, 101 ) 102 close(results) 103 if err != nil { 104 return nil, err 105 } 106 first := true 107 for r := range results { 108 if first { 109 qr = &sqltypes.Result{Fields: r.Fields} 110 first = false 111 } 112 qr.Rows = append(qr.Rows, r.Rows...) 113 } 114 return qr, nil 115 }