github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/kv/regionlock/range_ts_map_test.go (about) 1 // Copyright 2024 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 regionlock 15 16 import ( 17 "math" 18 "testing" 19 20 "github.com/stretchr/testify/require" 21 ) 22 23 func TestRangeTsMap(t *testing.T) { 24 t.Parallel() 25 26 m := newRangeTsMap([]byte("a"), []byte("z"), math.MaxUint64) 27 28 mustGetMin := func(startKey, endKey string, expectedTs uint64) { 29 ts := m.getMinTsInRange([]byte(startKey), []byte(endKey)) 30 require.Equal(t, expectedTs, ts) 31 } 32 set := func(startKey, endKey string, ts uint64) { 33 m.set([]byte(startKey), []byte(endKey), ts) 34 } 35 unset := func(startKey, endKey string) { 36 m.unset([]byte(startKey), []byte(endKey)) 37 } 38 39 mustGetMin("a", "z", math.MaxUint64) 40 unset("b", "e") 41 set("b", "e", 100) 42 mustGetMin("a", "z", 100) 43 mustGetMin("b", "e", 100) 44 mustGetMin("a", "c", 100) 45 mustGetMin("d", "f", 100) 46 mustGetMin("a", "b", math.MaxUint64) 47 mustGetMin("e", "f", math.MaxUint64) 48 mustGetMin("a", "b\x00", 100) 49 50 unset("d", "g") 51 set("d", "g", 80) 52 mustGetMin("d", "g", 80) 53 mustGetMin("a", "z", 80) 54 mustGetMin("d", "e", 80) 55 mustGetMin("a", "d", 100) 56 57 unset("c", "f") 58 set("c", "f", 120) 59 mustGetMin("c", "f", 120) 60 mustGetMin("c", "d", 120) 61 mustGetMin("d", "e", 120) 62 mustGetMin("e", "f", 120) 63 mustGetMin("b", "e", 100) 64 mustGetMin("a", "z", 80) 65 66 unset("c", "f") 67 set("c", "f", 130) 68 mustGetMin("c", "f", 130) 69 mustGetMin("c", "d", 130) 70 mustGetMin("d", "e", 130) 71 mustGetMin("e", "f", 130) 72 mustGetMin("b", "e", 100) 73 mustGetMin("a", "z", 80) 74 }