github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/servermaster/service_util_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 servermaster 15 16 import ( 17 "strings" 18 "testing" 19 20 "github.com/golang/mock/gomock" 21 electionMock "github.com/pingcap/tiflow/pkg/election/mock" 22 "github.com/stretchr/testify/require" 23 ) 24 25 func TestGenerateNodeID(t *testing.T) { 26 const ( 27 name = "executor" 28 genCount = 1000 29 minUniqueCount = 999 // Only allow 0.1% of collisions. 30 ) 31 32 ids := make(map[string]struct{}) 33 for i := 0; i < genCount; i++ { 34 id := generateNodeID(name) 35 require.True(t, strings.HasPrefix(id, name+"-")) 36 ids[id] = struct{}{} 37 } 38 39 require.GreaterOrEqual(t, len(ids), minUniqueCount, "too many collisions") 40 } 41 42 func TestFeatureDegrader(t *testing.T) { 43 fd := newFeatureDegrader() 44 require.False(t, fd.Available("ListExecutors")) 45 require.False(t, fd.Available("CreateJob")) 46 require.True(t, fd.Available("QueryMetaStore")) 47 require.True(t, fd.Available("UnknownAPI")) 48 49 fd.updateExecutorManager(true) 50 fd.updateMasterWorkerManager(true) 51 require.True(t, fd.Available("ListExecutors")) 52 require.True(t, fd.Available("CreateJob")) 53 } 54 55 func TestForwardChecker(t *testing.T) { 56 t.Parallel() 57 58 elector := electionMock.NewMockElector(gomock.NewController(t)) 59 fc := newForwardChecker(elector) 60 for method := range leaderOnlyMethods { 61 require.True(t, fc.LeaderOnly(method)) 62 } 63 64 elector.EXPECT().IsLeader().Times(1).Return(true) 65 require.True(t, fc.IsLeader()) 66 elector.EXPECT().IsLeader().Times(1).Return(false) 67 require.False(t, fc.IsLeader()) 68 }