Skip to content

Commit 0e49f89

Browse files
sinadarbouyhamedsalim1999
authored andcommitted
test: enhance TestAddPeer to verify peer addition for leader and follower nodes
- Updated TestAddPeer to include checks for adding peers when the node is a leader and a follower. - Introduced temporary directories for each node to ensure isolated testing environments. - Added assertions to confirm that both new peers are successfully integrated into the cluster. - Improved test reliability by implementing a loop to wait for both nodes to join the cluster before completing the test.
1 parent 04d26a3 commit 0e49f89

File tree

1 file changed

+48
-9
lines changed

1 file changed

+48
-9
lines changed

raft/raft_test.go

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -544,29 +544,31 @@ func TestGetHealthStatus(t *testing.T) {
544544

545545
func TestAddPeer(t *testing.T) {
546546
logger := setupTestLogger()
547-
tempDir := t.TempDir()
548547

549-
nodeconfig1 := config.Raft{
548+
tempDir := t.TempDir()
549+
nodeConfig1 := config.Raft{
550550
NodeID: "testAddPeerNode1",
551551
Address: "127.0.0.1:5679",
552552
IsBootstrap: true,
553553
Directory: tempDir,
554554
GRPCAddress: "127.0.0.1:5680",
555555
}
556556

557-
node1, err := NewRaftNode(logger, nodeconfig1)
557+
node1, err := NewRaftNode(logger, nodeConfig1)
558558
require.NoError(t, err)
559559
defer func() {
560560
_ = node1.Shutdown()
561561
}()
562562

563563
time.Sleep(2 * time.Second)
564564

565+
// Add node2 to the cluster when peer is leader
566+
tempDir2 := t.TempDir()
565567
nodeConfig2 := config.Raft{
566568
NodeID: "testAddPeerNode2",
567569
Address: "127.0.0.1:5689",
568570
IsBootstrap: false,
569-
Directory: tempDir,
571+
Directory: tempDir2,
570572
GRPCAddress: "127.0.0.1:5690",
571573
Peers: []config.RaftPeer{
572574
{ID: "testAddPeerNode1", Address: "127.0.0.1:5679", GRPCAddress: "127.0.0.1:5680"},
@@ -579,14 +581,51 @@ func TestAddPeer(t *testing.T) {
579581
_ = node2.Shutdown()
580582
}()
581583

582-
for {
584+
// Add node3 to the cluster when peer is follower
585+
tempDir3 := t.TempDir()
586+
nodeConfig3 := config.Raft{
587+
NodeID: "testAddPeerNode3",
588+
Address: "127.0.0.1:5699",
589+
IsBootstrap: false,
590+
Directory: tempDir3,
591+
GRPCAddress: "127.0.0.1:5700",
592+
Peers: []config.RaftPeer{
593+
{ID: "testAddPeerNode1", Address: "127.0.0.1:5689", GRPCAddress: "127.0.0.1:5690"},
594+
},
595+
}
596+
597+
node3, err := NewRaftNode(logger, nodeConfig3)
598+
require.NoError(t, err)
599+
defer func() {
600+
_ = node3.Shutdown()
601+
}()
602+
603+
// Function to check if a node is in the cluster configuration
604+
checkNodeInCluster := func(nodeID string) bool {
583605
existingConfig := node1.raft.GetConfiguration().Configuration()
584606
for _, server := range existingConfig.Servers {
585-
if server.ID == raft.ServerID(nodeConfig2.NodeID) {
586-
assert.True(t, true)
587-
return
607+
if server.ID == raft.ServerID(nodeID) {
608+
return true
588609
}
589610
}
590-
time.Sleep(1 * time.Second)
611+
return false
591612
}
613+
614+
// Wait and verify that both nodes join the cluster
615+
require.Eventually(t, func() bool {
616+
return checkNodeInCluster(nodeConfig2.NodeID) && checkNodeInCluster(nodeConfig3.NodeID)
617+
}, 30*time.Second, 1*time.Second, "Nodes failed to join the cluster")
618+
619+
// Verify the cluster has exactly 3 nodes
620+
existingConfig := node1.raft.GetConfiguration().Configuration()
621+
assert.Equal(t, 3, len(existingConfig.Servers), "Cluster should have exactly 3 nodes")
622+
623+
// Verify that node2 and node3 recognize node1 as the leader
624+
time.Sleep(2 * time.Second) // Give some time for leader recognition
625+
626+
_, leaderID := node2.raft.LeaderWithID()
627+
assert.Equal(t, raft.ServerID(nodeConfig1.NodeID), leaderID, "Node2 should recognize Node1 as leader")
628+
629+
_, leaderID = node3.raft.LeaderWithID()
630+
assert.Equal(t, raft.ServerID(nodeConfig1.NodeID), leaderID, "Node3 should recognize Node1 as leader")
592631
}

0 commit comments

Comments
 (0)