mux_test.go 6.06 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3 4 5 6 7 8 9 10 11
package mux

import (
	"bytes"
	"fmt"
	"testing"
	"time"

	msg "github.com/jbenet/go-ipfs/net/message"
	peer "github.com/jbenet/go-ipfs/peer"
	u "github.com/jbenet/go-ipfs/util"
12
	mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13

14
	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
)

type TestProtocol struct {
	*msg.Pipe
}

func (t *TestProtocol) GetPipe() *msg.Pipe {
	return t.Pipe
}

func newPeer(t *testing.T, id string) *peer.Peer {
	mh, err := mh.FromHexString(id)
	if err != nil {
		t.Error(err)
		return nil
	}

	return &peer.Peer{ID: peer.ID(mh)}
}

35 36 37
func testMsg(t *testing.T, m msg.NetMessage, data []byte) {
	if !bytes.Equal(data, m.Data()) {
		t.Errorf("Data does not match: %v != %v", data, m.Data())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38 39 40
	}
}

41 42
func testWrappedMsg(t *testing.T, m msg.NetMessage, pid ProtocolID, data []byte) {
	data2, pid2, err := unwrapData(m.Data())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
	if err != nil {
		t.Error(err)
	}

	if pid != pid2 {
		t.Errorf("ProtocolIDs do not match: %v != %v", pid, pid2)
	}

	if !bytes.Equal(data, data2) {
		t.Errorf("Data does not match: %v != %v", data, data2)
	}
}

func TestSimpleMuxer(t *testing.T) {

	// setup
	p1 := &TestProtocol{Pipe: msg.NewPipe(10)}
	p2 := &TestProtocol{Pipe: msg.NewPipe(10)}
	pid1 := ProtocolID_Test
	pid2 := ProtocolID_Routing
	mux1 := &Muxer{
		Pipe: msg.NewPipe(10),
		Protocols: ProtocolMap{
			pid1: p1,
			pid2: p2,
		},
	}
	peer1 := newPeer(t, "11140beec7b5ea3f0fdbc95d0dd47f3c5bc275aaaaaa")
	// peer2 := newPeer(t, "11140beec7b5ea3f0fdbc95d0dd47f3c5bc275bbbbbb")

	// run muxer
	ctx := context.Background()
	mux1.Start(ctx)

	// test outgoing p1
	for _, s := range []string{"foo", "bar", "baz"} {
79
		p1.Outgoing <- msg.New(peer1, []byte(s))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
80 81 82 83 84 85 86 87 88
		testWrappedMsg(t, <-mux1.Outgoing, pid1, []byte(s))
	}

	// test incoming p1
	for _, s := range []string{"foo", "bar", "baz"} {
		d, err := wrapData([]byte(s), pid1)
		if err != nil {
			t.Error(err)
		}
89
		mux1.Incoming <- msg.New(peer1, d)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
90 91 92 93 94
		testMsg(t, <-p1.Incoming, []byte(s))
	}

	// test outgoing p2
	for _, s := range []string{"foo", "bar", "baz"} {
95
		p2.Outgoing <- msg.New(peer1, []byte(s))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
96 97 98 99 100 101 102 103 104
		testWrappedMsg(t, <-mux1.Outgoing, pid2, []byte(s))
	}

	// test incoming p2
	for _, s := range []string{"foo", "bar", "baz"} {
		d, err := wrapData([]byte(s), pid2)
		if err != nil {
			t.Error(err)
		}
105
		mux1.Incoming <- msg.New(peer1, d)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
		testMsg(t, <-p2.Incoming, []byte(s))
	}
}

