Tests that if a service fails to start, all others started before it will be shut down.
(t *testing.T)
| 268 | // Tests that if a service fails to start, all others started before it will be |
| 269 | // shut down. |
| 270 | func TestServiceStartupAbortion(t *testing.T) { |
| 271 | stack, err := New(testNodeConfig()) |
| 272 | if err != nil { |
| 273 | t.Fatalf("failed to create protocol stack: %v", err) |
| 274 | } |
| 275 | // Register a batch of good services |
| 276 | services := map[string]InstrumentingWrapper{ |
| 277 | "A": InstrumentedServiceMakerA, |
| 278 | "B": InstrumentedServiceMakerB, |
| 279 | "C": InstrumentedServiceMakerC, |
| 280 | } |
| 281 | started := make(map[string]bool) |
| 282 | stopped := make(map[string]bool) |
| 283 | |
| 284 | for id, maker := range services { |
| 285 | id := id // Closure for the constructor |
| 286 | constructor := func(*ServiceContext) (Service, error) { |
| 287 | return &InstrumentedService{ |
| 288 | startHook: func(*p2p.Server) { started[id] = true }, |
| 289 | stopHook: func() { stopped[id] = true }, |
| 290 | }, nil |
| 291 | } |
| 292 | if err := stack.Register(maker(constructor)); err != nil { |
| 293 | t.Fatalf("service %s: registration failed: %v", id, err) |
| 294 | } |
| 295 | } |
| 296 | // Register a service that fails to start |
| 297 | failure := errors.New("fail") |
| 298 | failer := func(*ServiceContext) (Service, error) { |
| 299 | return &InstrumentedService{ |
| 300 | start: failure, |
| 301 | }, nil |
| 302 | } |
| 303 | if err := stack.Register(failer); err != nil { |
| 304 | t.Fatalf("failer registration failed: %v", err) |
| 305 | } |
| 306 | // Start the protocol stack and ensure all started services stop |
| 307 | for i := 0; i < 100; i++ { |
| 308 | if err := stack.Start(); err != failure { |
| 309 | t.Fatalf("iter %d: stack startup failure mismatch: have %v, want %v", i, err, failure) |
| 310 | } |
| 311 | for id := range services { |
| 312 | if started[id] && !stopped[id] { |
| 313 | t.Fatalf("service %s: started but not stopped", id) |
| 314 | } |
| 315 | delete(started, id) |
| 316 | delete(stopped, id) |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | // Tests that even if a registered service fails to shut down cleanly, it does |
| 322 | // not influece the rest of the shutdown invocations. |
nothing calls this directly
no test coverage detected