@@ -122,7 +122,7 @@ func TestLeaseGrantTimeToLiveExpired(t *testing.T) {
122
122
123
123
for _ , tc := range clusterTestCases () {
124
124
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 )
126
126
defer cancel ()
127
127
clus := testRunner .NewCluster (ctx , t , config .WithClusterConfig (tc .config ))
128
128
defer clus .Close ()
@@ -139,7 +139,23 @@ func TestLeaseGrantTimeToLiveExpired(t *testing.T) {
139
139
require .NoError (t , err )
140
140
require .Equal (t , int64 (1 ), getResp .Count )
141
141
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
+ }
143
159
144
160
ttlResp , err := cc .TimeToLive (ctx , leaseResp .ID , config.LeaseOption {})
145
161
require .NoError (t , err )
0 commit comments