bitswap_test.go 2.95 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
package bitswap

import (
	"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"
14
	testnet "github.com/jbenet/go-ipfs/exchange/bitswap/testnet"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
15 16 17 18 19 20
	peer "github.com/jbenet/go-ipfs/peer"
	testutil "github.com/jbenet/go-ipfs/util/testutil"
)

func TestGetBlockTimeout(t *testing.T) {

21 22
	net := testnet.VirtualNetwork()
	rs := testnet.VirtualRoutingServer()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
23 24 25 26 27 28 29 30 31 32 33 34
	ipfs := session(net, rs, []byte("peer id"))
	ctx, _ := context.WithTimeout(context.Background(), time.Nanosecond)
	block := testutil.NewBlockOrFail(t, "block")

	_, err := ipfs.exchange.Block(ctx, block.Key())
	if err != context.DeadlineExceeded {
		t.Fatal("Expected DeadlineExceeded error")
	}
}

func TestProviderForKeyButNetworkCannotFind(t *testing.T) {

35 36
	net := testnet.VirtualNetwork()
	rs := testnet.VirtualRoutingServer()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
37

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

Brian Tiger Chow's avatar
Brian Tiger Chow committed
41 42 43 44 45
	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
46 47 48 49 50
	if err != context.DeadlineExceeded {
		t.Fatal("Expected DeadlineExceeded error")
	}
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
51 52 53 54 55
// TestGetBlockAfterRequesting...

func TestGetBlockFromPeerAfterPeerAnnounces(t *testing.T) {
	t.Skip("Failing. Work in progress")

56 57
	net := testnet.VirtualNetwork()
	rs := testnet.VirtualRoutingServer()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
	block := testutil.NewBlockOrFail(t, "block")

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

	rs.Announce(hasBlock.peer, block.Key())
	hasBlock.blockstore.Put(block)
	hasBlock.exchange.HasBlock(context.Background(), block)

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

	ctx, _ := context.WithTimeout(context.Background(), time.Second)
	_, err := wantsBlock.exchange.Block(ctx, block.Key())
	if err != nil {
		t.Log(err)
		t.Fatal("Expected to succeed")
	}
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
76 77 78 79 80 81
type ipfs struct {
	peer       *peer.Peer
	exchange   exchange.Interface
	blockstore bstore.Blockstore
}

82
func session(net testnet.Network, rs testnet.RoutingServer, id peer.ID) ipfs {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
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
	p := &peer.Peer{}

	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)
	return ipfs{
		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|")
}