github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/executor/worker/test_util.go (about)

     1  // Copyright 2022 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 worker
    15  
    16  import (
    17  	"context"
    18  	"sync"
    19  	"time"
    20  
    21  	"github.com/pingcap/log"
    22  	"github.com/pingcap/tiflow/engine/pkg/clock"
    23  	"github.com/pingcap/tiflow/pkg/errors"
    24  	"go.uber.org/atomic"
    25  )
    26  
    27  type dummyWorker struct {
    28  	id RunnableID
    29  
    30  	needQuit atomic.Bool
    31  
    32  	blockMu   sync.Mutex
    33  	blockCond *sync.Cond
    34  	blocked   bool
    35  
    36  	submitTime atomic.Duration
    37  }
    38  
    39  func newDummyWorker(id RunnableID) *dummyWorker {
    40  	ret := &dummyWorker{
    41  		id:         id,
    42  		submitTime: *atomic.NewDuration(0),
    43  	}
    44  	ret.blockCond = sync.NewCond(&ret.blockMu)
    45  	return ret
    46  }
    47  
    48  func (d *dummyWorker) Init(ctx context.Context) error {
    49  	d.blockMu.Lock()
    50  	for d.blocked {
    51  		d.blockCond.Wait()
    52  	}
    53  	d.blockMu.Unlock()
    54  
    55  	rctx, ok := ToRuntimeCtx(ctx)
    56  	if !ok {
    57  		log.Panic("A RuntimeContext is expected to be used in unit tests")
    58  	}
    59  	d.submitTime.Store(time.Duration(rctx.SubmitTime()))
    60  
    61  	return nil
    62  }
    63  
    64  func (d *dummyWorker) Poll(ctx context.Context) error {
    65  	if d.needQuit.Load() {
    66  		return errors.New("worker is finished")
    67  	}
    68  	return nil
    69  }
    70  
    71  func (d *dummyWorker) Stop(ctx context.Context) error {
    72  	return nil
    73  }
    74  
    75  func (d *dummyWorker) ID() RunnableID {
    76  	return d.id
    77  }
    78  
    79  func (d *dummyWorker) Close(ctx context.Context) error {
    80  	return nil
    81  }
    82  
    83  func (d *dummyWorker) NotifyExit(ctx context.Context, errIn error) error {
    84  	return nil
    85  }
    86  
    87  func (d *dummyWorker) SetFinished() {
    88  	d.needQuit.Store(true)
    89  }
    90  
    91  func (d *dummyWorker) BlockInit() {
    92  	d.blockMu.Lock()
    93  	defer d.blockMu.Unlock()
    94  
    95  	d.blocked = true
    96  }
    97  
    98  func (d *dummyWorker) UnblockInit() {
    99  	d.blockMu.Lock()
   100  	defer d.blockMu.Unlock()
   101  
   102  	d.blocked = false
   103  	d.blockCond.Broadcast()
   104  }
   105  
   106  func (d *dummyWorker) SubmitTime() clock.MonotonicTime {
   107  	return clock.MonotonicTime(d.submitTime.Load())
   108  }