github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/syncer/safe-mode/mode_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 mode 15 16 import ( 17 "testing" 18 19 . "github.com/pingcap/check" 20 "github.com/pingcap/tidb/pkg/util/filter" 21 tcontext "github.com/pingcap/tiflow/dm/pkg/context" 22 ) 23 24 var _ = Suite(&testModeSuite{}) 25 26 func TestSuite(t *testing.T) { 27 TestingT(t) 28 } 29 30 type testModeSuite struct{} 31 32 func (t *testModeSuite) TestMode(c *C) { 33 m := NewSafeMode() 34 c.Assert(m.Enable(), IsFalse) 35 36 tctx := tcontext.Background() 37 // Add 1 38 err := m.Add(tctx, 1) 39 c.Assert(err, IsNil) 40 c.Assert(m.Enable(), IsTrue) 41 err = m.Add(tctx, -1) 42 c.Assert(m.Enable(), IsFalse) 43 c.Assert(err, IsNil) 44 45 // Add n 46 err = m.Add(tctx, 101) 47 c.Assert(m.Enable(), IsTrue) 48 c.Assert(err, IsNil) 49 err = m.Add(tctx, -1) 50 c.Assert(m.Enable(), IsTrue) 51 c.Assert(err, IsNil) 52 err = m.Add(tctx, -100) 53 c.Assert(m.Enable(), IsFalse) 54 c.Assert(err, IsNil) 55 56 // IncrForTable 57 table := &filter.Table{ 58 Schema: "schema", 59 Name: "table", 60 } 61 err = m.IncrForTable(tctx, table) 62 c.Assert(err, IsNil) 63 err = m.IncrForTable(tctx, table) // re-Add 64 c.Assert(err, IsNil) 65 c.Assert(m.Enable(), IsTrue) 66 err = m.DescForTable(tctx, table) 67 c.Assert(err, IsNil) 68 c.Assert(m.Enable(), IsFalse) 69 70 // Add n + IncrForTable 71 err = m.Add(tctx, 100) 72 c.Assert(err, IsNil) 73 err = m.IncrForTable(tctx, table) 74 c.Assert(err, IsNil) 75 c.Assert(m.Enable(), IsTrue) 76 err = m.Add(tctx, -100) 77 c.Assert(err, IsNil) 78 err = m.DescForTable(tctx, table) 79 c.Assert(m.Enable(), IsFalse) 80 c.Assert(err, IsNil) 81 82 // Add becomes to negative 83 err = m.Add(tctx, -1) 84 c.Assert(err, NotNil) 85 }