github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/filter/filter_test_helper.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 filter 15 16 import ( 17 "testing" 18 19 ticonfig "github.com/pingcap/tidb/pkg/config" 20 tiddl "github.com/pingcap/tidb/pkg/ddl" 21 "github.com/pingcap/tidb/pkg/domain" 22 "github.com/pingcap/tidb/pkg/kv" 23 timeta "github.com/pingcap/tidb/pkg/meta" 24 timodel "github.com/pingcap/tidb/pkg/parser/model" 25 "github.com/pingcap/tidb/pkg/session" 26 "github.com/pingcap/tidb/pkg/store/mockstore" 27 "github.com/pingcap/tidb/pkg/testkit" 28 "github.com/pingcap/tiflow/cdc/model" 29 "github.com/stretchr/testify/require" 30 "github.com/tikv/client-go/v2/oracle" 31 ) 32 33 // testHelper is a test helper for filter which creates 34 // an internal tidb instance to generate DDL jobs with meta information 35 type testHelper struct { 36 t *testing.T 37 tk *testkit.TestKit 38 storage kv.Storage 39 domain *domain.Domain 40 } 41 42 // newTestHelper creates a FilterTestHelper 43 func newTestHelper(t *testing.T) *testHelper { 44 store, err := mockstore.NewMockStore() 45 require.Nil(t, err) 46 ticonfig.UpdateGlobal(func(conf *ticonfig.Config) { 47 conf.AlterPrimaryKey = true 48 }) 49 session.SetSchemaLease(0) 50 session.DisableStats4Test() 51 domain, err := session.BootstrapSession(store) 52 require.Nil(t, err) 53 domain.SetStatsUpdating(true) 54 tk := testkit.NewTestKit(t, store) 55 return &testHelper{ 56 t: t, 57 tk: tk, 58 storage: store, 59 domain: domain, 60 } 61 } 62 63 // ddlToJob executes the DDL stmt and returns the DDL job 64 func (s *testHelper) ddlToJob(ddl string) *timodel.Job { 65 s.tk.MustExec(ddl) 66 jobs, err := tiddl.GetLastNHistoryDDLJobs(s.getCurrentMeta(), 1) 67 require.Nil(s.t, err) 68 require.Len(s.t, jobs, 1) 69 // Set State from Synced to Done. 70 // Because jobs are put to history queue after TiDB alter its state from 71 // Done to Synced. 72 jobs[0].State = timodel.JobStateDone 73 return jobs[0] 74 } 75 76 // execDDL executes the DDL statement and returns the newest TableInfo of the table. 77 func (s *testHelper) execDDL(ddl string) *model.TableInfo { 78 job := s.ddlToJob(ddl) 79 ti, err := s.getCurrentMeta().GetTable(job.SchemaID, job.TableID) 80 require.Nil(s.t, err) 81 return model.WrapTableInfo(job.ID, job.SchemaName, job.BinlogInfo.FinishedTS, ti) 82 } 83 84 // getTk returns the TestKit 85 func (s *testHelper) getTk() *testkit.TestKit { 86 return s.tk 87 } 88 89 // getCurrentMeta return the current meta snapshot 90 func (s *testHelper) getCurrentMeta() *timeta.Meta { 91 ver, err := s.storage.CurrentVersion(oracle.GlobalTxnScope) 92 require.Nil(s.t, err) 93 return timeta.NewSnapshotMeta(s.storage.GetSnapshot(ver)) 94 } 95 96 // close closes the helper 97 func (s *testHelper) close() { 98 s.domain.Close() 99 s.storage.Close() //nolint:errcheck 100 }