-
Notifications
You must be signed in to change notification settings - Fork 4
/
cached_addr_book_test.go
235 lines (201 loc) · 5.62 KB
/
cached_addr_book_test.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
package main
import (
"context"
"fmt"
"testing"
"time"
"github.com/libp2p/go-libp2p/core/event"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/p2p/host/eventbus"
ma "github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCachedAddrBook(t *testing.T) {
// Create a new cached address book
cab, err := newCachedAddrBook(WithAllowPrivateIPs())
require.NoError(t, err)
require.NotNil(t, cab)
require.NotNil(t, cab.peerCache)
require.NotNil(t, cab.addrBook)
}
func TestBackground(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Create a real event bus
eventBus := eventbus.NewBus()
emitter, err := eventBus.Emitter(new(event.EvtPeerIdentificationCompleted), eventbus.Stateful)
require.NoError(t, err)
// Use a mock host with a real event bus
mockHost := &mockHost{
eventBus: eventBus,
}
cab, err := newCachedAddrBook(WithAllowPrivateIPs())
require.NoError(t, err)
ctx, cancel = context.WithTimeout(ctx, time.Second*5)
defer cancel()
go cab.background(ctx, mockHost)
// Create a test peer
testPeer, err := peer.Decode("12D3KooWCZ67sU8oCvKd82Y6c9NgpqgoZYuZEUcg4upHCjK3n1aj")
require.NoError(t, err)
// Create test address
addr, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/1234")
require.NoError(t, err)
// Emit a real peer identification event
err = emitter.Emit(event.EvtPeerIdentificationCompleted{
Peer: testPeer,
Conn: &mockConnection{
remoteAddr: addr,
},
ListenAddrs: []ma.Multiaddr{addr},
})
require.NoError(t, err)
// Wait for the peer to be added to the cache
require.Eventually(t, func() bool {
_, exists := cab.peerCache.Get(testPeer)
return exists
}, time.Second*3, time.Millisecond*100, "peer was not added to cache")
// Verify peer state
pState, exists := cab.peerCache.Get(testPeer)
assert.True(t, exists)
assert.NotNil(t, pState)
}
func TestProbePeers(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Create a test libp2p host
mockHost := &mockHost{}
cab, err := newCachedAddrBook(WithAllowPrivateIPs())
require.NoError(t, err)
// Add a test peer with some addresses
testPeer, _ := peer.Decode("12D3KooWCZ67sU8oCvKd82Y6c9NgpqgoZYuZEUcg4upHCjK3n1aj")
addr, _ := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/1234")
cab.addrBook.AddAddrs(testPeer, []ma.Multiaddr{addr}, time.Hour)
// Initialize peer state with old connection time
cab.peerCache.Add(testPeer, peerState{
lastConnTime: time.Now().Add(-2 * PeerProbeThreshold),
})
// Run probe with mockHost instead of h
cab.probePeers(ctx, mockHost)
// Verify connect failures increased
pState, exists := cab.peerCache.Get(testPeer)
assert.True(t, exists)
assert.Equal(t, pState.connectFailures, uint(1))
}
func TestShouldProbePeer(t *testing.T) {
t.Parallel()
cab, err := newCachedAddrBook()
require.NoError(t, err)
testPeer := peer.ID("test-peer")
tests := []struct {
name string
peerState peerState
expectedResult bool
}{
{
name: "peer not in cache",
peerState: peerState{},
expectedResult: true,
},
{
name: "no failures, within threshold",
peerState: peerState{
lastFailedConnTime: time.Now().Add(-30 * time.Minute),
connectFailures: 0,
},
expectedResult: false,
},
{
name: "no failures, beyond threshold",
peerState: peerState{
lastFailedConnTime: time.Now().Add(-2 * PeerProbeThreshold),
connectFailures: 0,
},
expectedResult: true,
},
{
name: "one failure, within backoff",
peerState: peerState{
lastFailedConnTime: time.Now().Add(-90 * time.Minute),
connectFailures: 1,
},
expectedResult: true,
},
{
name: "one failure, beyond backoff",
peerState: peerState{
lastFailedConnTime: time.Now().Add(-3 * PeerProbeThreshold),
connectFailures: 1,
},
expectedResult: true,
},
{
name: "two failures, within backoff",
peerState: peerState{
lastFailedConnTime: time.Now().Add(-90 * time.Minute),
connectFailures: 2,
},
expectedResult: false,
},
{
name: "two failures, beyond backoff",
peerState: peerState{
lastFailedConnTime: time.Now().Add(-3 * PeerProbeThreshold),
connectFailures: 2,
},
expectedResult: true,
},
{
name: "never failed connection",
peerState: peerState{
lastFailedConnTime: time.Time{}, // zero time
connectFailures: 0,
},
expectedResult: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.peerState != (peerState{}) {
cab.peerCache.Add(testPeer, tt.peerState)
}
result := cab.ShouldProbePeer(testPeer)
assert.Equal(t, tt.expectedResult, result,
"expected ShouldProbePeer to return %v for case: %s",
tt.expectedResult, tt.name)
})
}
}
// Mock connection for testing
type mockConnection struct {
network.Conn
remoteAddr ma.Multiaddr
}
func (mc *mockConnection) RemoteMultiaddr() ma.Multiaddr {
return mc.remoteAddr
}
type mockHost struct {
host.Host
eventBus event.Bus
}
func (mh *mockHost) Connect(ctx context.Context, pi peer.AddrInfo) error {
// Simulate connection failure
return fmt.Errorf("mock connection failure")
}
// Add Network method to mockHost
func (mh *mockHost) Network() network.Network {
return &mockNetwork{}
}
// Add mockNetwork implementation
type mockNetwork struct {
network.Network
}
func (mn *mockNetwork) Connectedness(p peer.ID) network.Connectedness {
// Simulate not connected state
return network.NotConnected
}
func (mh *mockHost) EventBus() event.Bus {
return mh.eventBus
}