From 2a1564b5738f7baa7f6f38332cd0d891b2eee52c Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Thu, 3 Jan 2019 18:49:31 -0800 Subject: [PATCH] Add tests for node_handler.go; move stream interface to its own file;create mock for stream.go --- node/worker/worker_test.go | 66 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 node/worker/worker_test.go diff --git a/node/worker/worker_test.go b/node/worker/worker_test.go new file mode 100644 index 000000000..e961dfec3 --- /dev/null +++ b/node/worker/worker_test.go @@ -0,0 +1,66 @@ +package node + +import ( + "github.com/golang/mock/gomock" + "github.com/harmony-one/harmony/consensus" + "github.com/harmony-one/harmony/internal/utils" + "github.com/harmony-one/harmony/p2p" + "github.com/harmony-one/harmony/p2p/p2pimpl" + "testing" +) + +func TestNodeStreamHandler(t *testing.T) { + _, pubKey := utils.GenKey("1", "2") + leader := p2p.Peer{IP: "1", Port: "2", PubKey: pubKey} + validator := p2p.Peer{IP: "3", Port: "5"} + host := p2pimpl.NewHost(leader) + consensus := consensus.New(host, "0", []p2p.Peer{leader, validator}, leader) + node := New(host, consensus, nil) + + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + m := p2p.NewMockStream(ctrl) + + m.EXPECT().Read(gomock.Any()).AnyTimes() + m.EXPECT().SetReadDeadline(gomock.Any()) + m.EXPECT().Close() + + node.StreamHandler(m) +} + +func TestAddNewBlock(t *testing.T) { + _, pubKey := utils.GenKey("1", "2") + leader := p2p.Peer{IP: "1", Port: "2", PubKey: pubKey} + validator := p2p.Peer{IP: "3", Port: "5"} + host := p2pimpl.NewHost(leader) + consensus := consensus.New(host, "0", []p2p.Peer{leader, validator}, leader) + node := New(host, consensus, nil) + + selectedTxs := node.getTransactionsForNewBlock(MaxNumberOfTransactionsPerBlock) + node.Worker.CommitTransactions(selectedTxs) + block, _ := node.Worker.Commit() + + node.AddNewBlock(block) + + if node.blockchain.CurrentBlock().NumberU64() != 1 { + t.Error("New block is not added successfully") + } +} + +func TestVerifyNewBlock(t *testing.T) { + _, pubKey := utils.GenKey("1", "2") + leader := p2p.Peer{IP: "1", Port: "2", PubKey: pubKey} + validator := p2p.Peer{IP: "3", Port: "5"} + host := p2pimpl.NewHost(leader) + consensus := consensus.New(host, "0", []p2p.Peer{leader, validator}, leader) + node := New(host, consensus, nil) + + selectedTxs := node.getTransactionsForNewBlock(MaxNumberOfTransactionsPerBlock) + node.Worker.CommitTransactions(selectedTxs) + block, _ := node.Worker.Commit() + + if !node.VerifyNewBlock(block) { + t.Error("New block is not verified successfully") + } +}