swarm_test.go 3.08 KB
Newer Older
1 2 3
package swarm

import (
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
4
	"bytes"
5 6
	"fmt"
	"testing"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
7
	"time"
8

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
9
	ci "github.com/jbenet/go-ipfs/crypto"
10 11 12 13 14 15 16 17
	msg "github.com/jbenet/go-ipfs/net/message"
	peer "github.com/jbenet/go-ipfs/peer"
	u "github.com/jbenet/go-ipfs/util"

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
18
func pong(ctx context.Context, swarm *Swarm) {
19
	for {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
20 21
		select {
		case <-ctx.Done():
22
			return
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
23 24 25 26 27
		case m1 := <-swarm.Incoming:
			if bytes.Equal(m1.Data(), []byte("ping")) {
				m2 := msg.New(m1.Peer(), []byte("pong"))
				swarm.Outgoing <- m2
			}
28 29 30 31
		}
	}
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
32
func setupPeer(t *testing.T, addr string) *peer.Peer {
33 34
	tcp, err := ma.NewMultiaddr(addr)
	if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
35
		t.Fatal(err)
36 37
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38
	sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512)
39
	if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
40
		t.Fatal(err)
41 42
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
43
	id, err := peer.IDFromPubKey(pk)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
44 45 46
	if err != nil {
		t.Fatal(err)
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
47 48

	p := &peer.Peer{ID: id}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
49 50
	p.PrivKey = sk
	p.PubKey = pk
51
	p.AddAddress(tcp)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
52
	return p, nil
53 54
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
55 56
func makeSwarms(ctx context.Context, t *testing.T, peers map[string]string) []*Swarm {
	swarms := []*Swarm{}
57

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
58
	for key, addr := range peers {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
59
		local := setupPeer(t, addr)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
60 61 62 63 64 65
		peerstore := peer.NewPeerstore()
		swarm, err := NewSwarm(ctx, local, peerstore)
		if err != nil {
			t.Fatal(err)
		}
		swarms = append(swarms, swarm)
66 67
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
68 69
	return swarms
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
70

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
71 72 73
func TestSwarm(t *testing.T) {
	peers := map[string]string{
		"11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a30": "/ip4/127.0.0.1/tcp/1234",
74 75 76
		"11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a31": "/ip4/127.0.0.1/tcp/2345",
		"11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a32": "/ip4/127.0.0.1/tcp/3456",
		"11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33": "/ip4/127.0.0.1/tcp/4567",
77
		"11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a34": "/ip4/127.0.0.1/tcp/5678",
78 79
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
80 81 82 83 84 85
	ctx := context.Background()
	swarms := makeSwarms(ctx, t, peers)

	// connect everyone
	for _, s := range swarms {
		peers, err := s.peers.All()
86
		if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
87
			t.Fatal(err)
88
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
89 90 91 92 93 94 95

		for _, p := range *peers {
			fmt.Println("dialing")
			if _, err := s.Dial(p); err != nil {
				t.Fatal("error swarm dialing to peer", err)
			}
			fmt.Println("dialed")
96
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
97 98 99 100 101 102 103 104 105 106 107 108 109
	}

	// ping/pong
	for _, s1 := range swarms {
		ctx, cancel := context.WithCancel(ctx)

		// setup all others to pong
		for _, s2 := range swarms {
			if s1 == s2 {
				continue
			}

			go pong(ctx, s2)
110 111
		}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
112
		peers, err := s1.peers.All()
113
		if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
114
			t.Fatal(err)
115 116
		}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
117 118 119 120 121
		MsgNum := 1000
		for k := 0; k < MsgNum; k++ {
			for _, p := range *peers {
				s1.Outgoing <- msg.New(p, []byte("ping"))
			}
122 123
		}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
124 125 126 127 128 129
		got := map[u.Key]int{}
		for k := 0; k < (MsgNum * len(*peers)); k++ {
			msg := <-s1.Incoming
			if string(msg.Data()) != "pong" {
				t.Error("unexpected conn output", msg.Data)
			}
130

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
131 132
			n, _ := got[msg.Peer().Key()]
			got[msg.Peer().Key()] = n + 1
133 134
		}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
135 136 137
		if len(*peers) != len(got) {
			t.Error("got less messages than sent")
		}
138

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
139 140 141 142
		for p, n := range got {
			if n != MsgNum {
				t.Error("peer did not get all msgs", p, n, "/", MsgNum)
			}
143
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
144 145 146

		cancel()
		<-time.After(50 * time.Millisecond)
147 148
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
149 150
	for _, s := range swarms {
		s.Close()
151 152
	}
}