github.com/ethersphere/bee/v2@v2.2.0/pkg/p2p/mock/mock.go (about)

     1  // Copyright 2020 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package mock
     6  
     7  import (
     8  	"context"
     9  	"errors"
    10  	"time"
    11  
    12  	"github.com/ethersphere/bee/v2/pkg/bzz"
    13  	"github.com/ethersphere/bee/v2/pkg/p2p"
    14  	"github.com/ethersphere/bee/v2/pkg/swarm"
    15  	ma "github.com/multiformats/go-multiaddr"
    16  )
    17  
    18  // Service is the mock of a P2P Service
    19  type Service struct {
    20  	addProtocolFunc       func(p2p.ProtocolSpec) error
    21  	connectFunc           func(ctx context.Context, addr ma.Multiaddr) (address *bzz.Address, err error)
    22  	disconnectFunc        func(overlay swarm.Address, reason string) error
    23  	peersFunc             func() []p2p.Peer
    24  	blocklistedPeersFunc  func() ([]p2p.BlockListedPeer, error)
    25  	addressesFunc         func() ([]ma.Multiaddr, error)
    26  	notifierFunc          p2p.PickyNotifier
    27  	setWelcomeMessageFunc func(string) error
    28  	getWelcomeMessageFunc func() string
    29  	blocklistFunc         func(swarm.Address, time.Duration, string) error
    30  	welcomeMessage        string
    31  }
    32  
    33  // WithAddProtocolFunc sets the mock implementation of the AddProtocol function
    34  func WithAddProtocolFunc(f func(p2p.ProtocolSpec) error) Option {
    35  	return optionFunc(func(s *Service) {
    36  		s.addProtocolFunc = f
    37  	})
    38  }
    39  
    40  // WithConnectFunc sets the mock implementation of the Connect function
    41  func WithConnectFunc(f func(ctx context.Context, addr ma.Multiaddr) (address *bzz.Address, err error)) Option {
    42  	return optionFunc(func(s *Service) {
    43  		s.connectFunc = f
    44  	})
    45  }
    46  
    47  // WithDisconnectFunc sets the mock implementation of the Disconnect function
    48  func WithDisconnectFunc(f func(overlay swarm.Address, reason string) error) Option {
    49  	return optionFunc(func(s *Service) {
    50  		s.disconnectFunc = f
    51  	})
    52  }
    53  
    54  // WithPeersFunc sets the mock implementation of the Peers function
    55  func WithPeersFunc(f func() []p2p.Peer) Option {
    56  	return optionFunc(func(s *Service) {
    57  		s.peersFunc = f
    58  	})
    59  }
    60  
    61  // WithBlocklistedPeersFunc sets the mock implementation of the BlocklistedPeers function
    62  func WithBlocklistedPeersFunc(f func() ([]p2p.BlockListedPeer, error)) Option {
    63  	return optionFunc(func(s *Service) {
    64  		s.blocklistedPeersFunc = f
    65  	})
    66  }
    67  
    68  // WithAddressesFunc sets the mock implementation of the Addresses function
    69  func WithAddressesFunc(f func() ([]ma.Multiaddr, error)) Option {
    70  	return optionFunc(func(s *Service) {
    71  		s.addressesFunc = f
    72  	})
    73  }
    74  
    75  // WithGetWelcomeMessageFunc sets the mock implementation of the GetWelcomeMessage function
    76  func WithGetWelcomeMessageFunc(f func() string) Option {
    77  	return optionFunc(func(s *Service) {
    78  		s.getWelcomeMessageFunc = f
    79  	})
    80  }
    81  
    82  // WithSetWelcomeMessageFunc sets the mock implementation of the SetWelcomeMessage function
    83  func WithSetWelcomeMessageFunc(f func(string) error) Option {
    84  	return optionFunc(func(s *Service) {
    85  		s.setWelcomeMessageFunc = f
    86  	})
    87  }
    88  
    89  func WithBlocklistFunc(f func(swarm.Address, time.Duration, string) error) Option {
    90  	return optionFunc(func(s *Service) {
    91  		s.blocklistFunc = f
    92  	})
    93  }
    94  
    95  // New will create a new mock P2P Service with the given options
    96  func New(opts ...Option) *Service {
    97  	s := new(Service)
    98  	for _, o := range opts {
    99  		o.apply(s)
   100  	}
   101  	return s
   102  }
   103  
   104  func (s *Service) AddProtocol(spec p2p.ProtocolSpec) error {
   105  	if s.addProtocolFunc == nil {
   106  		return errors.New("function AddProtocol not configured")
   107  	}
   108  	return s.addProtocolFunc(spec)
   109  }
   110  
   111  func (s *Service) Connect(ctx context.Context, addr ma.Multiaddr) (address *bzz.Address, err error) {
   112  	if s.connectFunc == nil {
   113  		return nil, errors.New("function Connect not configured")
   114  	}
   115  	return s.connectFunc(ctx, addr)
   116  }
   117  
   118  func (s *Service) Disconnect(overlay swarm.Address, reason string) error {
   119  	if s.disconnectFunc == nil {
   120  		return errors.New("function Disconnect not configured")
   121  	}
   122  
   123  	if s.notifierFunc != nil {
   124  		s.notifierFunc.Disconnected(p2p.Peer{Address: overlay})
   125  	}
   126  
   127  	return s.disconnectFunc(overlay, reason)
   128  }
   129  
   130  func (s *Service) Addresses() ([]ma.Multiaddr, error) {
   131  	if s.addressesFunc == nil {
   132  		return nil, errors.New("function Addresses not configured")
   133  	}
   134  	return s.addressesFunc()
   135  }
   136  
   137  func (s *Service) Peers() []p2p.Peer {
   138  	if s.peersFunc == nil {
   139  		return nil
   140  	}
   141  	return s.peersFunc()
   142  }
   143  
   144  func (s *Service) Blocklisted(overlay swarm.Address) (bool, error) {
   145  	return false, nil
   146  }
   147  
   148  func (s *Service) BlocklistedPeers() ([]p2p.BlockListedPeer, error) {
   149  	if s.blocklistedPeersFunc == nil {
   150  		return nil, nil
   151  	}
   152  
   153  	return s.blocklistedPeersFunc()
   154  }
   155  
   156  func (s *Service) SetWelcomeMessage(val string) error {
   157  	if s.setWelcomeMessageFunc != nil {
   158  		return s.setWelcomeMessageFunc(val)
   159  	}
   160  	s.welcomeMessage = val
   161  	return nil
   162  }
   163  
   164  func (s *Service) GetWelcomeMessage() string {
   165  	if s.getWelcomeMessageFunc != nil {
   166  		return s.getWelcomeMessageFunc()
   167  	}
   168  	return s.welcomeMessage
   169  }
   170  
   171  func (s *Service) Halt() {}
   172  
   173  func (s *Service) Blocklist(overlay swarm.Address, duration time.Duration, reason string) error {
   174  	if s.blocklistFunc == nil {
   175  		return errors.New("function blocklist not configured")
   176  	}
   177  	return s.blocklistFunc(overlay, duration, reason)
   178  }
   179  
   180  func (s *Service) SetPickyNotifier(f p2p.PickyNotifier) {
   181  	s.notifierFunc = f
   182  }
   183  
   184  // NetworkStatus implements p2p.NetworkStatuser interface.
   185  // It always returns p2p.NetworkStatusAvailable.
   186  func (s *Service) NetworkStatus() p2p.NetworkStatus {
   187  	return p2p.NetworkStatusAvailable
   188  }
   189  
   190  type Option interface {
   191  	apply(*Service)
   192  }
   193  type optionFunc func(*Service)
   194  
   195  func (f optionFunc) apply(r *Service) { f(r) }