From 8fbba73f42e6bf36b2a6e6a4c13e2635a8277cdb Mon Sep 17 00:00:00 2001 From: Roman Volosatovs Date: Sun, 1 Aug 2021 17:04:01 +0200 Subject: [PATCH 1/2] libnetwork: wait until t.Deadline() instead of hardcoded value Signed-off-by: Roman Volosatovs --- libnetwork/networkdb/networkdb_test.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/libnetwork/networkdb/networkdb_test.go b/libnetwork/networkdb/networkdb_test.go index 3fa19b5488..4bfb4bcf63 100644 --- a/libnetwork/networkdb/networkdb_test.go +++ b/libnetwork/networkdb/networkdb_test.go @@ -99,7 +99,15 @@ func (db *NetworkDB) verifyNodeExistence(t *testing.T, node string, present bool func (db *NetworkDB) verifyNetworkExistence(t *testing.T, node string, id string, present bool) { t.Helper() - for i := 0; i < 80; i++ { + + const sleepInterval = 50 * time.Millisecond + var maxRetries int64 + if dl, ok := t.Deadline(); ok { + maxRetries = int64(time.Until(dl) / sleepInterval) + } else { + maxRetries = 80 + } + for i := int64(0); i < maxRetries; i++ { db.RLock() nn, nnok := db.networks[node] db.RUnlock() @@ -116,7 +124,7 @@ func (db *NetworkDB) verifyNetworkExistence(t *testing.T, node string, id string } } - time.Sleep(50 * time.Millisecond) + time.Sleep(sleepInterval) } t.Error("Network existence verification failed") From b8215904618f4d249c81d4db7defb4a3e0e0caff Mon Sep 17 00:00:00 2001 From: Roman Volosatovs Date: Sun, 1 Aug 2021 17:04:48 +0200 Subject: [PATCH 2/2] libnetwork/networkdb: consistently wait for nodes in tests Use `verifyNetworkExistence` like it was done in 2837fba75f5ee7e57c167af4e70569adaf59377a Signed-off-by: Roman Volosatovs --- libnetwork/networkdb/networkdb_test.go | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/libnetwork/networkdb/networkdb_test.go b/libnetwork/networkdb/networkdb_test.go index 4bfb4bcf63..30c775389e 100644 --- a/libnetwork/networkdb/networkdb_test.go +++ b/libnetwork/networkdb/networkdb_test.go @@ -481,7 +481,6 @@ func TestNetworkDBCRUDMediumCluster(t *testing.T) { } func TestNetworkDBNodeJoinLeaveIteration(t *testing.T) { - maxRetry := 5 dbs := createNetworkDBInstances(t, 2, "node", DefaultConfig()) // Single node Join/Leave @@ -530,22 +529,12 @@ func TestNetworkDBNodeJoinLeaveIteration(t *testing.T) { err = dbs[0].JoinNetwork("network1") assert.NilError(t, err) - for i := 0; i < maxRetry; i++ { - if len(dbs[0].networkNodes["network1"]) == 2 { - break - } - time.Sleep(1 * time.Second) - } + dbs[0].verifyNetworkExistence(t, dbs[1].config.NodeID, "network1", true) if len(dbs[0].networkNodes["network1"]) != 2 { t.Fatalf("The networkNodes list has to have be 2 instead of %d - %v", len(dbs[0].networkNodes["network1"]), dbs[0].networkNodes["network1"]) } - for i := 0; i < maxRetry; i++ { - if len(dbs[1].networkNodes["network1"]) == 2 { - break - } - time.Sleep(1 * time.Second) - } + dbs[1].verifyNetworkExistence(t, dbs[0].config.NodeID, "network1", true) if len(dbs[1].networkNodes["network1"]) != 2 { t.Fatalf("The networkNodes list has to have be 2 instead of %d - %v", len(dbs[1].networkNodes["network1"]), dbs[1].networkNodes["network1"]) }