Fix race for rCh in test NetworkManager
@@ -33,6 +33,7 @@ type NetworkManager struct {
t *testing.T
id module.PeerID
rCh chan packetEntry
+ stopCh chan struct{}
// mutable data
peers []Peer
@@ -56,19 +57,26 @@ func NewNetworkManager(t *testing.T, a module.Address) *NetworkManager {
roles: make(map[string]module.Role),
id: network.NewPeerIDFromAddress(a),
rCh: make(chan packetEntry, chLen),
+ stopCh: make(chan struct{}),
}
go n.handlePacketLoop()
return n
}
func (n *NetworkManager) handlePacketLoop() {
- for p := range n.rCh {
+forLoop:
+ for {
+ select {
+ case <-n.stopCh:
+ break forLoop
+ case p := <-n.rCh:
n.handlePacket(p.pk, p.cb)
}
}
+}
func (n *NetworkManager) Close() {
- close(n.rCh)
+ n.stopCh <- struct{}{}
}
func (n *NetworkManager) attach(p Peer) {
