net_test.go 1.73 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3
package net_test

import (
4
	"fmt"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
	"testing"

	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
	inet "github.com/jbenet/go-ipfs/net"
)

// TestConnectednessCorrect starts a few networks, connects a few
// and tests Connectedness value is correct.
func TestConnectednessCorrect(t *testing.T) {

	ctx := context.Background()

	nets := make([]inet.Network, 4)
	for i := 0; i < 4; i++ {
		nets[i] = GenNetwork(t, ctx)
	}

	// connect 0-1, 0-2, 0-3, 1-2, 2-3

	dial := func(a, b inet.Network) {
		DivulgeAddresses(b, a)
		if err := a.DialPeer(ctx, b.LocalPeer()); err != nil {
			t.Fatalf("Failed to dial: %s", err)
		}
	}

	dial(nets[0], nets[1])
	dial(nets[0], nets[3])
	dial(nets[1], nets[2])
	dial(nets[3], nets[2])

	// test those connected show up correctly

	// test connected
39 40 41 42
	testConnectedness(t, nets[0], nets[1], inet.Connected)
	testConnectedness(t, nets[0], nets[3], inet.Connected)
	testConnectedness(t, nets[1], nets[2], inet.Connected)
	testConnectedness(t, nets[3], nets[2], inet.Connected)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
43 44

	// test not connected
45 46
	testConnectedness(t, nets[0], nets[2], inet.NotConnected)
	testConnectedness(t, nets[1], nets[3], inet.NotConnected)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
47 48 49 50 51

	for _, n := range nets {
		n.Close()
	}
}
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

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
}