-
Notifications
You must be signed in to change notification settings - Fork 211
/
discovery_mock.go
265 lines (214 loc) · 5.77 KB
/
discovery_mock.go
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package discovery
import (
"context"
"github.com/spacemeshos/go-spacemesh/p2p/node"
"github.com/spacemeshos/go-spacemesh/p2p/p2pcrypto"
)
// MockPeerStore is a mocked discovery
type MockPeerStore struct {
UpdateFunc func(n, src *node.Info)
updateCount int
SelectPeersFunc func(ctx context.Context, qty int) []*node.Info
bsres error
bsCount int
LookupFunc func(p2pcrypto.PublicKey) (*node.Info, error)
lookupRes *node.Info
lookupErr error
IsLocalAddressFunc func(info *node.Info) bool
RemoveFunc func(key p2pcrypto.PublicKey)
GoodFunc func(key p2pcrypto.PublicKey)
AttemptFunc func(key p2pcrypto.PublicKey)
}
// Remove mock
func (m *MockPeerStore) Remove(key p2pcrypto.PublicKey) {
if m.RemoveFunc != nil {
m.RemoveFunc(key)
}
}
// IsLocalAddress mock
func (m *MockPeerStore) IsLocalAddress(info *node.Info) bool {
if m.IsLocalAddressFunc != nil {
return m.IsLocalAddressFunc(info)
}
return false
}
// SetUpdate sets the function to run on an issued update
func (m *MockPeerStore) SetUpdate(f func(n, addr *node.Info)) {
m.UpdateFunc = f
}
// SetLookupResult sets the result ok a lookup operation
func (m *MockPeerStore) SetLookupResult(node *node.Info, err error) {
m.lookupRes = node
m.lookupErr = err
}
// Update is a discovery update operation it updates the updatecount
func (m *MockPeerStore) Update(n, src *node.Info) {
if m.UpdateFunc != nil {
m.UpdateFunc(n, src)
}
m.updateCount++
}
// UpdateCount returns the number of times update was called
func (m *MockPeerStore) UpdateCount() int {
return m.updateCount
}
// BootstrapCount returns the number of times bootstrap was called
func (m *MockPeerStore) BootstrapCount() int {
return m.bsCount
}
// Lookup is a discovery lookup operation
func (m *MockPeerStore) Lookup(pubkey p2pcrypto.PublicKey) (*node.Info, error) {
if m.LookupFunc != nil {
return m.LookupFunc(pubkey)
}
return m.lookupRes, m.lookupErr
}
// SetBootstrap set the bootstrap result
func (m *MockPeerStore) SetBootstrap(err error) {
m.bsres = err
}
// Bootstrap is a discovery bootstrap operation function it update the bootstrap count
func (m *MockPeerStore) Bootstrap(ctx context.Context) error {
m.bsCount++
return m.bsres
}
// SelectPeers mocks selecting peers.
func (m *MockPeerStore) SelectPeers(ctx context.Context, qty int) []*node.Info {
if m.SelectPeersFunc != nil {
return m.SelectPeersFunc(ctx, qty)
}
return []*node.Info{}
}
// SetLocalAddresses to satisfy the iface
func (m *MockPeerStore) SetLocalAddresses(tcp, udp int) {
}
// Size returns the size of peers in the discovery
func (m *MockPeerStore) Size() int {
//todo: set size
return m.updateCount
}
// Shutdown is a mock.
func (m *MockPeerStore) Shutdown() {
}
// Good is a mock.
func (m *MockPeerStore) Good(key p2pcrypto.PublicKey) {
if m.GoodFunc != nil {
m.GoodFunc(key)
}
}
// Attempt is a mock.
func (m *MockPeerStore) Attempt(key p2pcrypto.PublicKey) {
if m.AttemptFunc != nil {
m.AttemptFunc(key)
}
}
// mockAddrBook
type mockAddrBook struct {
addAddressFunc func(n, src *node.Info)
LookupFunc func(p2pcrypto.PublicKey) (*node.Info, error)
lookupRes *node.Info
lookupErr error
GetAddressFunc func() *KnownAddress
GetAddressRes *KnownAddress
NeedNewAddressesFunc func() bool
AddressCacheFunc func() []*node.Info
GoodFunc func(key p2pcrypto.PublicKey)
AttemptFunc func(key p2pcrypto.PublicKey)
IsLocalAddressFunc func(info *node.Info) bool
AddLocalAddressFunc func(info *node.Info)
NumAddressesFunc func() int
}
func (m *mockAddrBook) Stop() {
}
func (m *mockAddrBook) Start() {
}
func (m *mockAddrBook) AddLocalAddress(info *node.Info) {
if m.AddLocalAddressFunc != nil {
m.AddLocalAddressFunc(info)
}
}
func (m *mockAddrBook) IsLocalAddress(info *node.Info) bool {
if m.IsLocalAddressFunc != nil {
return m.IsLocalAddressFunc(info)
}
return false
}
func (m *mockAddrBook) RemoveAddress(key p2pcrypto.PublicKey) {
}
func (m *mockAddrBook) Good(key p2pcrypto.PublicKey) {
if m.GoodFunc != nil {
m.GoodFunc(key)
}
}
func (m *mockAddrBook) Attempt(key p2pcrypto.PublicKey) {
if m.AttemptFunc != nil {
m.AttemptFunc(key)
}
}
func (m *mockAddrBook) NeedNewAddresses() bool {
if m.NeedNewAddressesFunc != nil {
return m.NeedNewAddressesFunc()
}
return false
}
// SetUpdate sets the function to run on an issued update
func (m *mockAddrBook) SetUpdate(f func(n, addr *node.Info)) {
m.addAddressFunc = f
}
// SetLookupResult sets the result ok a lookup operation
func (m *mockAddrBook) SetLookupResult(node *node.Info, err error) {
m.lookupRes = node
m.lookupErr = err
}
// AddAddress mock
func (m *mockAddrBook) AddAddress(n, src *node.Info) {
if m.addAddressFunc != nil {
m.addAddressFunc(n, src)
}
}
// AddAddresses mock
func (m *mockAddrBook) AddAddresses(n []*node.Info, src *node.Info) {
if m.addAddressFunc != nil {
for _, addr := range n {
m.addAddressFunc(addr, src)
}
}
}
// AddressCache mock
func (m *mockAddrBook) AddressCache() []*node.Info {
if m.AddressCacheFunc != nil {
return m.AddressCacheFunc()
}
return nil
}
// Lookup mock
func (m *mockAddrBook) Lookup(pubkey p2pcrypto.PublicKey) (*node.Info, error) {
if m.LookupFunc != nil {
return m.LookupFunc(pubkey)
}
return m.lookupRes, m.lookupErr
}
// GetAddress mock
func (m *mockAddrBook) GetAddress() *KnownAddress {
if m.GetAddressFunc != nil {
return m.GetAddressFunc()
}
return m.GetAddressRes
}
// NumAddresses mock
func (m *mockAddrBook) NumAddresses() int {
//todo: mockAddrBook sizem
if m.NumAddressesFunc != nil {
return m.NumAddressesFunc()
}
return 0
}
type refresherMock struct {
BootstrapFunc func(ctx context.Context, minPeers int) error
}
func (r *refresherMock) Bootstrap(ctx context.Context, minPeers int) error {
if r.BootstrapFunc != nil {
return r.BootstrapFunc(ctx, minPeers)
}
return nil
}