swarm_test.go 3.19 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 18
	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"
	mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
)

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

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

	mh, err := mh.FromHexString(id)
	if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
41
		t.Fatal(err)
42 43 44
	}

	p := &peer.Peer{ID: peer.ID(mh)}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
45 46 47 48 49 50 51 52

	sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512)
	if err != nil {
		t.Fatal(err)
	}
	p.PrivKey = sk
	p.PubKey = pk

53
	p.AddAddress(tcp)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
54
	return p
55 56
}

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

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
70 71
	return swarms
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
72

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

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

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

		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")
98
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
99 100 101 102 103 104 105 106 107 108 109 110 111
	}

	// 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)
112 113
		}

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

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
126 127 128 129 130 131
		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)
			}
132

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

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

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

		cancel()
		<-time.After(50 * time.Millisecond)
149 150
	}

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