Commit b61b281e authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

net/conns better printing of connections

parent 8b4c7cf3
...@@ -11,7 +11,7 @@ import ( ...@@ -11,7 +11,7 @@ import (
lgbl "github.com/jbenet/go-ipfs/util/eventlog/loggables" lgbl "github.com/jbenet/go-ipfs/util/eventlog/loggables"
) )
var log = eventlog.Logger("mux2") var log = eventlog.Logger("network")
// Mux provides simple stream multixplexing. // Mux provides simple stream multixplexing.
// It helps you precisely when: // It helps you precisely when:
......
...@@ -42,6 +42,10 @@ func (s *stream) Write(p []byte) (n int, err error) { ...@@ -42,6 +42,10 @@ func (s *stream) Write(p []byte) (n int, err error) {
type conn_ swarm.Conn type conn_ swarm.Conn
func (s *conn_) String() string {
return s.SwarmConn().String()
}
func (c *conn_) SwarmConn() *swarm.Conn { func (c *conn_) SwarmConn() *swarm.Conn {
return (*swarm.Conn)(c) return (*swarm.Conn)(c)
} }
......
package net_test package net_test
import ( import (
"fmt"
"testing" "testing"
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
...@@ -34,28 +35,37 @@ func TestConnectednessCorrect(t *testing.T) { ...@@ -34,28 +35,37 @@ func TestConnectednessCorrect(t *testing.T) {
// test those connected show up correctly // test those connected show up correctly
testConnectedness := func(a, b inet.Network, c inet.Connectedness) {
if a.Connectedness(b.LocalPeer()) != c {
t.Error("%s is connected to %s, but Connectedness incorrect", a, b)
}
// test symmetric case
if b.Connectedness(a.LocalPeer()) != c {
t.Error("%s is connected to %s, but Connectedness incorrect", a, b)
}
}
// test connected // test connected
testConnectedness(nets[0], nets[1], inet.Connected) testConnectedness(t, nets[0], nets[1], inet.Connected)
testConnectedness(nets[0], nets[3], inet.Connected) testConnectedness(t, nets[0], nets[3], inet.Connected)
testConnectedness(nets[1], nets[2], inet.Connected) testConnectedness(t, nets[1], nets[2], inet.Connected)
testConnectedness(nets[3], nets[2], inet.Connected) testConnectedness(t, nets[3], nets[2], inet.Connected)
// test not connected // test not connected
testConnectedness(nets[0], nets[2], inet.NotConnected) testConnectedness(t, nets[0], nets[2], inet.NotConnected)
testConnectedness(nets[1], nets[3], inet.NotConnected) testConnectedness(t, nets[1], nets[3], inet.NotConnected)
for _, n := range nets { for _, n := range nets {
n.Close() n.Close()
} }
} }
func testConnectedness(t *testing.T, a, b inet.Network, c inet.Connectedness) {
es := "%s is connected to %s, but Connectedness incorrect. %s %s"
if a.Connectedness(b.LocalPeer()) != c {
t.Errorf(es, a, b, printConns(a), printConns(b))
}
// test symmetric case
if b.Connectedness(a.LocalPeer()) != c {
t.Errorf(es, b, a, printConns(b), printConns(a))
}
}
func printConns(n inet.Network) string {
s := fmt.Sprintf("Connections in %s:\n", n)
for _, c := range n.Conns() {
s = s + fmt.Sprintf("- %s\n", c)
}
return s
}
...@@ -40,6 +40,10 @@ func (c *Conn) RawConn() conn.Conn { ...@@ -40,6 +40,10 @@ func (c *Conn) RawConn() conn.Conn {
return (*ps.Conn)(c).NetConn().(conn.Conn) return (*ps.Conn)(c).NetConn().(conn.Conn)
} }
func (c *Conn) String() string {
return fmt.Sprintf("<SwarmConn %s>", c.RawConn())
}
// LocalMultiaddr is the Multiaddr on this side // LocalMultiaddr is the Multiaddr on this side
func (c *Conn) LocalMultiaddr() ma.Multiaddr { func (c *Conn) LocalMultiaddr() ma.Multiaddr {
return c.RawConn().LocalMultiaddr() return c.RawConn().LocalMultiaddr()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment