fix race conditions in tests

This commit is contained in:
Ethan Buchman 2016-06-21 14:35:29 -04:00
parent 5bd7692323
commit 981c6868ad
3 changed files with 10 additions and 6 deletions

View File

@ -87,8 +87,6 @@ type MConnection struct {
}
func NewMConnection(config cfg.Config, conn net.Conn, chDescs []*ChannelDescriptor, onReceive receiveCbFunc, onError errorCbFunc) *MConnection {
setConfigDefaults(config)
mconn := &MConnection{
conn: conn,
bufReader: bufio.NewReaderSize(conn, minReadBufferSize),

View File

@ -181,8 +181,8 @@ func (sw *Switch) OnStop() {
// Stop peers
for _, peer := range sw.peers.List() {
peer.Stop()
sw.peers.Remove(peer)
}
sw.peers = NewPeerSet()
// Stop reactors
for _, reactor := range sw.reactors {
reactor.Stop()

View File

@ -76,6 +76,12 @@ func (tr *TestReactor) Receive(chID byte, peer *Peer, msgBytes []byte) {
}
}
func (tr *TestReactor) getMsgs(chID byte) []PeerMessage {
tr.mtx.Lock()
defer tr.mtx.Unlock()
return tr.msgsReceived[chID]
}
//-----------------------------------------------------------------------------
// convenience method for creating two switches connected to each other.
@ -170,7 +176,7 @@ func TestSwitches(t *testing.T) {
time.Sleep(5000 * time.Millisecond)
// Check message on ch0
ch0Msgs := s2.Reactor("foo").(*TestReactor).msgsReceived[byte(0x00)]
ch0Msgs := s2.Reactor("foo").(*TestReactor).getMsgs(byte(0x00))
if len(ch0Msgs) != 1 {
t.Errorf("Expected to have received 1 message in ch0")
}
@ -179,7 +185,7 @@ func TestSwitches(t *testing.T) {
}
// Check message on ch1
ch1Msgs := s2.Reactor("foo").(*TestReactor).msgsReceived[byte(0x01)]
ch1Msgs := s2.Reactor("foo").(*TestReactor).getMsgs(byte(0x01))
if len(ch1Msgs) != 1 {
t.Errorf("Expected to have received 1 message in ch1")
}
@ -188,7 +194,7 @@ func TestSwitches(t *testing.T) {
}
// Check message on ch2
ch2Msgs := s2.Reactor("bar").(*TestReactor).msgsReceived[byte(0x02)]
ch2Msgs := s2.Reactor("bar").(*TestReactor).getMsgs(byte(0x02))
if len(ch2Msgs) != 1 {
t.Errorf("Expected to have received 1 message in ch2")
}