github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/framework/mock_worker_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 framework
    15  
    16  // This file provides helper function to let the implementation of WorkerImpl
    17  // can finish its unit tests.
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	frameModel "github.com/pingcap/tiflow/engine/framework/model"
    24  	"github.com/pingcap/tiflow/engine/framework/statusutil"
    25  	dcontext "github.com/pingcap/tiflow/engine/pkg/context"
    26  	"github.com/pingcap/tiflow/engine/pkg/deps"
    27  	"github.com/pingcap/tiflow/engine/pkg/externalresource/broker"
    28  	metaMock "github.com/pingcap/tiflow/engine/pkg/meta/mock"
    29  	pkgOrm "github.com/pingcap/tiflow/engine/pkg/orm"
    30  	"github.com/pingcap/tiflow/engine/pkg/p2p"
    31  	"github.com/stretchr/testify/require"
    32  )
    33  
    34  // BaseWorkerForTesting mocks base worker
    35  type BaseWorkerForTesting struct {
    36  	*DefaultBaseWorker
    37  	Broker *broker.MockBroker
    38  }
    39  
    40  // MockBaseWorker creates a mock base worker for test
    41  func MockBaseWorker(
    42  	workerID frameModel.WorkerID,
    43  	masterID frameModel.MasterID,
    44  	workerImpl WorkerImpl,
    45  ) *BaseWorkerForTesting {
    46  	ctx := dcontext.Background()
    47  	dp := deps.NewDeps()
    48  	cli, err := pkgOrm.NewMockClient()
    49  	if err != nil {
    50  		panic(err)
    51  	}
    52  	resourceBroker := broker.NewBrokerForTesting("executor-1")
    53  	params := workerParamListForTest{
    54  		MessageHandlerManager: p2p.NewMockMessageHandlerManager(),
    55  		MessageSender:         p2p.NewMockMessageSender(),
    56  		FrameMetaClient:       cli,
    57  		BusinessClientConn:    metaMock.NewMockClientConn(),
    58  		ResourceBroker:        resourceBroker,
    59  	}
    60  	err = dp.Provide(func() workerParamListForTest {
    61  		return params
    62  	})
    63  	if err != nil {
    64  		panic(err)
    65  	}
    66  	ctx = ctx.WithDeps(dp)
    67  
    68  	epoch, err := params.FrameMetaClient.GenEpoch(ctx)
    69  	if err != nil {
    70  		panic(err)
    71  	}
    72  	ret := NewBaseWorker(
    73  		ctx,
    74  		workerImpl,
    75  		workerID,
    76  		masterID,
    77  		frameModel.FakeTask,
    78  		epoch,
    79  	)
    80  	return &BaseWorkerForTesting{
    81  		ret.(*DefaultBaseWorker),
    82  		resourceBroker,
    83  	}
    84  }
    85  
    86  // MockBaseWorkerCheckSendMessage checks can receive one message from mock message sender
    87  func MockBaseWorkerCheckSendMessage(
    88  	t *testing.T,
    89  	worker *DefaultBaseWorker,
    90  	topic p2p.Topic,
    91  	message interface{},
    92  ) {
    93  	masterNode := worker.masterClient.MasterNode()
    94  	got, ok := worker.messageSender.(*p2p.MockMessageSender).TryPop(masterNode, topic)
    95  	require.True(t, ok)
    96  	require.Equal(t, message, got)
    97  }
    98  
    99  // MockBaseWorkerWaitUpdateStatus checks can receive a update status message from
   100  // mock message sender
   101  func MockBaseWorkerWaitUpdateStatus(
   102  	t *testing.T,
   103  	worker *DefaultBaseWorker,
   104  ) {
   105  	topic := statusutil.WorkerStatusTopic(worker.masterClient.MasterID())
   106  	masterNode := worker.masterClient.MasterNode()
   107  	require.Eventually(t, func() bool {
   108  		_, ok := worker.messageSender.(*p2p.MockMessageSender).TryPop(masterNode, topic)
   109  		return ok
   110  	}, time.Second, 100*time.Millisecond)
   111  }