raftexample: Add test for adding new node to existing cluster

This commit is contained in:
Shintaro Murakami 2021-02-18 14:17:17 +09:00
parent 1b1be43d65
commit cb0d256a18

View File

@ -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")
}
}