diff options
author | Jimmy Hu <jimmy.hu@dexon.org> | 2018-12-08 16:33:42 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-08 16:33:42 +0800 |
commit | 7654e95f94f4902eb624bcdae1e6c8ebbf38e289 (patch) | |
tree | 5c50289fe6305507d71d72d96abbfe2d4b584d0a /core/consensus_test.go | |
parent | 1b352d9e52839c8b6c316c2601d08c91c995d8f0 (diff) | |
download | dexon-consensus-7654e95f94f4902eb624bcdae1e6c8ebbf38e289.tar dexon-consensus-7654e95f94f4902eb624bcdae1e6c8ebbf38e289.tar.gz dexon-consensus-7654e95f94f4902eb624bcdae1e6c8ebbf38e289.tar.bz2 dexon-consensus-7654e95f94f4902eb624bcdae1e6c8ebbf38e289.tar.lz dexon-consensus-7654e95f94f4902eb624bcdae1e6c8ebbf38e289.tar.xz dexon-consensus-7654e95f94f4902eb624bcdae1e6c8ebbf38e289.tar.zst dexon-consensus-7654e95f94f4902eb624bcdae1e6c8ebbf38e289.zip |
ci: Add TravisCI setting. (#363)
* Update Gopkg.lock
* Fix test
* Add travisCI setting
* Print log using fmt
* Update GNUmakefile
* Use single go rountine for consensus_test
Diffstat (limited to 'core/consensus_test.go')
-rw-r--r-- | core/consensus_test.go | 72 |
1 files changed, 48 insertions, 24 deletions
diff --git a/core/consensus_test.go b/core/consensus_test.go index 9234512..65f4e36 100644 --- a/core/consensus_test.go +++ b/core/consensus_test.go @@ -18,17 +18,19 @@ package core import ( + "encoding/json" "sort" "testing" "time" + "github.com/stretchr/testify/suite" + "github.com/dexon-foundation/dexon-consensus/common" "github.com/dexon-foundation/dexon-consensus/core/blockdb" "github.com/dexon-foundation/dexon-consensus/core/crypto" "github.com/dexon-foundation/dexon-consensus/core/test" "github.com/dexon-foundation/dexon-consensus/core/types" typesDKG "github.com/dexon-foundation/dexon-consensus/core/types/dkg" - "github.com/stretchr/testify/suite" ) // network implements core.Network. @@ -101,37 +103,33 @@ func (nc *networkConnection) broadcast(from types.NodeID, msg interface{}) { } func (nc *networkConnection) send(to types.NodeID, msg interface{}) { - con, exist := nc.cons[to] + ch, exist := nc.cons[to] if !exist { return } - go func() { - var err error - // Testify package does not support concurrent call. - // Use panic() to detact error. - switch val := msg.(type) { - case *types.Block: - err = con.preProcessBlock(val) - case *types.Vote: - err = con.ProcessVote(val) - case *types.AgreementResult: - err = con.ProcessAgreementResult(val) - case *types.BlockRandomnessResult: - err = con.ProcessBlockRandomnessResult(val) - case *typesDKG.PrivateShare: - err = con.cfgModule.processPrivateShare(val) - case *typesDKG.PartialSignature: - err = con.cfgModule.processPartialSignature(val) - } + msgCopy := msg + // Clone msg if necessary. + switch val := msg.(type) { + case *types.Block: + msgCopy = val.Clone() + case *typesDKG.PrivateShare: + // Use Marshal/Unmarshal to do deep copy. + data, err := json.Marshal(val) if err != nil { panic(err) } - }() + valCopy := &typesDKG.PrivateShare{} + if err := json.Unmarshal(data, valCopy); err != nil { + panic(err) + } + msgCopy = valCopy + } + ch <- msgCopy } type networkConnection struct { s *ConsensusTestSuite - cons map[types.NodeID]*Consensus + cons map[types.NodeID]chan interface{} } func (nc *networkConnection) newNetwork(nID types.NodeID) *network { @@ -142,7 +140,33 @@ func (nc *networkConnection) newNetwork(nID types.NodeID) *network { } func (nc *networkConnection) setCon(nID types.NodeID, con *Consensus) { - nc.cons[nID] = con + ch := make(chan interface{}, 1000) + go func() { + for { + msg := <-ch + var err error + // Testify package does not support concurrent call. + // Use panic() to detact error. + switch val := msg.(type) { + case *types.Block: + err = con.preProcessBlock(val) + case *types.Vote: + err = con.ProcessVote(val) + case *types.AgreementResult: + err = con.ProcessAgreementResult(val) + case *types.BlockRandomnessResult: + err = con.ProcessBlockRandomnessResult(val) + case *typesDKG.PrivateShare: + err = con.cfgModule.processPrivateShare(val) + case *typesDKG.PartialSignature: + err = con.cfgModule.processPartialSignature(val) + } + if err != nil { + panic(err) + } + } + }() + nc.cons[nID] = ch } type ConsensusTestSuite struct { @@ -153,7 +177,7 @@ type ConsensusTestSuite struct { func (s *ConsensusTestSuite) newNetworkConnection() *networkConnection { return &networkConnection{ s: s, - cons: make(map[types.NodeID]*Consensus), + cons: make(map[types.NodeID]chan interface{}), } } |