Skip to content

Commit

Permalink
*: make TestConfigTestSuite stable (tikv#7398)
Browse files Browse the repository at this point in the history
close tikv#7395

Signed-off-by: lhy1024 <[email protected]>
  • Loading branch information
lhy1024 authored and rleungx committed Dec 1, 2023
1 parent 2cf969b commit a79d242
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 11 deletions.
88 changes: 88 additions & 0 deletions client/retry/backoff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright 2023 TiKV Project Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package retry

import (
"context"
"time"

"github.com/pingcap/failpoint"
)

// BackOffer is a backoff policy for retrying operations.
type BackOffer struct {
max time.Duration
next time.Duration
base time.Duration
}

// Exec is a helper function to exec backoff.
func (bo *BackOffer) Exec(
ctx context.Context,
fn func() error,
) error {
if err := fn(); err != nil {
after := time.NewTimer(bo.nextInterval())
defer after.Stop()
select {
case <-ctx.Done():
case <-after.C:
failpoint.Inject("backOffExecute", func() {
testBackOffExecuteFlag = true
})
}
return err
}
// reset backoff when fn() succeed.
bo.resetBackoff()
return nil
}

// InitialBackOffer make the initial state for retrying.
func InitialBackOffer(base, max time.Duration) BackOffer {
return BackOffer{
max: max,
base: base,
next: base,
}
}

// nextInterval for now use the `exponentialInterval`.
func (bo *BackOffer) nextInterval() time.Duration {
return bo.exponentialInterval()
}

// exponentialInterval returns the exponential backoff duration.
func (bo *BackOffer) exponentialInterval() time.Duration {
backoffInterval := bo.next
bo.next *= 2
if bo.next > bo.max {
bo.next = bo.max
}
return backoffInterval
}

// resetBackoff resets the backoff to initial state.
func (bo *BackOffer) resetBackoff() {
bo.next = bo.base
}

// Only used for test.
var testBackOffExecuteFlag = false

// TestBackOffExecute Only used for test.
func TestBackOffExecute() bool {
return testBackOffExecuteFlag
}
3 changes: 1 addition & 2 deletions pkg/schedule/operator/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package operator
import (
"context"
"encoding/json"
"fmt"
"sync/atomic"
"testing"
"time"
Expand Down Expand Up @@ -514,7 +513,7 @@ func (suite *operatorTestSuite) TestOpStepTimeout() {
},
}
for i, v := range testData {
fmt.Printf("case:%d\n", i)
suite.T().Logf("case: %d", i)
for _, step := range v.step {
suite.Equal(v.expect, step.Timeout(v.regionSize))
}
Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/mcs/tso/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (suite *tsoServerTestSuite) TearDownSuite() {
func (suite *tsoServerTestSuite) TestTSOServerStartAndStopNormally() {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered from an unexpected panic", r)
suite.T().Log("Recovered from an unexpected panic", r)
suite.T().Errorf("Expected no panic, but something bad occurred with")
}
}()
Expand Down
5 changes: 3 additions & 2 deletions tests/pdctl/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,6 @@ func (suite *configTestSuite) checkPDServerConfig(cluster *tests.TestCluster) {
LastHeartbeat: time.Now().UnixNano(),
}
tests.MustPutStore(re, cluster, store)
defer cluster.Destroy()

output, err := pdctl.ExecuteCommand(cmd, "-u", pdAddr, "config", "show", "server")
re.NoError(err)
Expand All @@ -844,7 +843,9 @@ func (suite *configTestSuite) checkPDServerConfig(cluster *tests.TestCluster) {
re.Equal("table", conf.KeyType)
re.Equal(typeutil.StringSlice([]string{}), conf.RuntimeServices)
re.Equal("", conf.MetricStorage)
re.Equal("auto", conf.DashboardAddress)
if conf.DashboardAddress != "auto" { // dashboard has been assigned
re.Equal(leaderServer.GetAddr(), conf.DashboardAddress)
}
re.Equal(int(3), conf.FlowRoundByDigit)
}

Expand Down
1 change: 0 additions & 1 deletion tests/server/api/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,6 @@ func (suite *regionRuleTestSuite) checkRegionPlacementRule(cluster *tests.TestCl
var label labeler.LabelRule
escapedID := url.PathEscape("keyspaces/0")
u = fmt.Sprintf("%s/config/region-label/rule/%s", urlPrefix, escapedID)
fmt.Println("u====", u)
err = tu.ReadGetJSON(re, testDialClient, u, &label)
suite.NoError(err)
suite.Equal(label.ID, "keyspaces/0")
Expand Down
6 changes: 3 additions & 3 deletions tests/server/cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1288,7 +1288,7 @@ func TestTransferLeaderForScheduler(t *testing.T) {
re.NoError(err)
tc.WaitLeader()
// start
leaderServer := tc.GetServer(tc.GetLeader())
leaderServer := tc.GetLeaderServer()
re.NoError(leaderServer.BootstrapCluster())
rc := leaderServer.GetServer().GetRaftCluster()
re.NotNil(rc)
Expand Down Expand Up @@ -1327,7 +1327,7 @@ func TestTransferLeaderForScheduler(t *testing.T) {
tc.ResignLeader()
rc.Stop()
tc.WaitLeader()
leaderServer = tc.GetServer(tc.GetLeader())
leaderServer = tc.GetLeaderServer()
rc1 := leaderServer.GetServer().GetRaftCluster()
rc1.Start(leaderServer.GetServer())
re.NoError(err)
Expand All @@ -1347,7 +1347,7 @@ func TestTransferLeaderForScheduler(t *testing.T) {
tc.ResignLeader()
rc1.Stop()
tc.WaitLeader()
leaderServer = tc.GetServer(tc.GetLeader())
leaderServer = tc.GetLeaderServer()
rc = leaderServer.GetServer().GetRaftCluster()
rc.Start(leaderServer.GetServer())
re.NotNil(rc)
Expand Down
2 changes: 1 addition & 1 deletion tests/server/member/member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func TestPDLeaderLostWhileEtcdLeaderIntact(t *testing.T) {
re.NoError(err)

leader1 := cluster.WaitLeader()
memberID := cluster.GetServer(leader1).GetLeader().GetMemberId()
memberID := cluster.GetLeaderServer().GetLeader().GetMemberId()

re.NoError(failpoint.Enable("github.com/tikv/pd/server/leaderLoopCheckAgain", fmt.Sprintf("return(\"%d\")", memberID)))
re.NoError(failpoint.Enable("github.com/tikv/pd/server/exitCampaignLeader", fmt.Sprintf("return(\"%d\")", memberID)))
Expand Down
2 changes: 1 addition & 1 deletion tests/server/region_syncer/region_syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func TestPrepareCheckerWithTransferLeader(t *testing.T) {
err = cluster.RunInitialServers()
re.NoError(err)
cluster.WaitLeader()
leaderServer := cluster.GetServer(cluster.GetLeader())
leaderServer := cluster.GetLeaderServer()
re.NoError(leaderServer.BootstrapCluster())
rc := leaderServer.GetServer().GetRaftCluster()
re.NotNil(rc)
Expand Down

0 comments on commit a79d242

Please sign in to comment.