github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/syncer/test_injector.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 syncer
    15  
    16  import (
    17  	"github.com/pingcap/tiflow/dm/pkg/log"
    18  	"github.com/pingcap/tiflow/dm/pkg/terror"
    19  	"go.uber.org/zap"
    20  )
    21  
    22  // TestInjector is used to support inject test cases into syncer.
    23  // In some cases, we use failpoint to control the test flow,
    24  // but we may need to control the flow based on some previous status,
    25  // so we add this TestInjector to record these status.
    26  // NOTE: if HTTP for failpoint works well, then we may remove this.
    27  type TestInjector struct {
    28  	flushCheckpointStage int
    29  }
    30  
    31  var testInjector = TestInjector{}
    32  
    33  // handleFlushCheckpointStage handles failpoint of `FlushCheckpointStage`.
    34  func handleFlushCheckpointStage(expectStage, maxStage int, stageStr string) error {
    35  	if testInjector.flushCheckpointStage != expectStage {
    36  		return nil
    37  	}
    38  
    39  	log.L().Info("set FlushCheckpointStage", zap.String("failpoint", "FlushCheckpointStage"), zap.Int("stage", testInjector.flushCheckpointStage))
    40  	if testInjector.flushCheckpointStage == maxStage {
    41  		testInjector.flushCheckpointStage = -1 // disable for following stages.
    42  	} else {
    43  		testInjector.flushCheckpointStage++
    44  	}
    45  	return terror.ErrSyncerFailpoint.Generatef("failpoint error for FlushCheckpointStage %s", stageStr)
    46  }