github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/framework/mock_worker_impl.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 import ( 17 "context" 18 "sync" 19 20 frameModel "github.com/pingcap/tiflow/engine/framework/model" 21 "github.com/pingcap/tiflow/engine/pkg/externalresource/broker" 22 metaModel "github.com/pingcap/tiflow/engine/pkg/meta/model" 23 pkgOrm "github.com/pingcap/tiflow/engine/pkg/orm" 24 "github.com/pingcap/tiflow/engine/pkg/p2p" 25 "github.com/stretchr/testify/mock" 26 "go.uber.org/atomic" 27 "go.uber.org/dig" 28 ) 29 30 type mockWorkerImpl struct { 31 mu sync.Mutex 32 mock.Mock 33 34 *DefaultBaseWorker 35 id frameModel.WorkerID 36 37 messageHandlerManager *p2p.MockMessageHandlerManager 38 messageSender *p2p.MockMessageSender 39 metaClient pkgOrm.Client 40 41 closed atomic.Bool 42 } 43 44 type workerParamListForTest struct { 45 dig.Out 46 47 MessageHandlerManager p2p.MessageHandlerManager 48 MessageSender p2p.MessageSender 49 FrameMetaClient pkgOrm.Client 50 BusinessClientConn metaModel.ClientConn 51 ResourceBroker broker.Broker 52 } 53 54 //nolint:unparam 55 func newMockWorkerImpl(workerID frameModel.WorkerID, masterID frameModel.MasterID) *mockWorkerImpl { 56 ret := &mockWorkerImpl{ 57 id: workerID, 58 } 59 60 ret.DefaultBaseWorker = MockBaseWorker(workerID, masterID, ret).DefaultBaseWorker 61 ret.messageHandlerManager = ret.DefaultBaseWorker.messageHandlerManager.(*p2p.MockMessageHandlerManager) 62 ret.messageSender = ret.DefaultBaseWorker.messageSender.(*p2p.MockMessageSender) 63 ret.metaClient = ret.DefaultBaseWorker.frameMetaClient 64 return ret 65 } 66 67 func (w *mockWorkerImpl) InitImpl(ctx context.Context) error { 68 w.mu.Lock() 69 defer w.mu.Unlock() 70 71 args := w.Called(ctx) 72 return args.Error(0) 73 } 74 75 func (w *mockWorkerImpl) Tick(ctx context.Context) error { 76 if w.closed.Load() { 77 panic("Tick called after CloseImpl is called") 78 } 79 80 w.mu.Lock() 81 defer w.mu.Unlock() 82 83 args := w.Called(ctx) 84 return args.Error(0) 85 } 86 87 func (w *mockWorkerImpl) Status() frameModel.WorkerStatus { 88 w.mu.Lock() 89 defer w.mu.Unlock() 90 91 args := w.Called() 92 return args.Get(0).(frameModel.WorkerStatus) 93 } 94 95 func (w *mockWorkerImpl) OnMasterMessage(ctx context.Context, topic p2p.Topic, message p2p.MessageValue) error { 96 w.mu.Lock() 97 defer w.mu.Unlock() 98 99 args := w.Called(ctx, topic, message) 100 return args.Error(0) 101 } 102 103 func (w *mockWorkerImpl) CloseImpl(ctx context.Context) { 104 if w.closed.Swap(true) { 105 panic("CloseImpl called twice") 106 } 107 108 w.mu.Lock() 109 defer w.mu.Unlock() 110 111 w.Called() 112 }