vitess.io/vitess@v0.16.2/go/vt/binlog/binlogplayer/framework_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 binlogplayer
    18  
    19  import (
    20  	"context"
    21  	"reflect"
    22  	"testing"
    23  
    24  	"google.golang.org/protobuf/proto"
    25  
    26  	binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata"
    27  	querypb "vitess.io/vitess/go/vt/proto/query"
    28  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    29  )
    30  
    31  // This partially duplicates code from vreplication/framework_test.go.
    32  // TODO(sougou): merge the functionality when we move this package to vreplication.
    33  
    34  // fakeBinlogClient satisfies Client.
    35  // Not to be used concurrently.
    36  type fakeBinlogClient struct {
    37  	lastTablet   *topodatapb.Tablet
    38  	lastPos      string
    39  	lastTables   []string
    40  	lastKeyRange *topodatapb.KeyRange
    41  	lastCharset  *binlogdatapb.Charset
    42  }
    43  
    44  func newFakeBinlogClient() *fakeBinlogClient {
    45  	globalFBC = &fakeBinlogClient{}
    46  	return globalFBC
    47  }
    48  
    49  func (fbc *fakeBinlogClient) Dial(tablet *topodatapb.Tablet) error {
    50  	fbc.lastTablet = tablet
    51  	return nil
    52  }
    53  
    54  func (fbc *fakeBinlogClient) Close() {
    55  }
    56  
    57  func (fbc *fakeBinlogClient) StreamTables(ctx context.Context, position string, tables []string, charset *binlogdatapb.Charset) (BinlogTransactionStream, error) {
    58  	fbc.lastPos = position
    59  	fbc.lastTables = tables
    60  	fbc.lastCharset = charset
    61  	return &btStream{ctx: ctx}, nil
    62  }
    63  
    64  func (fbc *fakeBinlogClient) StreamKeyRange(ctx context.Context, position string, keyRange *topodatapb.KeyRange, charset *binlogdatapb.Charset) (BinlogTransactionStream, error) {
    65  	fbc.lastPos = position
    66  	fbc.lastKeyRange = keyRange
    67  	fbc.lastCharset = charset
    68  	return &btStream{ctx: ctx}, nil
    69  }
    70  
    71  // btStream satisfies BinlogTransactionStream
    72  type btStream struct {
    73  	ctx  context.Context
    74  	sent bool
    75  }
    76  
    77  func (t *btStream) Recv() (*binlogdatapb.BinlogTransaction, error) {
    78  	if !t.sent {
    79  		t.sent = true
    80  		return &binlogdatapb.BinlogTransaction{
    81  			Statements: []*binlogdatapb.BinlogTransaction_Statement{
    82  				{
    83  					Category: binlogdatapb.BinlogTransaction_Statement_BL_INSERT,
    84  					Sql:      []byte("insert into t values(1)"),
    85  				},
    86  			},
    87  			EventToken: &querypb.EventToken{
    88  				Timestamp: 72,
    89  				Position:  "MariaDB/0-1-1235",
    90  			},
    91  		}, nil
    92  	}
    93  	<-t.ctx.Done()
    94  	return nil, t.ctx.Err()
    95  }
    96  
    97  func expectFBCRequest(t *testing.T, fbc *fakeBinlogClient, tablet *topodatapb.Tablet, pos string, tables []string, kr *topodatapb.KeyRange) {
    98  	t.Helper()
    99  	if !proto.Equal(tablet, fbc.lastTablet) {
   100  		t.Errorf("Request tablet: %v, want %v", fbc.lastTablet, tablet)
   101  	}
   102  	if pos != fbc.lastPos {
   103  		t.Errorf("Request pos: %v, want %v", fbc.lastPos, pos)
   104  	}
   105  	if !reflect.DeepEqual(tables, fbc.lastTables) {
   106  		t.Errorf("Request tables: %v, want %v", fbc.lastTables, tables)
   107  	}
   108  	if !proto.Equal(kr, fbc.lastKeyRange) {
   109  		t.Errorf("Request KeyRange: %v, want %v", fbc.lastKeyRange, kr)
   110  	}
   111  }
   112  
   113  // globalFBC is set by newFakeBinlogClient, which is then returned by the client factory below.
   114  var globalFBC *fakeBinlogClient
   115  
   116  func init() {
   117  	RegisterClientFactory("test", func() Client { return globalFBC })
   118  
   119  	SetProtocol("binlogplayer_test_framework", "test")
   120  }