github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/election/storage_test.go (about) 1 // Copyright 2022 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 election_test 15 16 import ( 17 "testing" 18 19 "github.com/pingcap/tiflow/pkg/election" 20 "github.com/stretchr/testify/require" 21 ) 22 23 func TestMemberClone(t *testing.T) { 24 t.Parallel() 25 26 m := &election.Member{ 27 ID: "id", 28 Name: "name", 29 } 30 m2 := m.Clone() 31 require.False(t, m == m2) 32 require.Equal(t, m, m2) 33 } 34 35 func TestRecordClone(t *testing.T) { 36 t.Parallel() 37 38 r := &election.Record{ 39 LeaderID: "leader", 40 Members: []*election.Member{ 41 { 42 ID: "id", 43 Name: "name", 44 }, 45 }, 46 } 47 r2 := r.Clone() 48 require.False(t, r == r2) 49 require.Equal(t, r, r2) 50 r.Members[0] = nil 51 require.NotEqual(t, r, r2) 52 } 53 54 func TestRecordFindMember(t *testing.T) { 55 t.Parallel() 56 57 r := &election.Record{ 58 LeaderID: "leader", 59 Members: []*election.Member{ 60 { 61 ID: "id1", 62 Name: "name1", 63 }, 64 { 65 ID: "id2", 66 Name: "name2", 67 }, 68 { 69 ID: "id3", 70 Name: "name3", 71 }, 72 }, 73 } 74 _, ok := r.FindMember("id") 75 require.False(t, ok) 76 77 m, ok := r.FindMember("id2") 78 require.True(t, ok) 79 require.True(t, m == r.Members[1]) 80 }