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