Ver Fonte

Merge pull request #42703 from rvolosatovs/fix_network_db_test

libnetwork: wait until t.Deadline() instead of hardcoded value
Sebastiaan van Stijn há 3 anos atrás
pai
commit
7672963eec
1 ficheiros alterados com 12 adições e 15 exclusões
  1. 12 15
      libnetwork/networkdb/networkdb_test.go

+ 12 - 15
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) {
 func (db *NetworkDB) verifyNetworkExistence(t *testing.T, node string, id string, present bool) {
 	t.Helper()
 	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()
 		db.RLock()
 		nn, nnok := db.networks[node]
 		nn, nnok := db.networks[node]
 		db.RUnlock()
 		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")
 	t.Error("Network existence verification failed")
@@ -473,7 +481,6 @@ func TestNetworkDBCRUDMediumCluster(t *testing.T) {
 }
 }
 
 
 func TestNetworkDBNodeJoinLeaveIteration(t *testing.T) {
 func TestNetworkDBNodeJoinLeaveIteration(t *testing.T) {
-	maxRetry := 5
 	dbs := createNetworkDBInstances(t, 2, "node", DefaultConfig())
 	dbs := createNetworkDBInstances(t, 2, "node", DefaultConfig())
 
 
 	// Single node Join/Leave
 	// Single node Join/Leave
@@ -522,22 +529,12 @@ func TestNetworkDBNodeJoinLeaveIteration(t *testing.T) {
 	err = dbs[0].JoinNetwork("network1")
 	err = dbs[0].JoinNetwork("network1")
 	assert.NilError(t, err)
 	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 {
 	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"])
 		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 {
 	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"])
 		t.Fatalf("The networkNodes list has to have be 2 instead of %d - %v", len(dbs[1].networkNodes["network1"]), dbs[1].networkNodes["network1"])
 	}
 	}