utils_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. func NewNoopServiceA(*ServiceContext) (Service, error) { return new(NoopServiceA), nil }
  37. func NewNoopServiceB(*ServiceContext) (Service, error) { return new(NoopServiceB), nil }
  38. func NewNoopServiceC(*ServiceContext) (Service, error) { return new(NoopServiceC), nil }
  39. // InstrumentedService is an implementation of Service for which all interface
  40. // methods can be instrumented both return value as well as event hook wise.
  41. type InstrumentedService struct {
  42. protocols []p2p.Protocol
  43. apis []rpc.API
  44. start error
  45. stop error
  46. protocolsHook func()
  47. startHook func(*p2p.Server)
  48. stopHook func()
  49. }
  50. func NewInstrumentedService(*ServiceContext) (Service, error) { return new(InstrumentedService), nil }
  51. func (s *InstrumentedService) Protocols() []p2p.Protocol {
  52. if s.protocolsHook != nil {
  53. s.protocolsHook()
  54. }
  55. return s.protocols
  56. }
  57. func (s *InstrumentedService) APIs() []rpc.API {
  58. return s.apis
  59. }
  60. func (s *InstrumentedService) Start(server *p2p.Server) error {
  61. if s.startHook != nil {
  62. s.startHook(server)
  63. }
  64. return s.start
  65. }
  66. func (s *InstrumentedService) Stop() error {
  67. if s.stopHook != nil {
  68. s.stopHook()
  69. }
  70. return s.stop
  71. }
  72. // InstrumentingWrapper is a method to specialize a service constructor returning
  73. // a generic InstrumentedService into one returning a wrapping specific one.
  74. type InstrumentingWrapper func(base ServiceConstructor) ServiceConstructor
  75. func InstrumentingWrapperMaker(base ServiceConstructor, kind reflect.Type) ServiceConstructor {
  76. return func(ctx *ServiceContext) (Service, error) {
  77. obj, err := base(ctx)
  78. if err != nil {
  79. return nil, err
  80. }
  81. wrapper := reflect.New(kind)
  82. wrapper.Elem().Field(0).Set(reflect.ValueOf(obj).Elem())
  83. return wrapper.Interface().(Service), nil
  84. }
  85. }
  86. // Set of services all wrapping the base InstrumentedService resulting in the
  87. // same method signatures but different outer types.
  88. type InstrumentedServiceA struct{ InstrumentedService }
  89. type InstrumentedServiceB struct{ InstrumentedService }
  90. type InstrumentedServiceC struct{ InstrumentedService }
  91. func InstrumentedServiceMakerA(base ServiceConstructor) ServiceConstructor {
  92. return InstrumentingWrapperMaker(base, reflect.TypeOf(InstrumentedServiceA{}))
  93. }
  94. func InstrumentedServiceMakerB(base ServiceConstructor) ServiceConstructor {
  95. return InstrumentingWrapperMaker(base, reflect.TypeOf(InstrumentedServiceB{}))
  96. }
  97. func InstrumentedServiceMakerC(base ServiceConstructor) ServiceConstructor {
  98. return InstrumentingWrapperMaker(base, reflect.TypeOf(InstrumentedServiceC{}))
  99. }
  100. // OneMethodAPI is a single-method API handler to be returned by test services.
  101. type OneMethodAPI struct {
  102. fun func()
  103. }
  104. func (api *OneMethodAPI) TheOneMethod() {
  105. if api.fun != nil {
  106. api.fun()
  107. }
  108. }