From 3331300b494f87e7e536c7ca981d1559c2ea6676 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Thu, 22 Oct 2020 18:37:52 +0400 Subject: [PATCH] Cleanup and optimize code in node_test --- node/node_test.go | 67 +++++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 37 deletions(-) diff --git a/node/node_test.go b/node/node_test.go index 6b63326..b05c1e0 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -27,56 +27,49 @@ func TestConsensus(t *testing.T) { //cfg.BootstrapNodeMultiaddr = "/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN" // setup first node - privKey, err := generatePrivateKey() - if err != nil { - t.Error(err) - } - ctx, ctxCancel := context.WithCancel(context.Background()) - node1 := &Node{ - OracleTopic: "dione", - Config: cfg, - GlobalCtx: ctx, - GlobalCtxCancel: ctxCancel, - } - node1.setupNode(ctx, privKey) + node1 := newNode(cfg) // setup second node - privKey, err = generatePrivateKey() - if err != nil { - t.Error(err) - } - ctx, ctxCancel = context.WithCancel(context.Background()) cfg.ListenPort = "1235" cfg.Bootstrap = false cfg.BootstrapNodeMultiaddr = node1.Host.Addrs()[0].String() + fmt.Sprintf("/p2p/%s", node1.Host.ID().String()) - - node2 := &Node{ - OracleTopic: "dione", - Config: cfg, - GlobalCtx: ctx, - GlobalCtxCancel: ctxCancel, - } - node2.setupNode(ctx, privKey) + node2 := newNode(cfg) // setup third node - privKey, err = generatePrivateKey() - if err != nil { - t.Error(err) - } - ctx, ctxCancel = context.WithCancel(context.Background()) cfg.ListenPort = "1236" - node3 := &Node{ - OracleTopic: "dione", - Config: cfg, - GlobalCtx: ctx, - GlobalCtxCancel: ctxCancel, - } - node3.setupNode(ctx, privKey) + node3 := newNode(cfg) + + cfg.ListenPort = "1237" + node4 := newNode(cfg) + cfg.ListenPort = "1238" + node5 := newNode(cfg) + cfg.ListenPort = "1239" + node6 := newNode(cfg) time.Sleep(10 * time.Second) go node2.ConsensusManager.NewTestConsensus("test", "123") go node1.ConsensusManager.NewTestConsensus("test1", "123") go node3.ConsensusManager.NewTestConsensus("test", "123") + go node4.ConsensusManager.NewTestConsensus("test1", "123") + go node5.ConsensusManager.NewTestConsensus("test", "123") + go node6.ConsensusManager.NewTestConsensus("test2", "123") select{} +} + +func newNode(cfg *config.Config) *Node { + privKey, err := generatePrivateKey() + if err != nil { + logrus.Fatal(err) + } + ctx, ctxCancel := context.WithCancel(context.Background()) + + node := &Node{ + OracleTopic: "dione", + Config: cfg, + GlobalCtx: ctx, + GlobalCtxCancel: ctxCancel, + } + node.setupNode(ctx, privKey) + return node } \ No newline at end of file