Skip to content

Commit 22f450a

Browse files
authored
Merge pull request #19481 from ahrtr/3.6_lease_20250225
[3.6] Fix TestLeaseGrantTimeToLiveExpired flaky due to leader change
2 parents efcf06a + 6662b3c commit 22f450a

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

tests/common/lease_test.go

+18-2
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func TestLeaseGrantTimeToLiveExpired(t *testing.T) {
122122

123123
for _, tc := range clusterTestCases() {
124124
t.Run(tc.name, func(t *testing.T) {
125-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
125+
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
126126
defer cancel()
127127
clus := testRunner.NewCluster(ctx, t, config.WithClusterConfig(tc.config))
128128
defer clus.Close()
@@ -139,7 +139,23 @@ func TestLeaseGrantTimeToLiveExpired(t *testing.T) {
139139
require.NoError(t, err)
140140
require.Equal(t, int64(1), getResp.Count)
141141

142-
time.Sleep(3 * time.Second)
142+
// When leader changes, old leader steps back to follower
143+
// and may ignore the lease revoking. The new leader will
144+
// restart TTL counting. If so, we should call time.Sleep
145+
// again and wait for revoking. It can't completely avoid
146+
// flaky but reduce flaky possibility.
147+
for i := 0; i < 3; i++ {
148+
currentLeader := clus.WaitLeader(t)
149+
t.Logf("[%d] current leader index %d", i, currentLeader)
150+
151+
time.Sleep(3 * time.Second)
152+
153+
newLeader := clus.WaitLeader(t)
154+
if newLeader == currentLeader {
155+
break
156+
}
157+
t.Logf("[%d] leader changed, new leader index %d", i, newLeader)
158+
}
143159

144160
ttlResp, err := cc.TimeToLive(ctx, leaseResp.ID, config.LeaseOption{})
145161
require.NoError(t, err)

0 commit comments

Comments
 (0)