github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/loader/checkpoint_test.go (about) 1 // Copyright 2019 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 loader 15 16 import ( 17 "context" 18 "database/sql" 19 "fmt" 20 21 "github.com/DATA-DOG/go-sqlmock" 22 . "github.com/pingcap/check" 23 "github.com/pingcap/tiflow/dm/config/dbconfig" 24 "github.com/pingcap/tiflow/dm/pkg/conn" 25 "github.com/pingcap/tiflow/dm/pkg/log" 26 ) 27 28 var _ = Suite(&lightningCpListSuite{}) 29 30 type lightningCpListSuite struct { 31 mock sqlmock.Sqlmock 32 cpList *LightningCheckpointList 33 } 34 35 func (s *lightningCpListSuite) SetUpTest(c *C) { 36 s.mock = conn.InitMockDB(c) 37 38 baseDB, err := conn.GetDownstreamDB(&dbconfig.DBConfig{}) 39 c.Assert(err, IsNil) 40 41 metaSchema := "dm_meta" 42 cpList := NewLightningCheckpointList(baseDB, "test_lightning", "source1", metaSchema, log.L()) 43 44 s.cpList = cpList 45 } 46 47 func (s *lightningCpListSuite) TearDownTest(c *C) { 48 c.Assert(s.mock.ExpectationsWereMet(), IsNil) 49 } 50 51 func (s *lightningCpListSuite) TestLightningCheckpointListPrepare(c *C) { 52 ctx := context.Background() 53 s.mock.ExpectBegin() 54 s.mock.ExpectExec(fmt.Sprintf("CREATE SCHEMA IF NOT EXISTS %s.*", s.cpList.schema)).WillReturnResult(sqlmock.NewResult(1, 1)) 55 s.mock.ExpectCommit() 56 s.mock.ExpectBegin() 57 s.mock.ExpectExec(fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s.*", s.cpList.tableName)).WillReturnResult(sqlmock.NewResult(1, 1)) 58 s.mock.ExpectCommit() 59 err := s.cpList.Prepare(ctx) 60 c.Assert(err, IsNil) 61 } 62 63 func (s *lightningCpListSuite) TestLightningCheckpointListStatusInit(c *C) { 64 // no rows in target table, will return default status 65 s.mock.ExpectQuery(fmt.Sprintf("SELECT status FROM %s WHERE `task_name` = \\? AND `source_name` = \\?", s.cpList.tableName)). 66 WithArgs(s.cpList.taskName, s.cpList.sourceName). 67 WillReturnRows(sqlmock.NewRows([]string{"status"}).RowError(0, sql.ErrNoRows)) 68 status, err := s.cpList.taskStatus(context.Background()) 69 c.Assert(err, IsNil) 70 c.Assert(status, Equals, lightningStatusInit) 71 } 72 73 func (s *lightningCpListSuite) TestLightningCheckpointListStatusRunning(c *C) { 74 s.mock.ExpectQuery(fmt.Sprintf("SELECT status FROM %s WHERE `task_name` = \\? AND `source_name` = \\?", s.cpList.tableName)). 75 WithArgs(s.cpList.taskName, s.cpList.sourceName). 76 WillReturnRows(sqlmock.NewRows([]string{"status"}).AddRow("running")) 77 status, err := s.cpList.taskStatus(context.Background()) 78 c.Assert(err, IsNil) 79 c.Assert(status, Equals, lightningStatusRunning) 80 } 81 82 func (s *lightningCpListSuite) TestLightningCheckpointListRegister(c *C) { 83 s.mock.ExpectBegin() 84 s.mock.ExpectExec(fmt.Sprintf("INSERT IGNORE INTO %s \\(`task_name`, `source_name`\\) VALUES \\(\\?, \\?\\)", s.cpList.tableName)). 85 WithArgs(s.cpList.taskName, s.cpList.sourceName). 86 WillReturnResult(sqlmock.NewResult(2, 1)) 87 s.mock.ExpectCommit() 88 err := s.cpList.RegisterCheckPoint(context.Background()) 89 c.Assert(err, IsNil) 90 } 91 92 func (s *lightningCpListSuite) TestLightningCheckpointListUpdateStatus(c *C) { 93 s.mock.ExpectBegin() 94 s.mock.ExpectExec(fmt.Sprintf("UPDATE %s set status = \\? WHERE `task_name` = \\? AND `source_name` = \\?", s.cpList.tableName)). 95 WithArgs("running", s.cpList.taskName, s.cpList.sourceName). 96 WillReturnResult(sqlmock.NewResult(3, 1)) 97 s.mock.ExpectCommit() 98 err := s.cpList.UpdateStatus(context.Background(), lightningStatusRunning) 99 c.Assert(err, IsNil) 100 }