github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/jobmaster/example/worker_impl_test.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 example 15 16 import ( 17 "context" 18 "testing" 19 "time" 20 21 "github.com/pingcap/tiflow/engine/framework" 22 frameModel "github.com/pingcap/tiflow/engine/framework/model" 23 "github.com/pingcap/tiflow/pkg/logutil" 24 "github.com/stretchr/testify/require" 25 ) 26 27 func newExampleWorker() *exampleWorker { 28 self := &exampleWorker{} 29 self.BaseWorker = framework.MockBaseWorker(workerID, masterID, self) 30 return self 31 } 32 33 func TestExampleWorker(t *testing.T) { 34 t.Parallel() 35 36 initLogger.Do(func() { 37 _ = logutil.InitLogger(&logutil.Config{ 38 Level: "debug", 39 }) 40 }) 41 42 worker := newExampleWorker() 43 ctx := context.Background() 44 err := worker.Init(ctx) 45 require.NoError(t, err) 46 47 // tick twice 48 err = worker.Tick(ctx) 49 require.NoError(t, err) 50 err = worker.Tick(ctx) 51 require.NoError(t, err) 52 53 broker := worker.BaseWorker.(*framework.BaseWorkerForTesting).Broker 54 defer broker.Close() 55 broker.AssertPersisted(t, "/local/example") 56 broker.AssertFileExists(t, workerID, "/local/example", "1.txt") 57 broker.AssertFileExists(t, workerID, "/local/example", "2.txt") 58 59 time.Sleep(time.Second) 60 require.Eventually(t, func() bool { 61 return worker.Status().State == frameModel.WorkerStateFinished 62 }, time.Second, time.Millisecond*100) 63 64 resp, err := worker.BaseWorker.MetaKVClient().Get(ctx, tickKey) 65 require.NoError(t, err) 66 require.Len(t, resp.Kvs, 1) 67 require.Equal(t, "2", string(resp.Kvs[0].Value)) 68 69 framework.MockBaseWorkerCheckSendMessage(t, worker.BaseWorker.(*framework.BaseWorkerForTesting).DefaultBaseWorker, testTopic, testMsg) 70 err = worker.Close(ctx) 71 require.NoError(t, err) 72 }