Tests that all protocols defined by individual services get launched.
(t *testing.T)
| 431 | |
| 432 | // Tests that all protocols defined by individual services get launched. |
| 433 | func TestProtocolGather(t *testing.T) { |
| 434 | stack, err := New(testNodeConfig()) |
| 435 | if err != nil { |
| 436 | t.Fatalf("failed to create protocol stack: %v", err) |
| 437 | } |
| 438 | // Register a batch of services with some configured number of protocols |
| 439 | services := map[string]struct { |
| 440 | Count int |
| 441 | Maker InstrumentingWrapper |
| 442 | }{ |
| 443 | "Zero Protocols": {0, InstrumentedServiceMakerA}, |
| 444 | "Single Protocol": {1, InstrumentedServiceMakerB}, |
| 445 | "Many Protocols": {25, InstrumentedServiceMakerC}, |
| 446 | } |
| 447 | for id, config := range services { |
| 448 | protocols := make([]p2p.Protocol, config.Count) |
| 449 | for i := 0; i < len(protocols); i++ { |
| 450 | protocols[i].Name = id |
| 451 | protocols[i].Version = uint(i) |
| 452 | } |
| 453 | constructor := func(*ServiceContext) (Service, error) { |
| 454 | return &InstrumentedService{ |
| 455 | protocols: protocols, |
| 456 | }, nil |
| 457 | } |
| 458 | if err := stack.Register(config.Maker(constructor)); err != nil { |
| 459 | t.Fatalf("service %s: registration failed: %v", id, err) |
| 460 | } |
| 461 | } |
| 462 | // Start the services and ensure all protocols start successfully |
| 463 | if err := stack.Start(); err != nil { |
| 464 | t.Fatalf("failed to start protocol stack: %v", err) |
| 465 | } |
| 466 | defer stack.Stop() |
| 467 | |
| 468 | protocols := stack.Server().Protocols |
| 469 | if len(protocols) != 26 { |
| 470 | t.Fatalf("mismatching number of protocols launched: have %d, want %d", len(protocols), 26) |
| 471 | } |
| 472 | for id, config := range services { |
| 473 | for ver := 0; ver < config.Count; ver++ { |
| 474 | launched := false |
| 475 | for i := 0; i < len(protocols); i++ { |
| 476 | if protocols[i].Name == id && protocols[i].Version == uint(ver) { |
| 477 | launched = true |
| 478 | break |
| 479 | } |
| 480 | } |
| 481 | if !launched { |
| 482 | t.Errorf("configured protocol not launched: %s v%d", id, ver) |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | // Tests that all APIs defined by individual services get exposed. |
| 489 | func TestAPIGather(t *testing.T) { |