Browse code

Test: wait for network changes in TestNetworkDBNodeJoinLeaveIteration

In network node change test, the expected behavior is focused on how many nodes
left in networkDB, besides timing issues, things would also go tricky for a
leave-then-join sequence, if the check (counting the nodes) happened before the
first "leave" event, then the testcase actually miss its target and report PASS
without verifying its final result; if the check happened after the 'leave' event,
but before the 'join' event, the test would report FAIL unnecessary;

This code change would check both the db changes and the node count, it would
report PASS only when networkdb has indeed changed and the node count is expected.

Signed-off-by: David Wang <00107082@163.com>

David Wang authored on 2022/07/21 23:14:07
Showing 1 changed files
... ...
@@ -13,6 +13,7 @@ import (
13 13
 	"github.com/docker/docker/pkg/stringid"
14 14
 	"github.com/docker/go-events"
15 15
 	"github.com/hashicorp/memberlist"
16
+	"github.com/hashicorp/serf/serf"
16 17
 	"github.com/sirupsen/logrus"
17 18
 	"gotest.tools/v3/assert"
18 19
 	is "gotest.tools/v3/assert/cmp"
... ...
@@ -479,22 +480,47 @@ func TestNetworkDBCRUDMediumCluster(t *testing.T) {
479 479
 func TestNetworkDBNodeJoinLeaveIteration(t *testing.T) {
480 480
 	dbs := createNetworkDBInstances(t, 2, "node", DefaultConfig())
481 481
 
482
+	var (
483
+		dbIndex          int32
484
+		staleNetworkTime [2]serf.LamportTime
485
+		expectNodeCount  int
486
+		network          = "network1"
487
+	)
488
+	dbChangeWitness := func(t poll.LogT) poll.Result {
489
+		db := dbs[dbIndex]
490
+		networkTime := db.networkClock.Time()
491
+		if networkTime <= staleNetworkTime[dbIndex] {
492
+			return poll.Continue("network time is stale, no change registered yet.")
493
+		}
494
+		count := -1
495
+		db.Lock()
496
+		if nodes, ok := db.networkNodes[network]; ok {
497
+			count = len(nodes)
498
+		}
499
+		db.Unlock()
500
+		if count != expectNodeCount {
501
+			return poll.Continue("current number of nodes is %d, expect %d.", count, expectNodeCount)
502
+		}
503
+		return poll.Success()
504
+	}
505
+
482 506
 	// Single node Join/Leave
507
+	staleNetworkTime[0], staleNetworkTime[1] = dbs[0].networkClock.Time(), dbs[1].networkClock.Time()
483 508
 	err := dbs[0].JoinNetwork("network1")
484 509
 	assert.NilError(t, err)
485 510
 
486
-	if len(dbs[0].networkNodes["network1"]) != 1 {
487
-		t.Fatalf("The networkNodes list has to have be 1 instead of %d", len(dbs[0].networkNodes["network1"]))
488
-	}
511
+	dbIndex, expectNodeCount = 0, 1
512
+	poll.WaitOn(t, dbChangeWitness, poll.WithTimeout(3*time.Second), poll.WithDelay(5*time.Millisecond))
489 513
 
514
+	staleNetworkTime[0], staleNetworkTime[1] = dbs[0].networkClock.Time(), dbs[1].networkClock.Time()
490 515
 	err = dbs[0].LeaveNetwork("network1")
491 516
 	assert.NilError(t, err)
492 517
 
493
-	if len(dbs[0].networkNodes["network1"]) != 0 {
494
-		t.Fatalf("The networkNodes list has to have be 0 instead of %d", len(dbs[0].networkNodes["network1"]))
495
-	}
518
+	dbIndex, expectNodeCount = 0, 0
519
+	poll.WaitOn(t, dbChangeWitness, poll.WithTimeout(3*time.Second), poll.WithDelay(5*time.Millisecond))
496 520
 
497 521
 	// Multiple nodes Join/Leave
522
+	staleNetworkTime[0], staleNetworkTime[1] = dbs[0].networkClock.Time(), dbs[1].networkClock.Time()
498 523
 	err = dbs[0].JoinNetwork("network1")
499 524
 	assert.NilError(t, err)
500 525
 
... ...
@@ -503,37 +529,34 @@ func TestNetworkDBNodeJoinLeaveIteration(t *testing.T) {
503 503
 
504 504
 	// Wait for the propagation on db[0]
505 505
 	dbs[0].verifyNetworkExistence(t, dbs[1].config.NodeID, "network1", true)
506
-	if len(dbs[0].networkNodes["network1"]) != 2 {
507
-		t.Fatalf("The networkNodes list has to have be 2 instead of %d - %v", len(dbs[0].networkNodes["network1"]), dbs[0].networkNodes["network1"])
508
-	}
506
+	dbIndex, expectNodeCount = 0, 2
507
+	poll.WaitOn(t, dbChangeWitness, poll.WithTimeout(3*time.Second), poll.WithDelay(5*time.Millisecond))
509 508
 	if n, ok := dbs[0].networks[dbs[0].config.NodeID]["network1"]; !ok || n.leaving {
510 509
 		t.Fatalf("The network should not be marked as leaving:%t", n.leaving)
511 510
 	}
512 511
 
513 512
 	// Wait for the propagation on db[1]
514 513
 	dbs[1].verifyNetworkExistence(t, dbs[0].config.NodeID, "network1", true)
515
-	if len(dbs[1].networkNodes["network1"]) != 2 {
516
-		t.Fatalf("The networkNodes list has to have be 2 instead of %d - %v", len(dbs[1].networkNodes["network1"]), dbs[1].networkNodes["network1"])
517
-	}
514
+	dbIndex, expectNodeCount = 1, 2
515
+	poll.WaitOn(t, dbChangeWitness, poll.WithTimeout(3*time.Second), poll.WithDelay(5*time.Millisecond))
518 516
 	if n, ok := dbs[1].networks[dbs[1].config.NodeID]["network1"]; !ok || n.leaving {
519 517
 		t.Fatalf("The network should not be marked as leaving:%t", n.leaving)
520 518
 	}
521 519
 
522 520
 	// Try a quick leave/join
521
+	staleNetworkTime[0], staleNetworkTime[1] = dbs[0].networkClock.Time(), dbs[1].networkClock.Time()
523 522
 	err = dbs[0].LeaveNetwork("network1")
524 523
 	assert.NilError(t, err)
525 524
 	err = dbs[0].JoinNetwork("network1")
526 525
 	assert.NilError(t, err)
527 526
 
528 527
 	dbs[0].verifyNetworkExistence(t, dbs[1].config.NodeID, "network1", true)
529
-	if len(dbs[0].networkNodes["network1"]) != 2 {
530
-		t.Fatalf("The networkNodes list has to have be 2 instead of %d - %v", len(dbs[0].networkNodes["network1"]), dbs[0].networkNodes["network1"])
531
-	}
528
+	dbIndex, expectNodeCount = 0, 2
529
+	poll.WaitOn(t, dbChangeWitness, poll.WithTimeout(3*time.Second), poll.WithDelay(5*time.Millisecond))
532 530
 
533 531
 	dbs[1].verifyNetworkExistence(t, dbs[0].config.NodeID, "network1", true)
534
-	if len(dbs[1].networkNodes["network1"]) != 2 {
535
-		t.Fatalf("The networkNodes list has to have be 2 instead of %d - %v", len(dbs[1].networkNodes["network1"]), dbs[1].networkNodes["network1"])
536
-	}
532
+	dbIndex, expectNodeCount = 1, 2
533
+	poll.WaitOn(t, dbChangeWitness, poll.WithTimeout(3*time.Second), poll.WithDelay(5*time.Millisecond))
537 534
 
538 535
 	closeNetworkDBInstances(t, dbs)
539 536
 }