bitswap_test.go 3.09 KB
Newer Older
Brian Tiger Chow's avatar
Brian Tiger Chow committed
1 2 3
package bitswap

import (
4
	"bytes"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
5 6 7 8 9 10 11 12 13 14
	"testing"
	"time"

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

	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
	bstore "github.com/jbenet/go-ipfs/blockstore"
	exchange "github.com/jbenet/go-ipfs/exchange"
	notifications "github.com/jbenet/go-ipfs/exchange/bitswap/notifications"
	strategy "github.com/jbenet/go-ipfs/exchange/bitswap/strategy"
15
	testnet "github.com/jbenet/go-ipfs/exchange/bitswap/testnet"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
16 17 18 19 20 21
	peer "github.com/jbenet/go-ipfs/peer"
	testutil "github.com/jbenet/go-ipfs/util/testutil"
)

func TestGetBlockTimeout(t *testing.T) {

22 23
	net := testnet.VirtualNetwork()
	rs := testnet.VirtualRoutingServer()
24 25 26

	self := session(net, rs, []byte("peer id"))

Brian Tiger Chow's avatar
Brian Tiger Chow committed
27 28
	ctx, _ := context.WithTimeout(context.Background(), time.Nanosecond)
	block := testutil.NewBlockOrFail(t, "block")
29
	_, err := self.exchange.Block(ctx, block.Key())
Brian Tiger Chow's avatar
Brian Tiger Chow committed
30 31 32 33 34 35 36 37

	if err != context.DeadlineExceeded {
		t.Fatal("Expected DeadlineExceeded error")
	}
}

func TestProviderForKeyButNetworkCannotFind(t *testing.T) {

38 39
	net := testnet.VirtualNetwork()
	rs := testnet.VirtualRoutingServer()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
40

Brian Tiger Chow's avatar
Brian Tiger Chow committed
41
	block := testutil.NewBlockOrFail(t, "block")
Brian Tiger Chow's avatar
Brian Tiger Chow committed
42 43
	rs.Announce(&peer.Peer{}, block.Key()) // but not on network

Brian Tiger Chow's avatar
Brian Tiger Chow committed
44 45 46 47 48
	solo := session(net, rs, []byte("peer id"))

	ctx, _ := context.WithTimeout(context.Background(), time.Nanosecond)
	_, err := solo.exchange.Block(ctx, block.Key())

Brian Tiger Chow's avatar
Brian Tiger Chow committed
49 50 51 52 53
	if err != context.DeadlineExceeded {
		t.Fatal("Expected DeadlineExceeded error")
	}
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
54 55 56 57
// TestGetBlockAfterRequesting...

func TestGetBlockFromPeerAfterPeerAnnounces(t *testing.T) {

58 59
	net := testnet.VirtualNetwork()
	rs := testnet.VirtualRoutingServer()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
60 61 62 63
	block := testutil.NewBlockOrFail(t, "block")

	hasBlock := session(net, rs, []byte("hasBlock"))

64 65 66 67 68 69
	if err := hasBlock.blockstore.Put(block); err != nil {
		t.Fatal(err)
	}
	if err := hasBlock.exchange.HasBlock(context.Background(), block); err != nil {
		t.Fatal(err)
	}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
70 71 72 73

	wantsBlock := session(net, rs, []byte("wantsBlock"))

	ctx, _ := context.WithTimeout(context.Background(), time.Second)
74
	received, err := wantsBlock.exchange.Block(ctx, block.Key())
Brian Tiger Chow's avatar
Brian Tiger Chow committed
75 76 77 78
	if err != nil {
		t.Log(err)
		t.Fatal("Expected to succeed")
	}
79 80 81 82

	if !bytes.Equal(block.Data, received.Data) {
		t.Fatal("Data doesn't match")
	}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
83 84
}

85
type testnetBitSwap struct {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
86 87 88 89 90
	peer       *peer.Peer
	exchange   exchange.Interface
	blockstore bstore.Blockstore
}

91 92
func session(net testnet.Network, rs testnet.RoutingServer, id peer.ID) testnetBitSwap {
	p := &peer.Peer{ID: id}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
93 94 95 96 97 98 99 100 101 102 103 104 105

	adapter := net.Adapter(p)
	htc := rs.Client(p)

	blockstore := bstore.NewBlockstore(ds.NewMapDatastore())
	bs := &bitswap{
		blockstore:    blockstore,
		notifications: notifications.New(),
		strategy:      strategy.New(),
		routing:       htc,
		sender:        adapter,
	}
	adapter.SetDelegate(bs)
106
	return testnetBitSwap{
Brian Tiger Chow's avatar
Brian Tiger Chow committed
107 108 109 110 111 112 113 114 115 116 117
		peer:       p,
		exchange:   bs,
		blockstore: blockstore,
	}
}

func TestSendToWantingPeer(t *testing.T) {
	t.Log("Peer |w| tells me it wants file, but I don't have it")
	t.Log("Then another peer |o| sends it to me")
	t.Log("After receiving the file from |o|, I send it to the wanting peer |w|")
}