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

Function TestAPIGather

node/node_test.go:489–561  ·  view source on GitHub ↗

Tests that all APIs defined by individual services get exposed.

(t *testing.T)

Source from the content-addressed store, hash-verified

487
488// Tests that all APIs defined by individual services get exposed.
489func TestAPIGather(t *testing.T) {
490 stack, err := New(testNodeConfig())
491 if err != nil {
492 t.Fatalf("failed to create protocol stack: %v", err)
493 }
494 // Register a batch of services with some configured APIs
495 calls := make(chan string, 1)
496 makeAPI := func(result string) *OneMethodAPI {
497 return &OneMethodAPI{fun: func() { calls <- result }}
498 }
499 services := map[string]struct {
500 APIs []rpc.API
501 Maker InstrumentingWrapper
502 }{
503 "Zero APIs": {
504 []rpc.API{}, InstrumentedServiceMakerA},
505 "Single API": {
506 []rpc.API{
507 {Namespace: "single", Version: "1", Service: makeAPI("single.v1"), Public: true},
508 }, InstrumentedServiceMakerB},
509 "Many APIs": {
510 []rpc.API{
511 {Namespace: "multi", Version: "1", Service: makeAPI("multi.v1"), Public: true},
512 {Namespace: "multi.v2", Version: "2", Service: makeAPI("multi.v2"), Public: true},
513 {Namespace: "multi.v2.nested", Version: "2", Service: makeAPI("multi.v2.nested"), Public: true},
514 }, InstrumentedServiceMakerC},
515 }
516
517 for id, config := range services {
518 config := config
519 constructor := func(*ServiceContext) (Service, error) {
520 return &InstrumentedService{apis: config.APIs}, nil
521 }
522 if err := stack.Register(config.Maker(constructor)); err != nil {
523 t.Fatalf("service %s: registration failed: %v", id, err)
524 }
525 }
526 // Start the services and ensure all API start successfully
527 if err := stack.Start(); err != nil {
528 t.Fatalf("failed to start protocol stack: %v", err)
529 }
530 defer stack.Stop()
531
532 // Connect to the RPC server and verify the various registered endpoints
533 client, err := stack.Attach()
534 if err != nil {
535 t.Fatalf("failed to connect to the inproc API server: %v", err)
536 }
537 defer client.Close()
538
539 tests := []struct {
540 Method string
541 Result string
542 }{
543 {"single_theOneMethod", "single.v1"},
544 {"multi_theOneMethod", "multi.v1"},
545 {"multi.v2_theOneMethod", "multi.v2"},
546 {"multi.v2.nested_theOneMethod", "multi.v2.nested"},

Callers

nothing calls this directly

Calls 8

testNodeConfigFunction · 0.85
AttachMethod · 0.80
NewFunction · 0.70
StartMethod · 0.65
StopMethod · 0.65
CloseMethod · 0.65
CallMethod · 0.65
RegisterMethod · 0.45

Tested by

no test coverage detected