network_test.go 3.62 KB
Newer Older
Brian Tiger Chow's avatar
Brian Tiger Chow committed
1 2 3 4 5 6 7
package bitswap

import (
	"sync"
	"testing"

	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
8
	blocks "github.com/jbenet/go-ipfs/blocks"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
9 10 11
	bsmsg "github.com/jbenet/go-ipfs/exchange/bitswap/message"
	bsnet "github.com/jbenet/go-ipfs/exchange/bitswap/network"
	peer "github.com/jbenet/go-ipfs/peer"
12
	testutil "github.com/jbenet/go-ipfs/util/testutil"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
13 14 15
)

func TestSendRequestToCooperativePeer(t *testing.T) {
16
	net := VirtualNetwork()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
17 18 19 20 21

	idOfRecipient := []byte("recipient")

	t.Log("Get two network adapters")

22 23
	initiator := net.Adapter(testutil.NewPeerWithIDString("initiator"))
	recipient := net.Adapter(testutil.NewPeerWithID(idOfRecipient))
Brian Tiger Chow's avatar
Brian Tiger Chow committed
24 25 26 27

	expectedStr := "response from recipient"
	recipient.SetDelegate(lambda(func(
		ctx context.Context,
28
		from peer.Peer,
Brian Tiger Chow's avatar
Brian Tiger Chow committed
29
		incoming bsmsg.BitSwapMessage) (
30
		peer.Peer, bsmsg.BitSwapMessage) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
31 32 33 34 35 36

		t.Log("Recipient received a message from the network")

		// TODO test contents of incoming message

		m := bsmsg.New()
Jeromy's avatar
Jeromy committed
37
		m.AddBlock(blocks.NewBlock([]byte(expectedStr)))
Brian Tiger Chow's avatar
Brian Tiger Chow committed
38

39
		return from, m
Brian Tiger Chow's avatar
Brian Tiger Chow committed
40 41 42 43 44
	}))

	t.Log("Build a message and send a synchronous request to recipient")

	message := bsmsg.New()
Jeromy's avatar
Jeromy committed
45
	message.AddBlock(blocks.NewBlock([]byte("data")))
Brian Tiger Chow's avatar
Brian Tiger Chow committed
46
	response, err := initiator.SendRequest(
47
		context.Background(), testutil.NewPeerWithID(idOfRecipient), message)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
	if err != nil {
		t.Fatal(err)
	}

	t.Log("Check the contents of the response from recipient")

	for _, blockFromRecipient := range response.Blocks() {
		if string(blockFromRecipient.Data) == expectedStr {
			return
		}
	}
	t.Fatal("Should have returned after finding expected block data")
}

func TestSendMessageAsyncButWaitForResponse(t *testing.T) {
63
	net := VirtualNetwork()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
64
	idOfResponder := []byte("responder")
65 66
	waiter := net.Adapter(testutil.NewPeerWithIDString("waiter"))
	responder := net.Adapter(testutil.NewPeerWithID(idOfResponder))
Brian Tiger Chow's avatar
Brian Tiger Chow committed
67 68 69 70 71 72 73 74 75

	var wg sync.WaitGroup

	wg.Add(1)

	expectedStr := "received async"

	responder.SetDelegate(lambda(func(
		ctx context.Context,
76
		fromWaiter peer.Peer,
Brian Tiger Chow's avatar
Brian Tiger Chow committed
77
		msgFromWaiter bsmsg.BitSwapMessage) (
78
		peer.Peer, bsmsg.BitSwapMessage) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
79 80

		msgToWaiter := bsmsg.New()
Jeromy's avatar
Jeromy committed
81
		msgToWaiter.AddBlock(blocks.NewBlock([]byte(expectedStr)))
Brian Tiger Chow's avatar
Brian Tiger Chow committed
82

83
		return fromWaiter, msgToWaiter
Brian Tiger Chow's avatar
Brian Tiger Chow committed
84 85 86 87
	}))

	waiter.SetDelegate(lambda(func(
		ctx context.Context,
88
		fromResponder peer.Peer,
Brian Tiger Chow's avatar
Brian Tiger Chow committed
89
		msgFromResponder bsmsg.BitSwapMessage) (
90
		peer.Peer, bsmsg.BitSwapMessage) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104

		// TODO assert that this came from the correct peer and that the message contents are as expected
		ok := false
		for _, b := range msgFromResponder.Blocks() {
			if string(b.Data) == expectedStr {
				wg.Done()
				ok = true
			}
		}

		if !ok {
			t.Fatal("Message not received from the responder")

		}
105
		return nil, nil
Brian Tiger Chow's avatar
Brian Tiger Chow committed
106 107 108
	}))

	messageSentAsync := bsmsg.New()
Jeromy's avatar
Jeromy committed
109
	messageSentAsync.AddBlock(blocks.NewBlock([]byte("data")))
Brian Tiger Chow's avatar
Brian Tiger Chow committed
110
	errSending := waiter.SendMessage(
111
		context.Background(), testutil.NewPeerWithID(idOfResponder), messageSentAsync)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
112 113 114 115 116 117 118
	if errSending != nil {
		t.Fatal(errSending)
	}

	wg.Wait() // until waiter delegate function is executed
}

119 120
type receiverFunc func(ctx context.Context, p peer.Peer,
	incoming bsmsg.BitSwapMessage) (peer.Peer, bsmsg.BitSwapMessage)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
121 122 123 124 125 126 127 128 129

// lambda returns a Receiver instance given a receiver function
func lambda(f receiverFunc) bsnet.Receiver {
	return &lambdaImpl{
		f: f,
	}
}

type lambdaImpl struct {
130 131
	f func(ctx context.Context, p peer.Peer, incoming bsmsg.BitSwapMessage) (
		peer.Peer, bsmsg.BitSwapMessage)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
132 133 134
}

func (lam *lambdaImpl) ReceiveMessage(ctx context.Context,
135 136
	p peer.Peer, incoming bsmsg.BitSwapMessage) (
	peer.Peer, bsmsg.BitSwapMessage) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
137 138
	return lam.f(ctx, p, incoming)
}
139 140 141 142

func (lam *lambdaImpl) ReceiveError(err error) {
	// TODO log error
}