swarm/network/stream: add concurrent safe bool for watchDisconnections

This commit is contained in:
Janos Guljas 2019-02-12 15:59:51 +01:00
parent 3710a722b3
commit 714d871faa
5 changed files with 36 additions and 22 deletions

View file

@ -323,13 +323,14 @@ func createTestLocalStorageForID(id enode.ID, addr *network.BzzAddr) (storage.Ch
// watchDisconnections receives simulation peer events in a new goroutine and sets atomic value
// disconnected to true in case of a disconnect event.
func watchDisconnections(ctx context.Context, sim *simulation.Simulation) (disconnected atomic.Value) {
func watchDisconnections(ctx context.Context, sim *simulation.Simulation) (disconnected *boolean) {
log.Debug("Watching for disconnections")
disconnections := sim.PeerEvents(
ctx,
sim.NodeIDs(),
simulation.NewPeerEventsFilter().Drop(),
)
disconnected = new(boolean)
go func() {
for {
select {
@ -341,9 +342,32 @@ func watchDisconnections(ctx context.Context, sim *simulation.Simulation) (disco
} else {
log.Error("peer drop", "node", d.NodeID, "peer", d.PeerID)
}
disconnected.Store(true)
disconnected.set(true)
}
}
}()
return disconnected
}
// boolean is used to concurrently set
// and read a boolean value.
type boolean struct {
v bool
mu sync.RWMutex
}
// set sets the value.
func (b *boolean) set(v bool) {
b.mu.Lock()
defer b.mu.Unlock()
b.v = v
}
// bool reads the value.
func (b *boolean) bool() bool {
b.mu.RLock()
defer b.mu.RUnlock()
return b.v
}

View file

@ -550,11 +550,9 @@ func testDeliveryFromNodes(t *testing.T, nodes, chunkCount int, skipCheck bool)
disconnected := watchDisconnections(ctx, sim)
defer func() {
if err != nil {
if yes, ok := disconnected.Load().(bool); ok && yes {
if err != nil && disconnected.bool() {
err = errors.New("disconnect events received")
}
}
}()
//finally check that the pivot node gets all chunks via the root hash
@ -667,11 +665,9 @@ func benchmarkDeliveryFromNodes(b *testing.B, nodes, chunkCount int, skipCheck b
disconnected := watchDisconnections(ctx, sim)
defer func() {
if err != nil {
if yes, ok := disconnected.Load().(bool); ok && yes {
if err != nil && disconnected.bool() {
err = errors.New("disconnect events received")
}
}
}()
// benchmark loop
b.ResetTimer()

View file

@ -140,11 +140,9 @@ func testIntervals(t *testing.T, live bool, history *Range, skipCheck bool) {
disconnected := watchDisconnections(ctx, sim)
defer func() {
if err != nil {
if yes, ok := disconnected.Load().(bool); ok && yes {
if err != nil && disconnected.bool() {
err = errors.New("disconnect events received")
}
}
}()
go func() {

View file

@ -195,11 +195,9 @@ func runSim(conf *synctestConfig, ctx context.Context, sim *simulation.Simulatio
return sim.Run(ctx, func(ctx context.Context, sim *simulation.Simulation) (err error) {
disconnected := watchDisconnections(ctx, sim)
defer func() {
if err != nil {
if yes, ok := disconnected.Load().(bool); ok && yes {
if err != nil && disconnected.bool() {
err = errors.New("disconnect events received")
}
}
}()
nodeIDs := sim.UpNodeIDs()

View file

@ -141,11 +141,9 @@ func testSyncBetweenNodes(t *testing.T, nodes, chunkCount int, skipCheck bool, p
disconnected := watchDisconnections(ctx, sim)
defer func() {
if err != nil {
if yes, ok := disconnected.Load().(bool); ok && yes {
if err != nil && disconnected.bool() {
err = errors.New("disconnect events received")
}
}
}()
// each node Subscribes to each other's swarmChunkServerStreamName