@cryptotaxi247 / kubo / commits / 4d9f1f0fd

net: Connectedness bugfix

Connectedness was totally incorrect. added a test case.

Juan Batiz-Benet committed Dec 18, 2014 at 12:36 UTC 4d9f1f0fdf1b31fe23460e12e1b76dea4ec9de26
2 files changed +68 -1
net/net.go
+7 -1
@@ -2,6 +2,8 @@
2 package net
3
4 import (
5 + "fmt"
6 +
7 ic "github.com/jbenet/go-ipfs/crypto"
8 swarm "github.com/jbenet/go-ipfs/net/swarm"
9 peer "github.com/jbenet/go-ipfs/peer"
@@ -234,7 +236,7 @@ func (n *network) InterfaceListenAddresses() ([]ma.Multiaddr, error) {
236 // For now only returns Connected || NotConnected. Expand into more later.
237 func (n *network) Connectedness(p peer.ID) Connectedness {
238 c := n.swarm.ConnectionsToPeer(p)
237 - if c != nil && len(c) < 1 {
239 + if c != nil && len(c) > 0 {
240 return Connected
241 }
242 return NotConnected
@@ -266,6 +268,10 @@ func (n *network) SetHandler(p ProtocolID, h StreamHandler) {
268 n.mux.SetHandler(p, h)
269 }
270
271 +func (n *network) String() string {
272 + return fmt.Sprintf("<Network %s>", n.LocalPeer())
273 +}
274 +
275 func (n *network) IdentifyProtocol() *IDService {
276 return n.ids
277 }
net/net_test.go new
+61
@@ -0,0 +1,61 @@
1 +package net_test
2 +
3 +import (
4 + "testing"
5 +
6 + context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
7 + inet "github.com/jbenet/go-ipfs/net"
8 +)
9 +
10 +// TestConnectednessCorrect starts a few networks, connects a few
11 +// and tests Connectedness value is correct.
12 +func TestConnectednessCorrect(t *testing.T) {
13 +
14 + ctx := context.Background()
15 +
16 + nets := make([]inet.Network, 4)
17 + for i := 0; i < 4; i++ {
18 + nets[i] = GenNetwork(t, ctx)
19 + }
20 +
21 + // connect 0-1, 0-2, 0-3, 1-2, 2-3
22 +
23 + dial := func(a, b inet.Network) {
24 + DivulgeAddresses(b, a)
25 + if err := a.DialPeer(ctx, b.LocalPeer()); err != nil {
26 + t.Fatalf("Failed to dial: %s", err)
27 + }
28 + }
29 +
30 + dial(nets[0], nets[1])
31 + dial(nets[0], nets[3])
32 + dial(nets[1], nets[2])
33 + dial(nets[3], nets[2])
34 +
35 + // test those connected show up correctly
36 +
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 +
48 + // 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)
53 +
54 + // test not connected
55 + testConnectedness(nets[0], nets[2], inet.NotConnected)
56 + testConnectedness(nets[1], nets[3], inet.NotConnected)
57 +
58 + for _, n := range nets {
59 + n.Close()
60 + }
61 +}