vitess.io/vitess@v0.16.2/go/vt/vtgate/executor_vstream_test.go (about)

     1  /*
     2  Copyright 2022 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  	"vitess.io/vitess/go/vt/vtgate/engine"
    24  
    25  	querypb "vitess.io/vitess/go/vt/proto/query"
    26  
    27  	binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata"
    28  
    29  	vtgatepb "vitess.io/vitess/go/vt/proto/vtgate"
    30  
    31  	"context"
    32  
    33  	"github.com/stretchr/testify/require"
    34  
    35  	"vitess.io/vitess/go/sqltypes"
    36  	_ "vitess.io/vitess/go/vt/vtgate/vindexes"
    37  )
    38  
    39  // TestVStreamSQLUnsharded tests the experimental 'vstream * from' vtgate olap query
    40  func TestVStreamSQLUnsharded(t *testing.T) {
    41  	t.Skip("this test is failing due to races") //FIXME
    42  	executor, _, _, sbcLookup := createExecutorEnv()
    43  	logChan := QueryLogger.Subscribe("Test")
    44  	defer QueryLogger.Unsubscribe(logChan)
    45  	send1 := []*binlogdatapb.VEvent{
    46  		{Type: binlogdatapb.VEventType_GTID, Gtid: "gtid01"},
    47  		{Type: binlogdatapb.VEventType_FIELD, FieldEvent: &binlogdatapb.FieldEvent{TableName: "t1", Fields: []*querypb.Field{
    48  			{Type: sqltypes.Int64},
    49  		}}},
    50  		{Type: binlogdatapb.VEventType_ROW, RowEvent: &binlogdatapb.RowEvent{TableName: "t1", RowChanges: []*binlogdatapb.RowChange{{
    51  			After: sqltypes.RowToProto3([]sqltypes.Value{
    52  				sqltypes.NewInt64(1),
    53  			}),
    54  		}}}},
    55  		{Type: binlogdatapb.VEventType_ROW, RowEvent: &binlogdatapb.RowEvent{TableName: "t1", RowChanges: []*binlogdatapb.RowChange{{
    56  			After: sqltypes.RowToProto3([]sqltypes.Value{
    57  				sqltypes.NewInt64(2),
    58  			}),
    59  		}}}},
    60  		{Type: binlogdatapb.VEventType_ROW, RowEvent: &binlogdatapb.RowEvent{TableName: "t1", RowChanges: []*binlogdatapb.RowChange{{
    61  			Before: sqltypes.RowToProto3([]sqltypes.Value{
    62  				sqltypes.NewInt64(2),
    63  			}),
    64  		}}}},
    65  		{Type: binlogdatapb.VEventType_ROW, RowEvent: &binlogdatapb.RowEvent{TableName: "t1", RowChanges: []*binlogdatapb.RowChange{{
    66  			Before: sqltypes.RowToProto3([]sqltypes.Value{
    67  				sqltypes.NewInt64(1),
    68  			}),
    69  			After: sqltypes.RowToProto3([]sqltypes.Value{
    70  				sqltypes.NewInt64(4),
    71  			}),
    72  		}}}},
    73  		{Type: binlogdatapb.VEventType_COMMIT},
    74  	}
    75  	sbcLookup.AddVStreamEvents(send1, nil)
    76  
    77  	sql := "vstream * from t1"
    78  
    79  	results := make(chan *sqltypes.Result, 20)
    80  	ctx, cancel := context.WithCancel(context.Background())
    81  	defer cancel()
    82  	go func() {
    83  		err := executor.StreamExecute(
    84  			ctx,
    85  			"TestExecuteStream",
    86  			NewAutocommitSession(&vtgatepb.Session{TargetString: KsTestUnsharded}),
    87  			sql,
    88  			nil,
    89  			func(qr *sqltypes.Result) error {
    90  				results <- qr
    91  				return nil
    92  			},
    93  		)
    94  		require.NoError(t, err)
    95  	}()
    96  	timer := time.NewTimer(5 * time.Second)
    97  	done := false
    98  	numRows, numInserts, numUpdates, numDeletes := 0, 0, 0, 0
    99  	expectedRows, expectedInserts, expectedUpdates, expectedDeletes := 4, 2, 1, 1
   100  	fieldsValidated := false
   101  	for {
   102  		if done {
   103  			break
   104  		}
   105  		select {
   106  		case qr := <-results:
   107  			if !fieldsValidated {
   108  				require.Equal(t, 2, len(qr.Fields))
   109  				fieldsValidated = true
   110  			}
   111  			for _, row := range qr.Rows {
   112  				numRows++
   113  				switch row[0].ToString() {
   114  				case engine.RowChangeInsert:
   115  					numInserts++
   116  				case engine.RowChangeUpdate:
   117  					numUpdates++
   118  				case engine.RowChangeDelete:
   119  					numDeletes++
   120  				default:
   121  					require.FailNowf(t, "", "Unknown row change indicator: %s", row[0].ToString())
   122  				}
   123  			}
   124  			if numRows >= expectedRows {
   125  				done = true
   126  			}
   127  		case <-timer.C:
   128  			done = true
   129  		}
   130  	}
   131  	require.Equal(t, expectedRows, numRows)
   132  	require.Equal(t, expectedInserts, numInserts)
   133  	require.Equal(t, expectedUpdates, numUpdates)
   134  	require.Equal(t, expectedDeletes, numDeletes)
   135  }