From cb0d256a188fb5d902c8043f1bef911e83aff237 Mon Sep 17 00:00:00 2001 From: Shintaro Murakami Date: Thu, 18 Feb 2021 14:17:17 +0900 Subject: [PATCH] raftexample: Add test for adding new node to existing cluster --- contrib/raftexample/raftexample_test.go | 36 +++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/contrib/raftexample/raftexample_test.go b/contrib/raftexample/raftexample_test.go index 7643f77fd..9c11e7d61 100644 --- a/contrib/raftexample/raftexample_test.go +++ b/contrib/raftexample/raftexample_test.go @@ -205,3 +205,39 @@ func TestPutAndGetKeyValue(t *testing.T) { t.Fatalf("expect %s, got %s", wantValue, gotValue) } } + +// TestAddNewNode tests adding new node to the existing cluster. +func TestAddNewNode(t *testing.T) { + clus := newCluster(3) + defer clus.closeNoErrors(t) + + os.RemoveAll("raftexample-4") + os.RemoveAll("raftexample-4-snap") + defer func() { + os.RemoveAll("raftexample-4") + os.RemoveAll("raftexample-4-snap") + }() + + newNodeURL := "http://127.0.0.1:10004" + clus.confChangeC[0] <- raftpb.ConfChange{ + Type: raftpb.ConfChangeAddNode, + NodeID: 4, + Context: []byte(newNodeURL), + } + + proposeC := make(chan string) + defer close(proposeC) + + confChangeC := make(chan raftpb.ConfChange) + defer close(confChangeC) + + newRaftNode(4, append(clus.peers, newNodeURL), true, nil, proposeC, confChangeC) + + go func() { + proposeC <- "foo" + }() + + if c, ok := <-clus.commitC[0]; !ok || c.data[0] != "foo" { + t.Fatalf("Commit failed") + } +}