Tests that if a service fails to initialize itself, none of the other services will be allowed to even start.
(t *testing.T)
| 221 | // Tests that if a service fails to initialize itself, none of the other services |
| 222 | // will be allowed to even start. |
| 223 | func TestServiceConstructionAbortion(t *testing.T) { |
| 224 | stack, err := New(testNodeConfig()) |
| 225 | if err != nil { |
| 226 | t.Fatalf("failed to create protocol stack: %v", err) |
| 227 | } |
| 228 | // Define a batch of good services |
| 229 | services := map[string]InstrumentingWrapper{ |
| 230 | "A": InstrumentedServiceMakerA, |
| 231 | "B": InstrumentedServiceMakerB, |
| 232 | "C": InstrumentedServiceMakerC, |
| 233 | } |
| 234 | started := make(map[string]bool) |
| 235 | for id, maker := range services { |
| 236 | id := id // Closure for the constructor |
| 237 | constructor := func(*ServiceContext) (Service, error) { |
| 238 | return &InstrumentedService{ |
| 239 | startHook: func(*p2p.Server) { started[id] = true }, |
| 240 | }, nil |
| 241 | } |
| 242 | if err := stack.Register(maker(constructor)); err != nil { |
| 243 | t.Fatalf("service %s: registration failed: %v", id, err) |
| 244 | } |
| 245 | } |
| 246 | // Register a service that fails to construct itself |
| 247 | failure := errors.New("fail") |
| 248 | failer := func(*ServiceContext) (Service, error) { |
| 249 | return nil, failure |
| 250 | } |
| 251 | if err := stack.Register(failer); err != nil { |
| 252 | t.Fatalf("failer registration failed: %v", err) |
| 253 | } |
| 254 | // Start the protocol stack and ensure none of the services get started |
| 255 | for i := 0; i < 100; i++ { |
| 256 | if err := stack.Start(); err != failure { |
| 257 | t.Fatalf("iter %d: stack startup failure mismatch: have %v, want %v", i, err, failure) |
| 258 | } |
| 259 | for id := range services { |
| 260 | if started[id] { |
| 261 | t.Fatalf("service %s: started should not have", id) |
| 262 | } |
| 263 | delete(started, id) |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | // Tests that if a service fails to start, all others started before it will be |
| 269 | // shut down. |
nothing calls this directly
no test coverage detected