diff options
author | Janoš Guljaš <janos@users.noreply.github.com> | 2018-08-06 17:33:22 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2018-08-06 17:33:22 +0800 |
commit | a72ba5a55be3abb1be85126838ee0b4a9c475be4 (patch) | |
tree | 6c02c730ded55f6842778618ba06ae3d52d06bdf /swarm/network/simulation | |
parent | 6711f098d5f070fbb26a4dd7899e3c6c766f4477 (diff) | |
download | go-tangerine-a72ba5a55be3abb1be85126838ee0b4a9c475be4.tar go-tangerine-a72ba5a55be3abb1be85126838ee0b4a9c475be4.tar.gz go-tangerine-a72ba5a55be3abb1be85126838ee0b4a9c475be4.tar.bz2 go-tangerine-a72ba5a55be3abb1be85126838ee0b4a9c475be4.tar.lz go-tangerine-a72ba5a55be3abb1be85126838ee0b4a9c475be4.tar.xz go-tangerine-a72ba5a55be3abb1be85126838ee0b4a9c475be4.tar.zst go-tangerine-a72ba5a55be3abb1be85126838ee0b4a9c475be4.zip |
cmd/swarm, swarm: various test fixes (#17299)
* swarm/network/simulation: increase the sleep duration for TestRun
* cmd/swarm, swarm: fix failing tests on mac
* cmd/swarm: update TestCLISwarmFs skip comment
* swarm/network/simulation: adjust disconnections on simulation close
* swarm/network/simulation: call cleanups after net shutdown
Diffstat (limited to 'swarm/network/simulation')
-rw-r--r-- | swarm/network/simulation/simulation.go | 23 | ||||
-rw-r--r-- | swarm/network/simulation/simulation_test.go | 2 |
2 files changed, 20 insertions, 5 deletions
diff --git a/swarm/network/simulation/simulation.go b/swarm/network/simulation/simulation.go index 2241dfca2..74f9d98ee 100644 --- a/swarm/network/simulation/simulation.go +++ b/swarm/network/simulation/simulation.go @@ -62,6 +62,8 @@ type Simulation struct { // where all "global" state related to the service should be kept. // All cleanups needed for constructed service and any other constructed // objects should ne provided in a single returned cleanup function. +// Returned cleanup function will be called by Close function +// after network shutdown. type ServiceFunc func(ctx *adapters.ServiceContext, bucket *sync.Map) (s node.Service, cleanup func(), err error) // New creates a new Simulation instance with new @@ -161,6 +163,18 @@ var maxParallelCleanups = 10 // simulation. func (s *Simulation) Close() { close(s.done) + + // Close all connections before calling the Network Shutdown. + // It is possible that p2p.Server.Stop will block if there are + // existing connections. + for _, c := range s.Net.Conns { + if c.Up { + s.Net.Disconnect(c.One, c.Other) + } + } + s.shutdownWG.Wait() + s.Net.Shutdown() + sem := make(chan struct{}, maxParallelCleanups) s.mu.RLock() cleanupFuncs := make([]func(), len(s.cleanupFuncs)) @@ -170,16 +184,19 @@ func (s *Simulation) Close() { } } s.mu.RUnlock() + var cleanupWG sync.WaitGroup for _, cleanup := range cleanupFuncs { - s.shutdownWG.Add(1) + cleanupWG.Add(1) sem <- struct{}{} go func(cleanup func()) { - defer s.shutdownWG.Done() + defer cleanupWG.Done() defer func() { <-sem }() cleanup() }(cleanup) } + cleanupWG.Wait() + if s.httpSrv != nil { ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) defer cancel() @@ -189,8 +206,6 @@ func (s *Simulation) Close() { } close(s.runC) } - s.shutdownWG.Wait() - s.Net.Shutdown() } // Done returns a channel that is closed when the simulation diff --git a/swarm/network/simulation/simulation_test.go b/swarm/network/simulation/simulation_test.go index 803e0499a..8576732c9 100644 --- a/swarm/network/simulation/simulation_test.go +++ b/swarm/network/simulation/simulation_test.go @@ -68,7 +68,7 @@ func TestRun(t *testing.T) { defer cancel() r := sim.Run(ctx, func(ctx context.Context, sim *Simulation) error { - time.Sleep(100 * time.Millisecond) + time.Sleep(time.Second) return nil }) |