MCPcopy Create free account
hub / github.com/CPChain/chain / TestServiceStartupAbortion

Function TestServiceStartupAbortion

node/node_test.go:270–319  ·  view source on GitHub ↗

Tests that if a service fails to start, all others started before it will be shut down.

(t *testing.T)

Source from the content-addressed store, hash-verified

268// Tests that if a service fails to start, all others started before it will be
269// shut down.
270func 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.

Callers

nothing calls this directly

Calls 4

testNodeConfigFunction · 0.85
NewFunction · 0.70
StartMethod · 0.65
RegisterMethod · 0.45

Tested by

no test coverage detected