github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/syncer/status_test.go (about) 1 // Copyright 2021 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 "sync" 18 19 "github.com/go-mysql-org/go-mysql/mysql" 20 . "github.com/pingcap/check" 21 "github.com/pingcap/tidb/pkg/parser/model" 22 "github.com/pingcap/tidb/pkg/util/filter" 23 "github.com/pingcap/tiflow/dm/config" 24 "github.com/pingcap/tiflow/dm/pb" 25 "github.com/pingcap/tiflow/dm/pkg/binlog" 26 tcontext "github.com/pingcap/tiflow/dm/pkg/context" 27 "github.com/pingcap/tiflow/dm/pkg/log" 28 "github.com/pingcap/tiflow/dm/syncer/metrics" 29 "github.com/pingcap/tiflow/dm/syncer/shardddl" 30 "go.uber.org/zap" 31 ) 32 33 var _ = Suite(&statusSuite{}) 34 35 type statusSuite struct{} 36 37 func (t *statusSuite) TestStatusRace(c *C) { 38 s := &Syncer{} 39 40 l := log.With(zap.String("unit test", "TestStatueRace")) 41 s.tctx = tcontext.Background().WithLogger(l) 42 s.cfg = &config.SubTaskConfig{} 43 s.checkpoint = &mockCheckpoint{} 44 s.pessimist = shardddl.NewPessimist(&l, nil, "", "") 45 s.optimist = shardddl.NewOptimist(&l, nil, "", "") 46 s.metricsProxies = metrics.DefaultMetricsProxies.CacheForOneTask("task", "worker", "source") 47 48 sourceStatus := &binlog.SourceStatus{ 49 Location: binlog.Location{ 50 Position: mysql.Position{ 51 Name: "mysql-bin.000123", 52 Pos: 223, 53 }, 54 }, 55 Binlogs: binlog.FileSizes(nil), 56 } 57 58 wg := sync.WaitGroup{} 59 for i := 0; i < 10; i++ { 60 wg.Add(1) 61 go func() { 62 defer wg.Done() 63 ret := s.Status(sourceStatus) 64 status := ret.(*pb.SyncStatus) 65 c.Assert(status.MasterBinlog, Equals, "(mysql-bin.000123, 223)") 66 c.Assert(status.SyncerBinlog, Equals, "(mysql-bin.000123, 123)") 67 }() 68 } 69 wg.Wait() 70 } 71 72 type mockCheckpoint struct { 73 CheckPoint 74 } 75 76 func (*mockCheckpoint) FlushedGlobalPoint() binlog.Location { 77 return binlog.Location{ 78 Position: mysql.Position{ 79 Name: "mysql-bin.000123", 80 Pos: 123, 81 }, 82 } 83 } 84 85 func (*mockCheckpoint) SaveTablePoint(_ *filter.Table, _ binlog.Location, _ *model.TableInfo) {}