From f71196d1135575c461b624846bec96cc3c7f2e78 Mon Sep 17 00:00:00 2001 From: Danielle Lancashire Date: Thu, 31 Mar 2022 21:17:54 +0200 Subject: [PATCH] tests/common/lease: Wait for correct lease list response MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We don't consistently reach the same etcd server during the lifetime of a test and in some cases, this means that this test will flake if an etcd server was slow to update its state and the test hits the outdated server. Here we switch to using an `Eventually` case which will wait upto a second for the expected result before failing - with a 10ms gap between invocations. ``` [tests(dani/leasefix)] $ gotestsum -- ./common -tags integration -count 100 -timeout 15m -run TestLeaseGrantAndList ✓ common (2m26.71s) DONE 1600 tests in 147.258s ``` --- tests/common/lease_test.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/tests/common/lease_test.go b/tests/common/lease_test.go index 34260c6c287..2b239b2adfe 100644 --- a/tests/common/lease_test.go +++ b/tests/common/lease_test.go @@ -127,20 +127,30 @@ func TestLeaseGrantAndList(t *testing.T) { t.Logf("Created cluster and client") testutils.ExecuteWithTimeout(t, 10*time.Second, func() { createdLeases := []clientv3.LeaseID{} + lastRev := int64(0) for i := 0; i < nc.leaseCount; i++ { leaseResp, err := cc.Grant(10) t.Logf("Grant returned: resp:%s err:%v", leaseResp.String(), err) require.NoError(t, err) createdLeases = append(createdLeases, leaseResp.ID) + lastRev = leaseResp.GetRevision() } - resp, err := cc.LeaseList() - t.Logf("Lease list returned: resp:%s err:%v", resp.String(), err) - require.NoError(t, err) - require.Len(t, resp.Leases, nc.leaseCount) + // Because we're not guarunteed to talk to the same member, wait for + // listing to eventually return true, either by the result propagaing + // or by hitting an up to date member. + leases := []clientv3.LeaseStatus{} + require.Eventually(t, func() bool { + resp, err := cc.LeaseList() + if err != nil { + return false + } + leases = resp.Leases + return resp.GetRevision() >= lastRev + }, 2*time.Second, 10*time.Millisecond) returnedLeases := make([]clientv3.LeaseID, 0, nc.leaseCount) - for _, status := range resp.Leases { + for _, status := range leases { returnedLeases = append(returnedLeases, status.ID) }