swarm/network: wait for subs in PeerEvents and fix stream.runSyncTest

This commit is contained in:
Janos Guljas 2018-07-20 14:46:53 +02:00 committed by Fabio Barone
parent bc42faee0a
commit bf9c27a166
2 changed files with 19 additions and 11 deletions

View file

@ -18,6 +18,7 @@ package simulation
import ( import (
"context" "context"
"sync"
"github.com/ethereum/go-ethereum/p2p/discover" "github.com/ethereum/go-ethereum/p2p/discover"
@ -71,24 +72,32 @@ func (f *PeerEventsFilter) MsgCode(c uint64) *PeerEventsFilter {
func (s *Simulation) PeerEvents(ctx context.Context, ids []discover.NodeID, filters ...*PeerEventsFilter) <-chan PeerEvent { func (s *Simulation) PeerEvents(ctx context.Context, ids []discover.NodeID, filters ...*PeerEventsFilter) <-chan PeerEvent {
eventC := make(chan PeerEvent) eventC := make(chan PeerEvent)
// wait group to make sure all subscriptons to admin peerEvents are established
// before this function returns.
var subsWG sync.WaitGroup
for _, id := range ids { for _, id := range ids {
s.shutdownWG.Add(1) s.shutdownWG.Add(1)
subsWG.Add(1)
go func(id discover.NodeID) { go func(id discover.NodeID) {
defer s.shutdownWG.Done() defer s.shutdownWG.Done()
client, err := s.Net.GetNode(id).Client() client, err := s.Net.GetNode(id).Client()
if err != nil { if err != nil {
subsWG.Done()
eventC <- PeerEvent{NodeID: id, Error: err} eventC <- PeerEvent{NodeID: id, Error: err}
return return
} }
events := make(chan *p2p.PeerEvent) events := make(chan *p2p.PeerEvent)
sub, err := client.Subscribe(ctx, "admin", events, "peerEvents") sub, err := client.Subscribe(ctx, "admin", events, "peerEvents")
if err != nil { if err != nil {
subsWG.Done()
eventC <- PeerEvent{NodeID: id, Error: err} eventC <- PeerEvent{NodeID: id, Error: err}
return return
} }
defer sub.Unsubscribe() defer sub.Unsubscribe()
subsWG.Done()
for { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
@ -153,5 +162,7 @@ func (s *Simulation) PeerEvents(ctx context.Context, ids []discover.NodeID, filt
}(id) }(id)
} }
// wait all subscriptions
subsWG.Wait()
return eventC return eventC
} }

View file

@ -384,18 +384,15 @@ func runSyncTest(chunkCount int, nodeCount int) error {
subscriptionCount += cnt subscriptionCount += cnt
} }
go func() {
for e := range eventC { for e := range eventC {
if e.Error != nil { if e.Error != nil {
//return e.Error return e.Error
return
} }
subscriptionCount-- subscriptionCount--
if subscriptionCount == 0 { if subscriptionCount == 0 {
break break
} }
} }
}()
//get the the node at that index //get the the node at that index
//this is the node selected for upload //this is the node selected for upload
node := sim.RandomUpNode() node := sim.RandomUpNode()