func TestSimultMuxer(t *testing.T) {

	// setup
	p1 := &TestProtocol{Pipe: msg.NewPipe(10)}
	p2 := &TestProtocol{Pipe: msg.NewPipe(10)}
	pid1 := ProtocolID_Test
	pid2 := ProtocolID_Identify
	mux1 := &Muxer{
		Pipe: msg.NewPipe(10),
		Protocols: ProtocolMap{
			pid1: p1,
			pid2: p2,
		},
	}
	peer1 := newPeer(t, "11140beec7b5ea3f0fdbc95d0dd47f3c5bc275aaaaaa")
	// peer2 := newPeer(t, "11140beec7b5ea3f0fdbc95d0dd47f3c5bc275bbbbbb")

	// run muxer
	ctx, cancel := context.WithCancel(context.Background())
	mux1.Start(ctx)

	// counts
	total := 10000
	speed := time.Microsecond * 1
	counts := [2][2][2]int{}

	// run producers at every end sending incrementing messages
	produceOut := func(pid ProtocolID, size int) {
		limiter := time.Tick(speed)
		for i := 0; i < size; i++ {
			<-limiter
			s := fmt.Sprintf("proto %v out %v", pid, i)
142
			m := msg.New(peer1, []byte(s))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
			mux1.Protocols[pid].GetPipe().Outgoing <- m
			counts[pid][0][0]++
			u.DOut("sent %v\n", s)
		}
	}

	produceIn := func(pid ProtocolID, size int) {
		limiter := time.Tick(speed)
		for i := 0; i < size; i++ {
			<-limiter
			s := fmt.Sprintf("proto %v in %v", pid, i)
			d, err := wrapData([]byte(s), pid)
			if err != nil {
				t.Error(err)
			}

159
			m := msg.New(peer1, d)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
160 161 162 163 164 165 166 167 168 169
			mux1.Incoming <- m
			counts[pid][1][0]++
			u.DOut("sent %v\n", s)
		}
	}

	consumeOut := func() {
		for {
			select {
			case m := <-mux1.Outgoing:
170
				data, pid, err := unwrapData(m.Data())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
				if err != nil {
					t.Error(err)
				}

				u.DOut("got %v\n", string(data))
				counts[pid][1][1]++

			case <-ctx.Done():
				return
			}
		}
	}

	consumeIn := func(pid ProtocolID) {
		for {
			select {
			case m := <-mux1.Protocols[pid].GetPipe().Incoming:
				counts[pid][0][1]++
189
				u.DOut("got %v\n", string(m.Data()))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
			case <-ctx.Done():
				return
			}
		}
	}

	go produceOut(pid1, total)
	go produceOut(pid2, total)
	go produceIn(pid1, total)
	go produceIn(pid2, total)
	go consumeOut()
	go consumeIn(pid1)
	go consumeIn(pid2)

	limiter := time.Tick(speed)
	for {
		<-limiter
		got := counts[0][0][0] + counts[0][0][1] +
			counts[0][1][0] + counts[0][1][1] +
			counts[1][0][0] + counts[1][0][1] +
			counts[1][1][0] + counts[1][1][1]

		if got == total*8 {
			cancel()
			return
		}
	}

}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241

func TestStopping(t *testing.T) {

	// setup
	p1 := &TestProtocol{Pipe: msg.NewPipe(10)}
	p2 := &TestProtocol{Pipe: msg.NewPipe(10)}
	pid1 := ProtocolID_Test
	pid2 := ProtocolID_Identify
	mux1 := &Muxer{
		Pipe: msg.NewPipe(10),
		Protocols: ProtocolMap{
			pid1: p1,
			pid2: p2,
		},
	}
	peer1 := newPeer(t, "11140beec7b5ea3f0fdbc95d0dd47f3c5bc275aaaaaa")
	// peer2 := newPeer(t, "11140beec7b5ea3f0fdbc95d0dd47f3c5bc275bbbbbb")

	// run muxer
	mux1.Start(context.Background())

	// test outgoing p1
	for _, s := range []string{"foo", "bar", "baz"} {
242
		p1.Outgoing <- msg.New(peer1, []byte(s))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
243 244 245 246 247 248 249 250 251
		testWrappedMsg(t, <-mux1.Outgoing, pid1, []byte(s))
	}

	// test incoming p1
	for _, s := range []string{"foo", "bar", "baz"} {
		d, err := wrapData([]byte(s), pid1)
		if err != nil {
			t.Error(err)
		}
252
		mux1.Incoming <- msg.New(peer1, d)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
253 254 255 256 257 258 259 260 261 262
		testMsg(t, <-p1.Incoming, []byte(s))
	}

	mux1.Stop()
	if mux1.cancel != nil {
		t.Error("mux.cancel should be nil")
	}

	// test outgoing p1
	for _, s := range []string{"foo", "bar", "baz"} {
263
		p1.Outgoing <- msg.New(peer1, []byte(s))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
264 265 266 267 268 269 270 271 272 273 274 275 276
		select {
		case <-mux1.Outgoing:
			t.Error("should not have received anything.")
		case <-time.After(time.Millisecond):
		}
	}

	// test incoming p1
	for _, s := range []string{"foo", "bar", "baz"} {
		d, err := wrapData([]byte(s), pid1)
		if err != nil {
			t.Error(err)
		}
277
		mux1.Incoming <- msg.New(peer1, d)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
278 279 280 281 282 283 284 285
		select {
		case <-p1.Incoming:
			t.Error("should not have received anything.")
		case <-time.After(time.Millisecond):
		}
	}

}