vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletserver/vstreamer/resultstreamer_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 vstreamer
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/require"
    25  
    26  	binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata"
    27  )
    28  
    29  func TestStreamResults(t *testing.T) {
    30  	if testing.Short() {
    31  		t.Skip()
    32  	}
    33  
    34  	reset := AdjustPacketSize(1)
    35  	defer reset()
    36  	engine.resultStreamerNumPackets.Reset()
    37  	engine.resultStreamerNumRows.Reset()
    38  
    39  	execStatements(t, []string{
    40  		"create table t1(id int, val varbinary(128), primary key(id))",
    41  		"insert into t1 values (1, 'aaa'), (2, 'bbb')",
    42  	})
    43  	defer execStatements(t, []string{
    44  		"drop table t1",
    45  	})
    46  	engine.se.Reload(context.Background())
    47  
    48  	query := "select id, val from t1 order by id"
    49  	wantStream := []string{
    50  		`rows:{lengths:1 lengths:3 values:"1aaa"}`,
    51  		`rows:{lengths:1 lengths:3 values:"2bbb"}`,
    52  	}
    53  	i := 0
    54  	ch := make(chan error)
    55  	// We don't want to report errors inside callback functions because
    56  	// line numbers come out wrong.
    57  	go func() {
    58  		first := true
    59  		defer close(ch)
    60  		err := engine.StreamResults(context.Background(), query, func(rows *binlogdatapb.VStreamResultsResponse) error {
    61  			if first {
    62  				first = false
    63  				if rows.Gtid == "" {
    64  					ch <- fmt.Errorf("stream gtid is empty")
    65  				}
    66  				return nil
    67  			}
    68  			if i >= len(wantStream) {
    69  				ch <- fmt.Errorf("unexpected stream rows: %v", rows)
    70  				return nil
    71  			}
    72  			srows := fmt.Sprintf("%v", rows)
    73  			if srows != wantStream[i] {
    74  				ch <- fmt.Errorf("stream %d:\n%s, want\n%s", i, srows, wantStream[i])
    75  			}
    76  			i++
    77  			return nil
    78  		})
    79  		if err != nil {
    80  			ch <- err
    81  		}
    82  	}()
    83  	for err := range ch {
    84  		t.Error(err)
    85  	}
    86  	require.Equal(t, int64(2), engine.resultStreamerNumPackets.Get())
    87  	require.Equal(t, int64(2), engine.resultStreamerNumRows.Get())
    88  }