github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/processor/sinkmanager/manager_test_helper.go (about)

     1  // Copyright 2023 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 sinkmanager
    15  
    16  import (
    17  	"context"
    18  	"math"
    19  	"testing"
    20  
    21  	"github.com/pingcap/errors"
    22  	"github.com/pingcap/tiflow/cdc/entry"
    23  	"github.com/pingcap/tiflow/cdc/model"
    24  	"github.com/pingcap/tiflow/cdc/processor/sourcemanager"
    25  	"github.com/pingcap/tiflow/cdc/processor/sourcemanager/sorter"
    26  	"github.com/pingcap/tiflow/cdc/processor/sourcemanager/sorter/memory"
    27  	"github.com/pingcap/tiflow/cdc/redo"
    28  	"github.com/pingcap/tiflow/pkg/upstream"
    29  	pd "github.com/tikv/pd/client"
    30  )
    31  
    32  // MockPD only for test.
    33  type MockPD struct {
    34  	pd.Client
    35  	ts int64
    36  }
    37  
    38  // GetTS implements the PD interface.
    39  func (p *MockPD) GetTS(_ context.Context) (int64, int64, error) {
    40  	if p.ts != 0 {
    41  		return p.ts, p.ts, nil
    42  	}
    43  	return math.MaxInt64, math.MaxInt64, nil
    44  }
    45  
    46  // nolint:revive
    47  // In test it is ok move the ctx to the second parameter.
    48  func CreateManagerWithMemEngine(
    49  	t *testing.T,
    50  	ctx context.Context,
    51  	changefeedID model.ChangeFeedID,
    52  	changefeedInfo *model.ChangeFeedInfo,
    53  	errChan chan error,
    54  ) (*SinkManager, *sourcemanager.SourceManager, sorter.SortEngine) {
    55  	handleError := func(err error) {
    56  		if err != nil && errors.Cause(err) != context.Canceled {
    57  			select {
    58  			case errChan <- err:
    59  			case <-ctx.Done():
    60  			}
    61  		}
    62  	}
    63  
    64  	sortEngine := memory.New(ctx)
    65  	up := upstream.NewUpstream4Test(&MockPD{})
    66  	mg := &entry.MockMountGroup{}
    67  	schemaStorage := &entry.MockSchemaStorage{Resolved: math.MaxUint64}
    68  
    69  	sourceManager := sourcemanager.NewForTest(changefeedID, up, mg, sortEngine, false)
    70  	go func() { handleError(sourceManager.Run(ctx)) }()
    71  	sourceManager.WaitForReady(ctx)
    72  
    73  	sinkManager := New(changefeedID, changefeedInfo.SinkURI,
    74  		changefeedInfo.Config, up, schemaStorage, nil, sourceManager)
    75  	go func() { handleError(sinkManager.Run(ctx)) }()
    76  	sinkManager.WaitForReady(ctx)
    77  
    78  	return sinkManager, sourceManager, sortEngine
    79  }
    80  
    81  // nolint:revive
    82  // In test it is ok move the ctx to the second parameter.
    83  func NewManagerWithMemEngine(
    84  	t *testing.T,
    85  	changefeedID model.ChangeFeedID,
    86  	changefeedInfo *model.ChangeFeedInfo,
    87  	redoMgr redo.DMLManager,
    88  ) (*SinkManager, *sourcemanager.SourceManager, sorter.SortEngine) {
    89  	sortEngine := memory.New(context.Background())
    90  	up := upstream.NewUpstream4Test(&MockPD{})
    91  	mg := &entry.MockMountGroup{}
    92  	schemaStorage := &entry.MockSchemaStorage{Resolved: math.MaxUint64}
    93  	sourceManager := sourcemanager.NewForTest(changefeedID, up, mg, sortEngine, false)
    94  	sinkManager := New(changefeedID, changefeedInfo.SinkURI,
    95  		changefeedInfo.Config, up, schemaStorage, redoMgr, sourceManager)
    96  	return sinkManager, sourceManager, sortEngine
    97  }