Skip to content

Commit 6f4d058

Browse files
authored
Merge pull request #22 from wetterj/master
Improve Test Coverage
2 parents c5de9b3 + 2e2a793 commit 6f4d058

File tree

2 files changed

+234
-1
lines changed

2 files changed

+234
-1
lines changed

p2p/net/connmgr/connmgr.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var log = logging.Logger("connmgr")
2020
// to trimming. Trims are automatically run on demand, only if the time from the
2121
// previous trim is higher than 10 seconds. Furthermore, trims can be explicitly
2222
// requested through the public interface of this struct (see TrimOpenConns).
23-
23+
//
2424
// See configuration parameters in NewConnManager.
2525
type BasicConnMgr struct {
2626
highWater int
@@ -129,6 +129,8 @@ func (cm *BasicConnMgr) getConnsToClose(ctx context.Context) []inet.Conn {
129129
return closed
130130
}
131131

132+
// GetTagInfo is called to fetch the tag information associated with a given
133+
// peer, nil is returned if p refers to an unknown peer.
132134
func (cm *BasicConnMgr) GetTagInfo(p peer.ID) *ifconnmgr.TagInfo {
133135
cm.lk.Lock()
134136
defer cm.lk.Unlock()
@@ -155,6 +157,7 @@ func (cm *BasicConnMgr) GetTagInfo(p peer.ID) *ifconnmgr.TagInfo {
155157
return out
156158
}
157159

160+
// TagPeer is called to associate a string and integer with a given peer.
158161
func (cm *BasicConnMgr) TagPeer(p peer.ID, tag string, val int) {
159162
cm.lk.Lock()
160163
defer cm.lk.Unlock()
@@ -170,6 +173,7 @@ func (cm *BasicConnMgr) TagPeer(p peer.ID, tag string, val int) {
170173
pi.tags[tag] = val
171174
}
172175

176+
// UntagPeer is called to disassociate a string and integer from a given peer.
173177
func (cm *BasicConnMgr) UntagPeer(p peer.ID, tag string) {
174178
cm.lk.Lock()
175179
defer cm.lk.Unlock()

p2p/net/connmgr/connmgr_test.go

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package connmgr
33
import (
44
"context"
55
"testing"
6+
"time"
67

78
inet "github.com/libp2p/go-libp2p-net"
89
peer "github.com/libp2p/go-libp2p-peer"
910
tu "github.com/libp2p/go-testutil"
11+
ma "github.com/multiformats/go-multiaddr"
1012
)
1113

1214
type tconn struct {
@@ -24,6 +26,14 @@ func (c *tconn) RemotePeer() peer.ID {
2426
return c.peer
2527
}
2628

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+
2737
func randConn(t *testing.T) inet.Conn {
2838
pid := tu.RandPeerIDFatal(t)
2939
return &tconn{peer: pid}
@@ -65,3 +75,222 @@ func TestConnTrimming(t *testing.T) {
6575
t.Fatal("conn with bad tag should have gotten closed")
6676
}
6777
}
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

Comments
 (0)