utils_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // Contains a batch of utility type declarations used by the tests. As the node
  17. // operates on unique types, a lot of them are needed to check various features.
  18. package node
  19. import (
  20. "reflect"
  21. "github.com/ethereum/go-ethereum/p2p"
  22. "github.com/ethereum/go-ethereum/rpc"
  23. )
  24. // NoopService is a trivial implementation of the Service interface.
  25. type NoopService struct{}
  26. func (s *NoopService) Protocols() []p2p.Protocol { return nil }
  27. func (s *NoopService) APIs() []rpc.API { return nil }
  28. func (s *NoopService) Start(*p2p.Server) error { return nil }
  29. func (s *NoopService) Stop() error { return nil }
  30. func NewNoopService(*ServiceContext) (Service, error) { return new(NoopService), nil }
  31. // Set of services all wrapping the base NoopService resulting in the same method
  32. // signatures but different outer types.
  33. type NoopServiceA struct{ NoopService }
  34. type NoopServiceB struct{ NoopService }
  35. type NoopServiceC struct{ NoopService }
  36. type NoopServiceD struct{ NoopService }
  37. func NewNoopServiceA(*ServiceContext) (Service, error) { return new(NoopServiceA), nil }
  38. func NewNoopServiceB(*ServiceContext) (Service, error) { return new(NoopServiceB), nil }
  39. func NewNoopServiceC(*ServiceContext) (Service, error) { return new(NoopServiceC), nil }
  40. func NewNoopServiceD(*ServiceContext) (Service, error) { return new(NoopServiceD), nil }
  41. // InstrumentedService is an implementation of Service for which all interface
  42. // methods can be instrumented both return value as well as event hook wise.
  43. type InstrumentedService struct {
  44. protocols []p2p.Protocol
  45. apis []rpc.API
  46. start error
  47. stop error
  48. protocolsHook func()
  49. startHook func(*p2p.Server)
  50. stopHook func()
  51. }
  52. func NewInstrumentedService(*ServiceContext) (Service, error) { return new(InstrumentedService), nil }
  53. func (s *InstrumentedService) Protocols() []p2p.Protocol {
  54. if s.protocolsHook != nil {
  55. s.protocolsHook()
  56. }
  57. return s.protocols
  58. }
  59. func (s *InstrumentedService) APIs() []rpc.API {
  60. return s.apis
  61. }
  62. func (s *InstrumentedService) Start(server *p2p.Server) error {
  63. if s.startHook != nil {
  64. s.startHook(server)
  65. }
  66. return s.start
  67. }
  68. func (s *InstrumentedService) Stop() error {
  69. if s.stopHook != nil {
  70. s.stopHook()
  71. }
  72. return s.stop
  73. }
  74. // InstrumentingWrapper is a method to specialize a service constructor returning
  75. // a generic InstrumentedService into one returning a wrapping specific one.
  76. type InstrumentingWrapper func(base ServiceConstructor) ServiceConstructor
  77. func InstrumentingWrapperMaker(base ServiceConstructor, kind reflect.Type) ServiceConstructor {
  78. return func(ctx *ServiceContext) (Service, error) {
  79. obj, err := base(ctx)
  80. if err != nil {
  81. return nil, err
  82. }
  83. wrapper := reflect.New(kind)
  84. wrapper.Elem().Field(0).Set(reflect.ValueOf(obj).Elem())
  85. return wrapper.Interface().(Service), nil
  86. }
  87. }
  88. // Set of services all wrapping the base InstrumentedService resulting in the
  89. // same method signatures but different outer types.
  90. type InstrumentedServiceA struct{ InstrumentedService }
  91. type InstrumentedServiceB struct{ InstrumentedService }
  92. type InstrumentedServiceC struct{ InstrumentedService }
  93. func InstrumentedServiceMakerA(base ServiceConstructor) ServiceConstructor {
  94. return InstrumentingWrapperMaker(base, reflect.TypeOf(InstrumentedServiceA{}))
  95. }
  96. func InstrumentedServiceMakerB(base ServiceConstructor) ServiceConstructor {
  97. return InstrumentingWrapperMaker(base, reflect.TypeOf(InstrumentedServiceB{}))
  98. }
  99. func InstrumentedServiceMakerC(base ServiceConstructor) ServiceConstructor {
  100. return InstrumentingWrapperMaker(base, reflect.TypeOf(InstrumentedServiceC{}))
  101. }
  102. // OneMethodApi is a single-method API handler to be returned by test services.
  103. type OneMethodApi struct {
  104. fun func()
  105. }
  106. func (api *OneMethodApi) TheOneMethod() {
  107. if api.fun != nil {
  108. api.fun()
  109. }
  110. }