net/conns better printing of connections
Juan Batiz-Benet committed
Dec 18, 2014 at 14:52 UTC
b61b281eae7263690a3673683f9e7b8b62496c25
4 files changed
+36
-18
net/mux.go
+1
-1
@@ -11,7 +11,7 @@ import (
11
lgbl "github.com/jbenet/go-ipfs/util/eventlog/loggables"
12
)
13
14
-var log = eventlog.Logger("mux2")
14
+var log = eventlog.Logger("network")
15
16
// Mux provides simple stream multixplexing.
17
// It helps you precisely when:
net/net.go
+4
@@ -42,6 +42,10 @@ func (s *stream) Write(p []byte) (n int, err error) {
42
43
type conn_ swarm.Conn
44
45
+func (s *conn_) String() string {
46
+ return s.SwarmConn().String()
47
+}
48
+
49
func (c *conn_) SwarmConn() *swarm.Conn {
50
return (*swarm.Conn)(c)
51
}
net/net_test.go
+27
-17
@@ -1,6 +1,7 @@
1
package net_test
2
3
import (
4
+ "fmt"
5
"testing"
6
7
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) {
35
36
// test those connected show up correctly
37
37
- testConnectedness := func(a, b inet.Network, c inet.Connectedness) {
38
- if a.Connectedness(b.LocalPeer()) != c {
39
- t.Error("%s is connected to %s, but Connectedness incorrect", a, b)
40
- }
41
-
42
- // test symmetric case
43
- if b.Connectedness(a.LocalPeer()) != c {
44
- t.Error("%s is connected to %s, but Connectedness incorrect", a, b)
45
- }
46
- }
47
-
38
// test connected
49
- testConnectedness(nets[0], nets[1], inet.Connected)
50
- testConnectedness(nets[0], nets[3], inet.Connected)
51
- testConnectedness(nets[1], nets[2], inet.Connected)
52
- testConnectedness(nets[3], nets[2], inet.Connected)
39
+ testConnectedness(t, nets[0], nets[1], inet.Connected)
40
+ testConnectedness(t, nets[0], nets[3], inet.Connected)
41
+ testConnectedness(t, nets[1], nets[2], inet.Connected)
42
+ testConnectedness(t, nets[3], nets[2], inet.Connected)
43
44
// test not connected
55
- testConnectedness(nets[0], nets[2], inet.NotConnected)
56
- testConnectedness(nets[1], nets[3], inet.NotConnected)
45
+ testConnectedness(t, nets[0], nets[2], inet.NotConnected)
46
+ testConnectedness(t, nets[1], nets[3], inet.NotConnected)
47
48
for _, n := range nets {
49
n.Close()
50
}
51
}
52
+
53
+func testConnectedness(t *testing.T, a, b inet.Network, c inet.Connectedness) {
54
+ es := "%s is connected to %s, but Connectedness incorrect. %s %s"
55
+ if a.Connectedness(b.LocalPeer()) != c {
56
+ t.Errorf(es, a, b, printConns(a), printConns(b))
57
+ }
58
+
59
+ // test symmetric case
60
+ if b.Connectedness(a.LocalPeer()) != c {
61
+ t.Errorf(es, b, a, printConns(b), printConns(a))
62
+ }
63
+}
64
+
65
+func printConns(n inet.Network) string {
66
+ s := fmt.Sprintf("Connections in %s:\n", n)
67
+ for _, c := range n.Conns() {
68
+ s = s + fmt.Sprintf("- %s\n", c)
69
+ }
70
+ return s
71
+}
net/swarm/swarm_conn.go
+4
@@ -40,6 +40,10 @@ func (c *Conn) RawConn() conn.Conn {
40
return (*ps.Conn)(c).NetConn().(conn.Conn)
41
}
42
43
+func (c *Conn) String() string {
44
+ return fmt.Sprintf("<SwarmConn %s>", c.RawConn())
45
+}
46
+
47
// LocalMultiaddr is the Multiaddr on this side
48
func (c *Conn) LocalMultiaddr() ma.Multiaddr {
49
return c.RawConn().LocalMultiaddr()