github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/p2p/server_ack_manager_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 p2p 15 16 import ( 17 "fmt" 18 "testing" 19 20 "github.com/stretchr/testify/require" 21 ) 22 23 func TestAckManagerGetSet(t *testing.T) { 24 t.Parallel() 25 26 am := newAckManager() 27 ack := am.Get("node-1", "topic-1") 28 require.Equal(t, initAck, ack) 29 30 am.Set("node-1", "topic-1", Seq(5)) 31 ack = am.Get("node-1", "topic-1") 32 require.Equal(t, Seq(5), ack) 33 34 ack = am.Get("node-1", "topic-2") 35 require.Equal(t, initAck, ack) 36 37 am.Set("node-1", "topic-2", Seq(7)) 38 ack = am.Get("node-1", "topic-2") 39 require.Equal(t, Seq(7), ack) 40 41 ack = am.Get("node-2", "topic-1") 42 require.Equal(t, initAck, ack) 43 44 am.Set("node-2", "topic-1", Seq(8)) 45 ack = am.Get("node-2", "topic-1") 46 require.Equal(t, Seq(8), ack) 47 48 am.Set("node-1", "topic-2", Seq(7)) 49 ack = am.Get("node-1", "topic-2") 50 require.Equal(t, Seq(7), ack) 51 } 52 53 func TestAckManagerRange(t *testing.T) { 54 t.Parallel() 55 56 am := newAckManager() 57 58 expected := make(map[Topic]Seq) 59 for i := 1; i <= 100; i++ { 60 am.Set("node-1", fmt.Sprintf("topic-%d", i), Seq(i+1)) 61 expected[fmt.Sprintf("topic-%d", i)] = Seq(i + 1) 62 } 63 64 actual := make(map[Topic]Seq) 65 am.Range("node-1", func(topic Topic, seq Seq) bool { 66 actual[topic] = seq 67 return true 68 }) 69 70 require.Equal(t, expected, actual) 71 } 72 73 func TestAckManagerRangeTerminate(t *testing.T) { 74 t.Parallel() 75 76 am := newAckManager() 77 78 for i := 1; i <= 100; i++ { 79 am.Set("node-1", fmt.Sprintf("topic-%d", i), Seq(i+1)) 80 } 81 82 counter := 0 83 am.Range("node-1", func(topic Topic, seq Seq) bool { 84 counter++ 85 require.LessOrEqual(t, counter, 50) 86 return counter != 50 87 }) 88 } 89 90 func TestAckManagerRangeNotExists(t *testing.T) { 91 t.Parallel() 92 93 am := newAckManager() 94 95 for i := 1; i <= 100; i++ { 96 am.Set("node-1", fmt.Sprintf("topic-%d", i), Seq(i+1)) 97 } 98 99 am.Range("node-2", func(topic Topic, seq Seq) bool { 100 require.Fail(t, "unreachable") 101 return false 102 }) 103 } 104 105 func TestAckManagerRemove(t *testing.T) { 106 t.Parallel() 107 108 am := newAckManager() 109 for i := 1; i <= 10; i++ { 110 for j := 1; j <= 10; j++ { 111 am.Set(fmt.Sprintf("node-%d", i), fmt.Sprintf("topic-%d", j), Seq(i+j)) 112 } 113 } 114 115 ack := am.Get("node-2", "topic-3") 116 require.Equal(t, Seq(5), ack) 117 118 ack = am.Get("node-3", "topic-3") 119 require.Equal(t, Seq(6), ack) 120 121 am.RemoveTopic("topic-3") 122 123 ack = am.Get("node-2", "topic-3") 124 require.Equal(t, initAck, ack) 125 126 ack = am.Get("node-3", "topic-3") 127 require.Equal(t, initAck, ack) 128 129 ack = am.Get("node-2", "topic-2") 130 require.Equal(t, Seq(4), ack) 131 132 ack = am.Get("node-3", "topic-2") 133 require.Equal(t, Seq(5), ack) 134 135 am.RemoveNode("node-2") 136 137 ack = am.Get("node-2", "topic-2") 138 require.Equal(t, initAck, ack) 139 }