github.com/core-coin/go-core/v2@v2.1.9/node/utils_test.go (about)

     1  // Copyright 2015 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // Contains a batch of utility type declarations used by the tests. As the node
    18  // operates on unique types, a lot of them are needed to check various features.
    19  
    20  package node
    21  
    22  import (
    23  	"github.com/core-coin/go-core/v2/p2p"
    24  	"github.com/core-coin/go-core/v2/rpc"
    25  )
    26  
    27  // NoopLifecycle is a trivial implementation of the Service interface.
    28  type NoopLifecycle struct{}
    29  
    30  func (s *NoopLifecycle) Start() error { return nil }
    31  func (s *NoopLifecycle) Stop() error  { return nil }
    32  
    33  func NewNoop() *Noop {
    34  	noop := new(Noop)
    35  	return noop
    36  }
    37  
    38  // Set of services all wrapping the base NoopLifecycle resulting in the same method
    39  // signatures but different outer types.
    40  type Noop struct{ NoopLifecycle }
    41  
    42  // InstrumentedService is an implementation of Lifecycle for which all interface
    43  // methods can be instrumented both return value as well as event hook wise.
    44  type InstrumentedService struct {
    45  	start error
    46  	stop  error
    47  
    48  	startHook func()
    49  	stopHook  func()
    50  
    51  	protocols []p2p.Protocol
    52  }
    53  
    54  func (s *InstrumentedService) Start() error {
    55  	if s.startHook != nil {
    56  		s.startHook()
    57  	}
    58  	return s.start
    59  }
    60  
    61  func (s *InstrumentedService) Stop() error {
    62  	if s.stopHook != nil {
    63  		s.stopHook()
    64  	}
    65  	return s.stop
    66  }
    67  
    68  type FullService struct{}
    69  
    70  func NewFullService(stack *Node) (*FullService, error) {
    71  	fs := new(FullService)
    72  
    73  	stack.RegisterProtocols(fs.Protocols())
    74  	stack.RegisterAPIs(fs.APIs())
    75  	stack.RegisterLifecycle(fs)
    76  	return fs, nil
    77  }
    78  
    79  func (f *FullService) Start() error { return nil }
    80  
    81  func (f *FullService) Stop() error { return nil }
    82  
    83  func (f *FullService) Protocols() []p2p.Protocol {
    84  	return []p2p.Protocol{
    85  		p2p.Protocol{
    86  			Name:    "test1",
    87  			Version: uint(1),
    88  		},
    89  		p2p.Protocol{
    90  			Name:    "test2",
    91  			Version: uint(2),
    92  		},
    93  	}
    94  }
    95  
    96  func (f *FullService) APIs() []rpc.API {
    97  	return []rpc.API{
    98  		{
    99  			Namespace: "admin",
   100  			Version:   "1.0",
   101  		},
   102  		{
   103  			Namespace: "debug",
   104  			Version:   "1.0",
   105  			Public:    true,
   106  		},
   107  		{
   108  			Namespace: "net",
   109  			Version:   "1.0",
   110  			Public:    true,
   111  		},
   112  	}
   113  }