From 2ae3cfd7ef9e91999bf9db309da58af7614ebb8b Mon Sep 17 00:00:00 2001 From: James Wetter Date: Mon, 17 Sep 2018 01:50:30 +0900 Subject: [PATCH 1/2] lift test coverage --- p2p/net/connmgr/connmgr_test.go | 229 ++++++++++++++++++++++++++++++++ 1 file changed, 229 insertions(+) diff --git a/p2p/net/connmgr/connmgr_test.go b/p2p/net/connmgr/connmgr_test.go index 8c30e9e5d9..cf11f208db 100644 --- a/p2p/net/connmgr/connmgr_test.go +++ b/p2p/net/connmgr/connmgr_test.go @@ -3,10 +3,12 @@ package connmgr import ( "context" "testing" + "time" inet "github.com/libp2p/go-libp2p-net" peer "github.com/libp2p/go-libp2p-peer" tu "github.com/libp2p/go-testutil" + ma "github.com/multiformats/go-multiaddr" ) type tconn struct { @@ -24,6 +26,14 @@ func (c *tconn) RemotePeer() peer.ID { return c.peer } +func (c *tconn) RemoteMultiaddr() ma.Multiaddr { + addr, err := ma.NewMultiaddr("/ip4/127.0.0.1/udp/1234") + if err != nil { + panic("cannot create multiaddr") + } + return addr +} + func randConn(t *testing.T) inet.Conn { pid := tu.RandPeerIDFatal(t) return &tconn{peer: pid} @@ -65,3 +75,222 @@ func TestConnTrimming(t *testing.T) { t.Fatal("conn with bad tag should have gotten closed") } } + +func TestConnsToClose(t *testing.T) { + cm := NewConnManager(0, 10, 0) + conns := cm.getConnsToClose(context.Background()) + if conns != nil { + t.Fatal("expected no connections") + } + + cm = NewConnManager(10, 0, 0) + conns = cm.getConnsToClose(context.Background()) + if conns != nil { + t.Fatal("expected no connections") + } + + cm = NewConnManager(1, 1, 0) + conns = cm.getConnsToClose(context.Background()) + if conns != nil { + t.Fatal("expected no connections") + } + + cm = NewConnManager(1, 1, time.Duration(10*time.Minute)) + not := cm.Notifee() + for i := 0; i < 5; i++ { + conn := randConn(t) + not.Connected(nil, conn) + } + conns = cm.getConnsToClose(context.Background()) + if len(conns) != 0 { + t.Fatal("expected no connections") + } +} + +func TestGetTagInfo(t *testing.T) { + start := time.Now() + cm := NewConnManager(1, 1, time.Duration(10*time.Minute)) + not := cm.Notifee() + conn := randConn(t) + not.Connected(nil, conn) + end := time.Now() + + other := tu.RandPeerIDFatal(t) + tag := cm.GetTagInfo(other) + if tag != nil { + t.Fatal("expected no tag") + } + + tag = cm.GetTagInfo(conn.RemotePeer()) + if tag == nil { + t.Fatal("expected tag") + } + if tag.FirstSeen.Before(start) || tag.FirstSeen.After(end) { + t.Fatal("expected first seen time") + } + if tag.Value != 0 { + t.Fatal("expected zero value") + } + if len(tag.Tags) != 0 { + t.Fatal("expected no tags") + } + if len(tag.Conns) != 1 { + t.Fatal("expected one connection") + } + for s, tm := range tag.Conns { + if s != conn.RemoteMultiaddr().String() { + t.Fatal("unexpected multiaddr") + } + if tm.Before(start) || tm.After(end) { + t.Fatal("unexpected connection time") + } + } + + cm.TagPeer(conn.RemotePeer(), "tag", 5) + tag = cm.GetTagInfo(conn.RemotePeer()) + if tag == nil { + t.Fatal("expected tag") + } + if tag.FirstSeen.Before(start) || tag.FirstSeen.After(end) { + t.Fatal("expected first seen time") + } + if tag.Value != 5 { + t.Fatal("expected five value") + } + if len(tag.Tags) != 1 { + t.Fatal("expected no tags") + } + for tString, v := range tag.Tags { + if tString != "tag" || v != 5 { + t.Fatal("expected tag value") + } + } + if len(tag.Conns) != 1 { + t.Fatal("expected one connection") + } + for s, tm := range tag.Conns { + if s != conn.RemoteMultiaddr().String() { + t.Fatal("unexpected multiaddr") + } + if tm.Before(start) || tm.After(end) { + t.Fatal("unexpected connection time") + } + } +} + +func TestTagPeerNonExistant(t *testing.T) { + cm := NewConnManager(1, 1, time.Duration(10*time.Minute)) + + id := tu.RandPeerIDFatal(t) + cm.TagPeer(id, "test", 1) + + if len(cm.peers) != 0 { + t.Fatal("expected zero peers") + } +} + +func TestUntagPeer(t *testing.T) { + cm := NewConnManager(1, 1, time.Duration(10*time.Minute)) + not := cm.Notifee() + conn := randConn(t) + not.Connected(nil, conn) + rp := conn.RemotePeer() + cm.TagPeer(rp, "tag", 5) + cm.TagPeer(rp, "tag two", 5) + + id := tu.RandPeerIDFatal(t) + cm.UntagPeer(id, "test") + if len(cm.peers[rp].tags) != 2 { + t.Fatal("expected tags to be uneffected") + } + + cm.UntagPeer(conn.RemotePeer(), "test") + if len(cm.peers[rp].tags) != 2 { + t.Fatal("expected tags to be uneffected") + } + + cm.UntagPeer(conn.RemotePeer(), "tag") + if len(cm.peers[rp].tags) != 1 { + t.Fatal("expected tag to be removed") + } + if cm.peers[rp].value != 5 { + t.Fatal("expected aggreagte tag value to be 5") + } +} + +func TestGetInfo(t *testing.T) { + start := time.Now() + gp := time.Duration(10 * time.Minute) + cm := NewConnManager(1, 5, gp) + not := cm.Notifee() + conn := randConn(t) + not.Connected(nil, conn) + cm.TrimOpenConns(context.Background()) + end := time.Now() + + info := cm.GetInfo() + if info.HighWater != 5 { + t.Fatal("expected highwater to be 5") + } + if info.LowWater != 1 { + t.Fatal("expected highwater to be 1") + } + if info.LastTrim.Before(start) || info.LastTrim.After(end) { + t.Fatal("unexpected last trim time") + } + if info.GracePeriod != gp { + t.Fatal("unexpected grace period") + } + if info.ConnCount != 1 { + t.Fatal("unexpected number of connections") + } +} + +func TestDoubleConnection(t *testing.T) { + gp := time.Duration(10 * time.Minute) + cm := NewConnManager(1, 5, gp) + not := cm.Notifee() + conn := randConn(t) + not.Connected(nil, conn) + cm.TagPeer(conn.RemotePeer(), "foo", 10) + not.Connected(nil, conn) + if cm.connCount != 1 { + t.Fatal("unexpected number of connections") + } + if cm.peers[conn.RemotePeer()].value != 10 { + t.Fatal("unexpected peer value") + } +} + +func TestDisconnected(t *testing.T) { + gp := time.Duration(10 * time.Minute) + cm := NewConnManager(1, 5, gp) + not := cm.Notifee() + conn := randConn(t) + not.Connected(nil, conn) + cm.TagPeer(conn.RemotePeer(), "foo", 10) + + not.Disconnected(nil, randConn(t)) + if cm.connCount != 1 { + t.Fatal("unexpected number of connections") + } + if cm.peers[conn.RemotePeer()].value != 10 { + t.Fatal("unexpected peer value") + } + + not.Disconnected(nil, &tconn{peer: conn.RemotePeer()}) + if cm.connCount != 1 { + t.Fatal("unexpected number of connections") + } + if cm.peers[conn.RemotePeer()].value != 10 { + t.Fatal("unexpected peer value") + } + + not.Disconnected(nil, conn) + if cm.connCount != 0 { + t.Fatal("unexpected number of connections") + } + if len(cm.peers) != 0 { + t.Fatal("unexpected number of peers") + } +} From 2e2a793e30d87e75cb76f09c51fe14f1e8452caa Mon Sep 17 00:00:00 2001 From: James Wetter Date: Mon, 17 Sep 2018 01:56:46 +0900 Subject: [PATCH 2/2] add comments for tag functions to silence golint --- p2p/net/connmgr/connmgr.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/p2p/net/connmgr/connmgr.go b/p2p/net/connmgr/connmgr.go index 56ca27ec59..8e7d5aaafb 100644 --- a/p2p/net/connmgr/connmgr.go +++ b/p2p/net/connmgr/connmgr.go @@ -20,7 +20,7 @@ var log = logging.Logger("connmgr") // to trimming. Trims are automatically run on demand, only if the time from the // previous trim is higher than 10 seconds. Furthermore, trims can be explicitly // requested through the public interface of this struct (see TrimOpenConns). - +// // See configuration parameters in NewConnManager. type BasicConnMgr struct { highWater int @@ -129,6 +129,8 @@ func (cm *BasicConnMgr) getConnsToClose(ctx context.Context) []inet.Conn { return closed } +// GetTagInfo is called to fetch the tag information associated with a given +// peer, nil is returned if p refers to an unknown peer. func (cm *BasicConnMgr) GetTagInfo(p peer.ID) *ifconnmgr.TagInfo { cm.lk.Lock() defer cm.lk.Unlock() @@ -155,6 +157,7 @@ func (cm *BasicConnMgr) GetTagInfo(p peer.ID) *ifconnmgr.TagInfo { return out } +// TagPeer is called to associate a string and integer with a given peer. func (cm *BasicConnMgr) TagPeer(p peer.ID, tag string, val int) { cm.lk.Lock() defer cm.lk.Unlock() @@ -170,6 +173,7 @@ func (cm *BasicConnMgr) TagPeer(p peer.ID, tag string, val int) { pi.tags[tag] = val } +// UntagPeer is called to disassociate a string and integer from a given peer. func (cm *BasicConnMgr) UntagPeer(p peer.ID, tag string) { cm.lk.Lock() defer cm.lk.Unlock()