network_test.go 3.56 KB
Newer Older
Brian Tiger Chow's avatar
Brian Tiger Chow committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
package bitswap

import (
	"sync"
	"testing"

	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
	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"
	testutil "github.com/jbenet/go-ipfs/util/testutil"
)

func TestSendRequestToCooperativePeer(t *testing.T) {
15
	net := VirtualNetwork()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

	idOfRecipient := []byte("recipient")

	t.Log("Get two network adapters")

	initiator := net.Adapter(&peer.Peer{ID: []byte("initiator")})
	recipient := net.Adapter(&peer.Peer{ID: idOfRecipient})

	expectedStr := "response from recipient"
	recipient.SetDelegate(lambda(func(
		ctx context.Context,
		from *peer.Peer,
		incoming bsmsg.BitSwapMessage) (
		*peer.Peer, bsmsg.BitSwapMessage, error) {

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

		// TODO test contents of incoming message

		m := bsmsg.New()
		m.AppendBlock(testutil.NewBlockOrFail(t, expectedStr))

		return from, m, nil
	}))

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

	message := bsmsg.New()
	message.AppendBlock(testutil.NewBlockOrFail(t, "data"))
	response, err := initiator.SendRequest(
		context.Background(), &peer.Peer{ID: idOfRecipient}, message)
	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) {
62
	net := VirtualNetwork()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 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
	idOfResponder := []byte("responder")
	waiter := net.Adapter(&peer.Peer{ID: []byte("waiter")})
	responder := net.Adapter(&peer.Peer{ID: idOfResponder})

	var wg sync.WaitGroup

	wg.Add(1)

	expectedStr := "received async"

	responder.SetDelegate(lambda(func(
		ctx context.Context,
		fromWaiter *peer.Peer,
		msgFromWaiter bsmsg.BitSwapMessage) (
		*peer.Peer, bsmsg.BitSwapMessage, error) {

		msgToWaiter := bsmsg.New()
		msgToWaiter.AppendBlock(testutil.NewBlockOrFail(t, expectedStr))

		return fromWaiter, msgToWaiter, nil
	}))

	waiter.SetDelegate(lambda(func(
		ctx context.Context,
		fromResponder *peer.Peer,
		msgFromResponder bsmsg.BitSwapMessage) (
		*peer.Peer, bsmsg.BitSwapMessage, error) {

		// 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")

		}
		return nil, nil, nil
	}))

	messageSentAsync := bsmsg.New()
	messageSentAsync.AppendBlock(testutil.NewBlockOrFail(t, "data"))
	errSending := waiter.SendMessage(
		context.Background(), &peer.Peer{ID: idOfResponder}, messageSentAsync)
	if errSending != nil {
		t.Fatal(errSending)
	}

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

type receiverFunc func(ctx context.Context, p *peer.Peer,
	incoming bsmsg.BitSwapMessage) (*peer.Peer, bsmsg.BitSwapMessage, error)

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

type lambdaImpl struct {
	f func(ctx context.Context, p *peer.Peer,
		incoming bsmsg.BitSwapMessage) (
		*peer.Peer, bsmsg.BitSwapMessage, error)
}

func (lam *lambdaImpl) ReceiveMessage(ctx context.Context,
	p *peer.Peer, incoming bsmsg.BitSwapMessage) (
	*peer.Peer, bsmsg.BitSwapMessage, error) {
	return lam.f(ctx, p, incoming)
}