github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/election/config_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 "context" 18 "testing" 19 "time" 20 21 "github.com/golang/mock/gomock" 22 "github.com/pingcap/tiflow/pkg/election" 23 "github.com/pingcap/tiflow/pkg/election/mock" 24 "github.com/stretchr/testify/require" 25 ) 26 27 func TestConfigAdjustAndValidate(t *testing.T) { 28 t.Parallel() 29 30 err := (&election.Config{ 31 Name: "name1", 32 Storage: mock.NewMockStorage(gomock.NewController(t)), 33 LeaderCallback: func(ctx context.Context) error { 34 return nil 35 }, 36 }).AdjustAndValidate() 37 require.EqualError(t, err, "id must not be empty") 38 39 err = (&election.Config{ 40 ID: "id1", 41 LeaderCallback: func(ctx context.Context) error { 42 return nil 43 }, 44 }).AdjustAndValidate() 45 require.EqualError(t, err, "storage must not be nil") 46 47 err = (&election.Config{ 48 ID: "id1", 49 Name: "name1", 50 Storage: mock.NewMockStorage(gomock.NewController(t)), 51 }).AdjustAndValidate() 52 require.EqualError(t, err, "LeaderCallback must not be nil") 53 54 err = (&election.Config{ 55 ID: "id1", 56 Name: "name1", 57 Storage: mock.NewMockStorage(gomock.NewController(t)), 58 LeaderCallback: func(ctx context.Context) error { 59 return nil 60 }, 61 LeaseDuration: time.Second * 10, 62 RenewInterval: time.Second * 20, 63 }).AdjustAndValidate() 64 require.EqualError(t, err, "RenewInterval must not be greater than LeaseDuration") 65 66 err = (&election.Config{ 67 ID: "id1", 68 Name: "name1", 69 Storage: mock.NewMockStorage(gomock.NewController(t)), 70 LeaderCallback: func(ctx context.Context) error { 71 return nil 72 }, 73 LeaseDuration: time.Second * 15, 74 RenewInterval: time.Second * 10, 75 RenewDeadline: time.Second * 10, 76 }).AdjustAndValidate() 77 require.EqualError(t, err, "RenewDeadline must not be greater than LeaseDuration - RenewInterval") 78 }