github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/pkg/binlog/reader/mock.go (about)

     1  // Copyright 2019 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package reader
    15  
    16  import (
    17  	"context"
    18  
    19  	gmysql "github.com/go-mysql-org/go-mysql/mysql"
    20  	"github.com/go-mysql-org/go-mysql/replication"
    21  )
    22  
    23  // MockReader is a binlog event reader which read binlog events from an input channel.
    24  type MockReader struct {
    25  	ch  chan *replication.BinlogEvent
    26  	ech chan error
    27  
    28  	// returned error for methods
    29  	ErrStartByPos  error
    30  	ErrStartByGTID error
    31  	ErrClose       error
    32  }
    33  
    34  // NewMockReader creates a MockReader instance.
    35  func NewMockReader() Reader {
    36  	return &MockReader{
    37  		ch:  make(chan *replication.BinlogEvent),
    38  		ech: make(chan error),
    39  	}
    40  }
    41  
    42  // StartSyncByPos implements Reader.StartSyncByPos.
    43  func (r *MockReader) StartSyncByPos(pos gmysql.Position) error {
    44  	return r.ErrStartByPos
    45  }
    46  
    47  // StartSyncByGTID implements Reader.StartSyncByGTID.
    48  func (r *MockReader) StartSyncByGTID(gSet gmysql.GTIDSet) error {
    49  	return r.ErrStartByGTID
    50  }
    51  
    52  // Close implements Reader.Close.
    53  func (r *MockReader) Close() error {
    54  	return r.ErrClose
    55  }
    56  
    57  // GetEvent implements Reader.GetEvent.
    58  func (r *MockReader) GetEvent(ctx context.Context) (*replication.BinlogEvent, error) {
    59  	select {
    60  	case ev := <-r.ch:
    61  		return ev, nil
    62  	case err := <-r.ech:
    63  		return nil, err
    64  	case <-ctx.Done():
    65  		return nil, ctx.Err()
    66  	}
    67  }
    68  
    69  // Status implements Reader.Status.
    70  func (r *MockReader) Status() interface{} {
    71  	return nil
    72  }
    73  
    74  // PushEvent pushes an event into the reader.
    75  func (r *MockReader) PushEvent(ctx context.Context, ev *replication.BinlogEvent) error {
    76  	select {
    77  	case r.ch <- ev:
    78  		return nil
    79  	case <-ctx.Done():
    80  		return ctx.Err()
    81  	}
    82  }
    83  
    84  // PushError pushes an error into the reader.
    85  func (r *MockReader) PushError(ctx context.Context, err error) error {
    86  	select {
    87  	case r.ech <- err:
    88  		return nil
    89  	case <-ctx.Done():
    90  		return ctx.Err()
    91  	}
    92  }