Browse Source

fix race conditions in tests

pull/456/head
Ethan Buchman 9 years ago
parent
commit
981c6868ad
3 changed files with 10 additions and 6 deletions
  1. +0
    -2
      connection.go
  2. +1
    -1
      switch.go
  3. +9
    -3
      switch_test.go

+ 0
- 2
connection.go 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),


+ 1
- 1
switch.go 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()


+ 9
- 3
switch_test.go 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")
}


Loading…
Cancel
Save