|
@@ -62,6 +62,8 @@ type Simulation struct {
|
|
|
// where all "global" state related to the service should be kept.
|
|
// where all "global" state related to the service should be kept.
|
|
|
// All cleanups needed for constructed service and any other constructed
|
|
// All cleanups needed for constructed service and any other constructed
|
|
|
// objects should ne provided in a single returned cleanup function.
|
|
// 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)
|
|
type ServiceFunc func(ctx *adapters.ServiceContext, bucket *sync.Map) (s node.Service, cleanup func(), err error)
|
|
|
|
|
|
|
|
// New creates a new Simulation instance with new
|
|
// New creates a new Simulation instance with new
|
|
@@ -161,6 +163,18 @@ var maxParallelCleanups = 10
|
|
|
// simulation.
|
|
// simulation.
|
|
|
func (s *Simulation) Close() {
|
|
func (s *Simulation) Close() {
|
|
|
close(s.done)
|
|
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)
|
|
sem := make(chan struct{}, maxParallelCleanups)
|
|
|
s.mu.RLock()
|
|
s.mu.RLock()
|
|
|
cleanupFuncs := make([]func(), len(s.cleanupFuncs))
|
|
cleanupFuncs := make([]func(), len(s.cleanupFuncs))
|
|
@@ -170,16 +184,19 @@ func (s *Simulation) Close() {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
s.mu.RUnlock()
|
|
s.mu.RUnlock()
|
|
|
|
|
+ var cleanupWG sync.WaitGroup
|
|
|
for _, cleanup := range cleanupFuncs {
|
|
for _, cleanup := range cleanupFuncs {
|
|
|
- s.shutdownWG.Add(1)
|
|
|
|
|
|
|
+ cleanupWG.Add(1)
|
|
|
sem <- struct{}{}
|
|
sem <- struct{}{}
|
|
|
go func(cleanup func()) {
|
|
go func(cleanup func()) {
|
|
|
- defer s.shutdownWG.Done()
|
|
|
|
|
|
|
+ defer cleanupWG.Done()
|
|
|
defer func() { <-sem }()
|
|
defer func() { <-sem }()
|
|
|
|
|
|
|
|
cleanup()
|
|
cleanup()
|
|
|
}(cleanup)
|
|
}(cleanup)
|
|
|
}
|
|
}
|
|
|
|
|
+ cleanupWG.Wait()
|
|
|
|
|
+
|
|
|
if s.httpSrv != nil {
|
|
if s.httpSrv != nil {
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
defer cancel()
|
|
defer cancel()
|
|
@@ -189,8 +206,6 @@ func (s *Simulation) Close() {
|
|
|
}
|
|
}
|
|
|
close(s.runC)
|
|
close(s.runC)
|
|
|
}
|
|
}
|
|
|
- s.shutdownWG.Wait()
|
|
|
|
|
- s.Net.Shutdown()
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Done returns a channel that is closed when the simulation
|
|
// Done returns a channel that is closed when the simulation
|