Cleanup and optimize code in node_test

This commit is contained in:
ChronosX88 2020-10-22 18:37:52 +04:00
parent 97a4e7a7ad
commit 3331300b49
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A

View File

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