@@ -3,10 +3,12 @@ package connmgr
3
3
import (
4
4
"context"
5
5
"testing"
6
+ "time"
6
7
7
8
inet "github.com/libp2p/go-libp2p-net"
8
9
peer "github.com/libp2p/go-libp2p-peer"
9
10
tu "github.com/libp2p/go-testutil"
11
+ ma "github.com/multiformats/go-multiaddr"
10
12
)
11
13
12
14
type tconn struct {
@@ -24,6 +26,14 @@ func (c *tconn) RemotePeer() peer.ID {
24
26
return c .peer
25
27
}
26
28
29
+ func (c * tconn ) RemoteMultiaddr () ma.Multiaddr {
30
+ addr , err := ma .NewMultiaddr ("/ip4/127.0.0.1/udp/1234" )
31
+ if err != nil {
32
+ panic ("cannot create multiaddr" )
33
+ }
34
+ return addr
35
+ }
36
+
27
37
func randConn (t * testing.T ) inet.Conn {
28
38
pid := tu .RandPeerIDFatal (t )
29
39
return & tconn {peer : pid }
@@ -65,3 +75,222 @@ func TestConnTrimming(t *testing.T) {
65
75
t .Fatal ("conn with bad tag should have gotten closed" )
66
76
}
67
77
}
78
+
79
+ func TestConnsToClose (t * testing.T ) {
80
+ cm := NewConnManager (0 , 10 , 0 )
81
+ conns := cm .getConnsToClose (context .Background ())
82
+ if conns != nil {
83
+ t .Fatal ("expected no connections" )
84
+ }
85
+
86
+ cm = NewConnManager (10 , 0 , 0 )
87
+ conns = cm .getConnsToClose (context .Background ())
88
+ if conns != nil {
89
+ t .Fatal ("expected no connections" )
90
+ }
91
+
92
+ cm = NewConnManager (1 , 1 , 0 )
93
+ conns = cm .getConnsToClose (context .Background ())
94
+ if conns != nil {
95
+ t .Fatal ("expected no connections" )
96
+ }
97
+
98
+ cm = NewConnManager (1 , 1 , time .Duration (10 * time .Minute ))
99
+ not := cm .Notifee ()
100
+ for i := 0 ; i < 5 ; i ++ {
101
+ conn := randConn (t )
102
+ not .Connected (nil , conn )
103
+ }
104
+ conns = cm .getConnsToClose (context .Background ())
105
+ if len (conns ) != 0 {
106
+ t .Fatal ("expected no connections" )
107
+ }
108
+ }
109
+
110
+ func TestGetTagInfo (t * testing.T ) {
111
+ start := time .Now ()
112
+ cm := NewConnManager (1 , 1 , time .Duration (10 * time .Minute ))
113
+ not := cm .Notifee ()
114
+ conn := randConn (t )
115
+ not .Connected (nil , conn )
116
+ end := time .Now ()
117
+
118
+ other := tu .RandPeerIDFatal (t )
119
+ tag := cm .GetTagInfo (other )
120
+ if tag != nil {
121
+ t .Fatal ("expected no tag" )
122
+ }
123
+
124
+ tag = cm .GetTagInfo (conn .RemotePeer ())
125
+ if tag == nil {
126
+ t .Fatal ("expected tag" )
127
+ }
128
+ if tag .FirstSeen .Before (start ) || tag .FirstSeen .After (end ) {
129
+ t .Fatal ("expected first seen time" )
130
+ }
131
+ if tag .Value != 0 {
132
+ t .Fatal ("expected zero value" )
133
+ }
134
+ if len (tag .Tags ) != 0 {
135
+ t .Fatal ("expected no tags" )
136
+ }
137
+ if len (tag .Conns ) != 1 {
138
+ t .Fatal ("expected one connection" )
139
+ }
140
+ for s , tm := range tag .Conns {
141
+ if s != conn .RemoteMultiaddr ().String () {
142
+ t .Fatal ("unexpected multiaddr" )
143
+ }
144
+ if tm .Before (start ) || tm .After (end ) {
145
+ t .Fatal ("unexpected connection time" )
146
+ }
147
+ }
148
+
149
+ cm .TagPeer (conn .RemotePeer (), "tag" , 5 )
150
+ tag = cm .GetTagInfo (conn .RemotePeer ())
151
+ if tag == nil {
152
+ t .Fatal ("expected tag" )
153
+ }
154
+ if tag .FirstSeen .Before (start ) || tag .FirstSeen .After (end ) {
155
+ t .Fatal ("expected first seen time" )
156
+ }
157
+ if tag .Value != 5 {
158
+ t .Fatal ("expected five value" )
159
+ }
160
+ if len (tag .Tags ) != 1 {
161
+ t .Fatal ("expected no tags" )
162
+ }
163
+ for tString , v := range tag .Tags {
164
+ if tString != "tag" || v != 5 {
165
+ t .Fatal ("expected tag value" )
166
+ }
167
+ }
168
+ if len (tag .Conns ) != 1 {
169
+ t .Fatal ("expected one connection" )
170
+ }
171
+ for s , tm := range tag .Conns {
172
+ if s != conn .RemoteMultiaddr ().String () {
173
+ t .Fatal ("unexpected multiaddr" )
174
+ }
175
+ if tm .Before (start ) || tm .After (end ) {
176
+ t .Fatal ("unexpected connection time" )
177
+ }
178
+ }
179
+ }
180
+
181
+ func TestTagPeerNonExistant (t * testing.T ) {
182
+ cm := NewConnManager (1 , 1 , time .Duration (10 * time .Minute ))
183
+
184
+ id := tu .RandPeerIDFatal (t )
185
+ cm .TagPeer (id , "test" , 1 )
186
+
187
+ if len (cm .peers ) != 0 {
188
+ t .Fatal ("expected zero peers" )
189
+ }
190
+ }
191
+
192
+ func TestUntagPeer (t * testing.T ) {
193
+ cm := NewConnManager (1 , 1 , time .Duration (10 * time .Minute ))
194
+ not := cm .Notifee ()
195
+ conn := randConn (t )
196
+ not .Connected (nil , conn )
197
+ rp := conn .RemotePeer ()
198
+ cm .TagPeer (rp , "tag" , 5 )
199
+ cm .TagPeer (rp , "tag two" , 5 )
200
+
201
+ id := tu .RandPeerIDFatal (t )
202
+ cm .UntagPeer (id , "test" )
203
+ if len (cm .peers [rp ].tags ) != 2 {
204
+ t .Fatal ("expected tags to be uneffected" )
205
+ }
206
+
207
+ cm .UntagPeer (conn .RemotePeer (), "test" )
208
+ if len (cm .peers [rp ].tags ) != 2 {
209
+ t .Fatal ("expected tags to be uneffected" )
210
+ }
211
+
212
+ cm .UntagPeer (conn .RemotePeer (), "tag" )
213
+ if len (cm .peers [rp ].tags ) != 1 {
214
+ t .Fatal ("expected tag to be removed" )
215
+ }
216
+ if cm .peers [rp ].value != 5 {
217
+ t .Fatal ("expected aggreagte tag value to be 5" )
218
+ }
219
+ }
220
+
221
+ func TestGetInfo (t * testing.T ) {
222
+ start := time .Now ()
223
+ gp := time .Duration (10 * time .Minute )
224
+ cm := NewConnManager (1 , 5 , gp )
225
+ not := cm .Notifee ()
226
+ conn := randConn (t )
227
+ not .Connected (nil , conn )
228
+ cm .TrimOpenConns (context .Background ())
229
+ end := time .Now ()
230
+
231
+ info := cm .GetInfo ()
232
+ if info .HighWater != 5 {
233
+ t .Fatal ("expected highwater to be 5" )
234
+ }
235
+ if info .LowWater != 1 {
236
+ t .Fatal ("expected highwater to be 1" )
237
+ }
238
+ if info .LastTrim .Before (start ) || info .LastTrim .After (end ) {
239
+ t .Fatal ("unexpected last trim time" )
240
+ }
241
+ if info .GracePeriod != gp {
242
+ t .Fatal ("unexpected grace period" )
243
+ }
244
+ if info .ConnCount != 1 {
245
+ t .Fatal ("unexpected number of connections" )
246
+ }
247
+ }
248
+
249
+ func TestDoubleConnection (t * testing.T ) {
250
+ gp := time .Duration (10 * time .Minute )
251
+ cm := NewConnManager (1 , 5 , gp )
252
+ not := cm .Notifee ()
253
+ conn := randConn (t )
254
+ not .Connected (nil , conn )
255
+ cm .TagPeer (conn .RemotePeer (), "foo" , 10 )
256
+ not .Connected (nil , conn )
257
+ if cm .connCount != 1 {
258
+ t .Fatal ("unexpected number of connections" )
259
+ }
260
+ if cm .peers [conn .RemotePeer ()].value != 10 {
261
+ t .Fatal ("unexpected peer value" )
262
+ }
263
+ }
264
+
265
+ func TestDisconnected (t * testing.T ) {
266
+ gp := time .Duration (10 * time .Minute )
267
+ cm := NewConnManager (1 , 5 , gp )
268
+ not := cm .Notifee ()
269
+ conn := randConn (t )
270
+ not .Connected (nil , conn )
271
+ cm .TagPeer (conn .RemotePeer (), "foo" , 10 )
272
+
273
+ not .Disconnected (nil , randConn (t ))
274
+ if cm .connCount != 1 {
275
+ t .Fatal ("unexpected number of connections" )
276
+ }
277
+ if cm .peers [conn .RemotePeer ()].value != 10 {
278
+ t .Fatal ("unexpected peer value" )
279
+ }
280
+
281
+ not .Disconnected (nil , & tconn {peer : conn .RemotePeer ()})
282
+ if cm .connCount != 1 {
283
+ t .Fatal ("unexpected number of connections" )
284
+ }
285
+ if cm .peers [conn .RemotePeer ()].value != 10 {
286
+ t .Fatal ("unexpected peer value" )
287
+ }
288
+
289
+ not .Disconnected (nil , conn )
290
+ if cm .connCount != 0 {
291
+ t .Fatal ("unexpected number of connections" )
292
+ }
293
+ if len (cm .peers ) != 0 {
294
+ t .Fatal ("unexpected number of peers" )
295
+ }
296
+ }
0 commit comments