From ed0e40bdc8fba82b110a865096693b144b125688 Mon Sep 17 00:00:00 2001 From: Wei Fu Date: Mon, 24 Feb 2025 17:33:22 -0500 Subject: [PATCH] tests: deflakey TestLeaseGrantTimeToLiveExpired The following change can reproduce the issue #17504 by `TESTCASE=TestLeaseGrantTimeToLiveExpired/PeerAutoTLS make test-e2e`. We can retry it to reduce flakey possibility, if there is leader change. ```diff diff --git a/tests/common/lease_test.go b/tests/common/lease_test.go index afb27c330..dea8459dc 100644 --- a/tests/common/lease_test.go +++ b/tests/common/lease_test.go @@ -16,6 +16,7 @@ package common import ( "context" + "sync" "testing" "time" @@ -139,6 +140,18 @@ func TestLeaseGrantTimeToLiveExpired(t *testing.T) { require.NoError(t, err) require.Equal(t, int64(1), getResp.Count) + var wg sync.WaitGroup + defer wg.Wait() + + wg.Add(1) + go func() { + defer wg.Done() + time.Sleep(1000 * time.Millisecond) + clus.(interface { + MoveLeader(ctx context.Context, t testing.TB, i int) error + }).MoveLeader(ctx, t, (clus.WaitLeader(t)+1)%len(clus.Members())) + }() + time.Sleep(3 * time.Second) ttlResp, err := cc.TimeToLive(ctx, leaseResp.ID, config.LeaseOption{}) ``` Signed-off-by: Wei Fu --- tests/common/lease_test.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tests/common/lease_test.go b/tests/common/lease_test.go index afb27c330f9..ae83877945a 100644 --- a/tests/common/lease_test.go +++ b/tests/common/lease_test.go @@ -139,7 +139,23 @@ func TestLeaseGrantTimeToLiveExpired(t *testing.T) { require.NoError(t, err) require.Equal(t, int64(1), getResp.Count) - time.Sleep(3 * time.Second) + // FIXME: When leader changes, old leader steps + // back to follower and ignores the lease revoking. + // The new leader will restart TTL counting. If so, + // we should call time.Sleep again and wait for revoking. + // It can't avoid flakey but reduce flakey possiblility. + for i := 0; i < 3; i++ { + currentLeader := clus.WaitLeader(t) + t.Logf("[%d] current leader index %d", i, currentLeader) + + time.Sleep(3 * time.Second) + + newLeader := clus.WaitLeader(t) + if newLeader == currentLeader { + break + } + t.Logf("[%d] leader changed, new leader index %d", i, newLeader) + } ttlResp, err := cc.TimeToLive(ctx, leaseResp.ID, config.LeaseOption{}) require.NoError(t, err)