mock.go 5.05 KB
Newer Older
1 2 3 4
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

5 6 7 8
package mock

import (
	"context"
9
	"errors"
10
	"time"
11

12
	"github.com/ethersphere/bee/pkg/bzz"
13
	"github.com/ethersphere/bee/pkg/p2p"
14
	"github.com/ethersphere/bee/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 21 22 23
	addProtocolFunc       func(p2p.ProtocolSpec) error
	connectFunc           func(ctx context.Context, addr ma.Multiaddr) (address *bzz.Address, err error)
	disconnectFunc        func(overlay swarm.Address) error
	peersFunc             func() []p2p.Peer
24
	blocklistedPeersFunc  func() ([]p2p.Peer, error)
25
	addressesFunc         func() ([]ma.Multiaddr, error)
26
	setNotifierFunc       func(p2p.PickyNotifier)
27 28
	setWelcomeMessageFunc func(string) error
	getWelcomeMessageFunc func() string
29
	blocklistFunc         func(swarm.Address, time.Duration) error
30 31 32 33
	welcomeMessage        string
}

// WithAddProtocolFunc sets the mock implementation of the AddProtocol function
34 35 36 37
func WithAddProtocolFunc(f func(p2p.ProtocolSpec) error) Option {
	return optionFunc(func(s *Service) {
		s.addProtocolFunc = f
	})
38 39
}

40
// WithSetNotifierFunc sets the mock implementation of the SetNotifier function
41
func WithSetPickyNotifierFunc(f func(p2p.PickyNotifier)) Option {
42 43 44 45 46
	return optionFunc(func(s *Service) {
		s.setNotifierFunc = f
	})
}

47
// WithConnectFunc sets the mock implementation of the Connect function
48
func WithConnectFunc(f func(ctx context.Context, addr ma.Multiaddr) (address *bzz.Address, err error)) Option {
49 50
	return optionFunc(func(s *Service) {
		s.connectFunc = f
51 52 53
	})
}

54
// WithDisconnectFunc sets the mock implementation of the Disconnect function
55
func WithDisconnectFunc(f func(overlay swarm.Address) error) Option {
56 57
	return optionFunc(func(s *Service) {
		s.disconnectFunc = f
58 59 60
	})
}

61
// WithPeersFunc sets the mock implementation of the Peers function
62 63 64 65 66 67
func WithPeersFunc(f func() []p2p.Peer) Option {
	return optionFunc(func(s *Service) {
		s.peersFunc = f
	})
}

68 69 70 71 72 73 74
// WithBlocklistedPeersFunc sets the mock implementation of the BlocklistedPeers function
func WithBlocklistedPeersFunc(f func() ([]p2p.Peer, error)) Option {
	return optionFunc(func(s *Service) {
		s.blocklistedPeersFunc = f
	})
}

75
// WithAddressesFunc sets the mock implementation of the Adresses function
76 77 78 79 80 81
func WithAddressesFunc(f func() ([]ma.Multiaddr, error)) Option {
	return optionFunc(func(s *Service) {
		s.addressesFunc = f
	})
}

82 83 84 85 86 87 88 89 90 91 92 93 94 95
// WithGetWelcomeMessageFunc sets the mock implementation of the GetWelcomeMessage function
func WithGetWelcomeMessageFunc(f func() string) Option {
	return optionFunc(func(s *Service) {
		s.getWelcomeMessageFunc = f
	})
}

// WithSetWelcomeMessageFunc sets the mock implementation of the SetWelcomeMessage function
func WithSetWelcomeMessageFunc(f func(string) error) Option {
	return optionFunc(func(s *Service) {
		s.setWelcomeMessageFunc = f
	})
}

96 97 98 99 100 101
func WithBlocklistFunc(f func(swarm.Address, time.Duration) error) Option {
	return optionFunc(func(s *Service) {
		s.blocklistFunc = f
	})
}

102
// New will create a new mock P2P Service with the given options
103 104
func New(opts ...Option) *Service {
	s := new(Service)
105
	for _, o := range opts {
106
		o.apply(s)
107
	}
108
	return s
109 110
}

111
func (s *Service) AddProtocol(spec p2p.ProtocolSpec) error {
112
	if s.addProtocolFunc == nil {
113
		return errors.New("function AddProtocol not configured")
114
	}
115
	return s.addProtocolFunc(spec)
116 117
}

118
func (s *Service) Connect(ctx context.Context, addr ma.Multiaddr) (address *bzz.Address, err error) {
119
	if s.connectFunc == nil {
120
		return nil, errors.New("function Connect not configured")
121
	}
122
	return s.connectFunc(ctx, addr)
123 124
}

125
func (s *Service) Disconnect(overlay swarm.Address) error {
126
	if s.disconnectFunc == nil {
127
		return errors.New("function Disconnect not configured")
128
	}
129
	return s.disconnectFunc(overlay)
130
}
131

132 133 134 135 136 137 138
func (s *Service) Addresses() ([]ma.Multiaddr, error) {
	if s.addressesFunc == nil {
		return nil, errors.New("function Addresses not configured")
	}
	return s.addressesFunc()
}

139 140 141 142 143 144 145
func (s *Service) Peers() []p2p.Peer {
	if s.peersFunc == nil {
		return nil
	}
	return s.peersFunc()
}

146 147 148 149 150 151 152 153
func (s *Service) BlocklistedPeers() ([]p2p.Peer, error) {
	if s.blocklistedPeersFunc == nil {
		return nil, nil
	}

	return s.blocklistedPeersFunc()
}

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
func (s *Service) SetWelcomeMessage(val string) error {
	if s.setWelcomeMessageFunc != nil {
		return s.setWelcomeMessageFunc(val)
	}
	s.welcomeMessage = val
	return nil
}

func (s *Service) GetWelcomeMessage() string {
	if s.getWelcomeMessageFunc != nil {
		return s.getWelcomeMessageFunc()
	}
	return s.welcomeMessage
}

169 170
func (s *Service) Halt() {}

171 172 173 174 175 176 177
func (s *Service) Blocklist(overlay swarm.Address, duration time.Duration) error {
	if s.blocklistFunc == nil {
		return errors.New("function blocklist not configured")
	}
	return s.blocklistFunc(overlay, duration)
}

178
func (s *Service) SetPickyNotifier(f p2p.PickyNotifier) {
179 180 181 182 183 184 185
	if s.setNotifierFunc == nil {
		return
	}

	s.setNotifierFunc(f)
}

186
type Option interface {
187
	apply(*Service)
188
}
189
type optionFunc func(*Service)
190

191
func (f optionFunc) apply(r *Service) { f(r) }