Commit 01625e32 authored by Brian Tiger Chow's avatar Brian Tiger Chow

refac(bitswap:testnet) give testnet its own package

parent c67d48d9
...@@ -17,18 +17,9 @@ import ( ...@@ -17,18 +17,9 @@ import (
u "github.com/jbenet/go-ipfs/util" u "github.com/jbenet/go-ipfs/util"
) )
// TODO rename -> Router?
type Routing interface {
// FindProvidersAsync returns a channel of providers for the given key
FindProvidersAsync(context.Context, u.Key, int) <-chan *peer.Peer
// Provide provides the key to the network
Provide(key u.Key) error
}
// NetMessageSession initializes a BitSwap session that communicates over the // NetMessageSession initializes a BitSwap session that communicates over the
// provided NetMessage service // provided NetMessage service
func NetMessageSession(parent context.Context, s bsnet.NetMessageService, p *peer.Peer, d ds.Datastore, directory Routing) exchange.Interface { func NetMessageSession(parent context.Context, s bsnet.NetMessageService, p *peer.Peer, d ds.Datastore, directory bsnet.Routing) exchange.Interface {
networkAdapter := bsnet.NetMessageAdapter(s, nil) networkAdapter := bsnet.NetMessageAdapter(s, nil)
bs := &bitswap{ bs := &bitswap{
...@@ -54,7 +45,7 @@ type bitswap struct { ...@@ -54,7 +45,7 @@ type bitswap struct {
blockstore blockstore.Blockstore blockstore blockstore.Blockstore
// routing interface for communication // routing interface for communication
routing Routing routing bsnet.Routing
notifications notifications.PubSub notifications notifications.PubSub
......
...@@ -11,14 +11,15 @@ import ( ...@@ -11,14 +11,15 @@ import (
exchange "github.com/jbenet/go-ipfs/exchange" exchange "github.com/jbenet/go-ipfs/exchange"
notifications "github.com/jbenet/go-ipfs/exchange/bitswap/notifications" notifications "github.com/jbenet/go-ipfs/exchange/bitswap/notifications"
strategy "github.com/jbenet/go-ipfs/exchange/bitswap/strategy" strategy "github.com/jbenet/go-ipfs/exchange/bitswap/strategy"
testnet "github.com/jbenet/go-ipfs/exchange/bitswap/testnet"
peer "github.com/jbenet/go-ipfs/peer" peer "github.com/jbenet/go-ipfs/peer"
testutil "github.com/jbenet/go-ipfs/util/testutil" testutil "github.com/jbenet/go-ipfs/util/testutil"
) )
func TestGetBlockTimeout(t *testing.T) { func TestGetBlockTimeout(t *testing.T) {
net := LocalNetwork() net := testnet.VirtualNetwork()
rs := newRoutingServer() rs := testnet.VirtualRoutingServer()
ipfs := session(net, rs, []byte("peer id")) ipfs := session(net, rs, []byte("peer id"))
ctx, _ := context.WithTimeout(context.Background(), time.Nanosecond) ctx, _ := context.WithTimeout(context.Background(), time.Nanosecond)
block := testutil.NewBlockOrFail(t, "block") block := testutil.NewBlockOrFail(t, "block")
...@@ -31,8 +32,8 @@ func TestGetBlockTimeout(t *testing.T) { ...@@ -31,8 +32,8 @@ func TestGetBlockTimeout(t *testing.T) {
func TestProviderForKeyButNetworkCannotFind(t *testing.T) { func TestProviderForKeyButNetworkCannotFind(t *testing.T) {
net := LocalNetwork() net := testnet.VirtualNetwork()
rs := newRoutingServer() rs := testnet.VirtualRoutingServer()
block := testutil.NewBlockOrFail(t, "block") block := testutil.NewBlockOrFail(t, "block")
rs.Announce(&peer.Peer{}, block.Key()) // but not on network rs.Announce(&peer.Peer{}, block.Key()) // but not on network
...@@ -52,8 +53,8 @@ func TestProviderForKeyButNetworkCannotFind(t *testing.T) { ...@@ -52,8 +53,8 @@ func TestProviderForKeyButNetworkCannotFind(t *testing.T) {
func TestGetBlockFromPeerAfterPeerAnnounces(t *testing.T) { func TestGetBlockFromPeerAfterPeerAnnounces(t *testing.T) {
t.Skip("Failing. Work in progress") t.Skip("Failing. Work in progress")
net := LocalNetwork() net := testnet.VirtualNetwork()
rs := newRoutingServer() rs := testnet.VirtualRoutingServer()
block := testutil.NewBlockOrFail(t, "block") block := testutil.NewBlockOrFail(t, "block")
hasBlock := session(net, rs, []byte("hasBlock")) hasBlock := session(net, rs, []byte("hasBlock"))
...@@ -78,7 +79,7 @@ type ipfs struct { ...@@ -78,7 +79,7 @@ type ipfs struct {
blockstore bstore.Blockstore blockstore bstore.Blockstore
} }
func session(net Network, rs RoutingServer, id peer.ID) ipfs { func session(net testnet.Network, rs testnet.RoutingServer, id peer.ID) ipfs {
p := &peer.Peer{} p := &peer.Peer{}
adapter := net.Adapter(p) adapter := net.Adapter(p)
......
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
bsmsg "github.com/jbenet/go-ipfs/exchange/bitswap/message" bsmsg "github.com/jbenet/go-ipfs/exchange/bitswap/message"
netmsg "github.com/jbenet/go-ipfs/net/message" netmsg "github.com/jbenet/go-ipfs/net/message"
peer "github.com/jbenet/go-ipfs/peer" peer "github.com/jbenet/go-ipfs/peer"
u "github.com/jbenet/go-ipfs/util"
) )
// Adapter provides network connectivity for BitSwap sessions // Adapter provides network connectivity for BitSwap sessions
...@@ -41,3 +42,12 @@ type NetMessageService interface { ...@@ -41,3 +42,12 @@ type NetMessageService interface {
SendMessage(ctx context.Context, m netmsg.NetMessage) error SendMessage(ctx context.Context, m netmsg.NetMessage) error
SetHandler(netservice.Handler) SetHandler(netservice.Handler)
} }
// TODO rename -> Router?
type Routing interface {
// FindProvidersAsync returns a channel of providers for the given key
FindProvidersAsync(context.Context, u.Key, int) <-chan *peer.Peer
// Provide provides the key to the network
Provide(key u.Key) error
}
...@@ -30,7 +30,7 @@ type Network interface { ...@@ -30,7 +30,7 @@ type Network interface {
// network impl // network impl
func LocalNetwork() Network { func VirtualNetwork() Network {
return &network{ return &network{
clients: make(map[util.Key]bsnet.Receiver), clients: make(map[util.Key]bsnet.Receiver),
} }
......
...@@ -12,7 +12,7 @@ import ( ...@@ -12,7 +12,7 @@ import (
) )
func TestSendRequestToCooperativePeer(t *testing.T) { func TestSendRequestToCooperativePeer(t *testing.T) {
net := LocalNetwork() net := VirtualNetwork()
idOfRecipient := []byte("recipient") idOfRecipient := []byte("recipient")
...@@ -59,7 +59,7 @@ func TestSendRequestToCooperativePeer(t *testing.T) { ...@@ -59,7 +59,7 @@ func TestSendRequestToCooperativePeer(t *testing.T) {
} }
func TestSendMessageAsyncButWaitForResponse(t *testing.T) { func TestSendMessageAsyncButWaitForResponse(t *testing.T) {
net := LocalNetwork() net := VirtualNetwork()
idOfResponder := []byte("responder") idOfResponder := []byte("responder")
waiter := net.Adapter(&peer.Peer{ID: []byte("waiter")}) waiter := net.Adapter(&peer.Peer{ID: []byte("waiter")})
responder := net.Adapter(&peer.Peer{ID: idOfResponder}) responder := net.Adapter(&peer.Peer{ID: idOfResponder})
......
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"sync" "sync"
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
bsnet "github.com/jbenet/go-ipfs/exchange/bitswap/network"
peer "github.com/jbenet/go-ipfs/peer" peer "github.com/jbenet/go-ipfs/peer"
u "github.com/jbenet/go-ipfs/util" u "github.com/jbenet/go-ipfs/util"
) )
...@@ -18,10 +19,10 @@ type RoutingServer interface { ...@@ -18,10 +19,10 @@ type RoutingServer interface {
// TODO // TODO
// Returns a Routing instance configured to query this hash table // Returns a Routing instance configured to query this hash table
Client(*peer.Peer) Routing Client(*peer.Peer) bsnet.Routing
} }
func newRoutingServer() RoutingServer { func VirtualRoutingServer() RoutingServer {
return &hashTable{ return &hashTable{
m: make(map[u.Key]map[*peer.Peer]bool), m: make(map[u.Key]map[*peer.Peer]bool),
} }
...@@ -61,7 +62,7 @@ func (rs *hashTable) Providers(k u.Key) []*peer.Peer { ...@@ -61,7 +62,7 @@ func (rs *hashTable) Providers(k u.Key) []*peer.Peer {
} }
// TODO // TODO
func (rs *hashTable) Client(p *peer.Peer) Routing { func (rs *hashTable) Client(p *peer.Peer) bsnet.Routing {
return &routingClient{ return &routingClient{
peer: p, peer: p,
hashTable: rs, hashTable: rs,
......
...@@ -29,7 +29,7 @@ func TestSetAndGet(t *testing.T) { ...@@ -29,7 +29,7 @@ func TestSetAndGet(t *testing.T) {
ID: pid, ID: pid,
} }
k := u.Key("42") k := u.Key("42")
rs := newRoutingServer() rs := VirtualRoutingServer()
err := rs.Announce(p, k) err := rs.Announce(p, k)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
...@@ -50,7 +50,7 @@ func TestClientFindProviders(t *testing.T) { ...@@ -50,7 +50,7 @@ func TestClientFindProviders(t *testing.T) {
peer := &peer.Peer{ peer := &peer.Peer{
ID: []byte("42"), ID: []byte("42"),
} }
rs := newRoutingServer() rs := VirtualRoutingServer()
client := rs.Client(peer) client := rs.Client(peer)
k := u.Key("hello") k := u.Key("hello")
err := client.Provide(k) err := client.Provide(k)
...@@ -83,7 +83,7 @@ func TestClientFindProviders(t *testing.T) { ...@@ -83,7 +83,7 @@ func TestClientFindProviders(t *testing.T) {
} }
func TestClientOverMax(t *testing.T) { func TestClientOverMax(t *testing.T) {
rs := newRoutingServer() rs := VirtualRoutingServer()
k := u.Key("hello") k := u.Key("hello")
numProvidersForHelloKey := 100 numProvidersForHelloKey := 100
for i := 0; i < numProvidersForHelloKey; i++ { for i := 0; i < numProvidersForHelloKey; i++ {
...@@ -115,7 +115,7 @@ func TestClientOverMax(t *testing.T) { ...@@ -115,7 +115,7 @@ func TestClientOverMax(t *testing.T) {
// TODO does dht ensure won't receive self as a provider? probably not. // TODO does dht ensure won't receive self as a provider? probably not.
func TestCanceledContext(t *testing.T) { func TestCanceledContext(t *testing.T) {
rs := newRoutingServer() rs := VirtualRoutingServer()
k := u.Key("hello") k := u.Key("hello")
t.Log("async'ly announce infinite stream of providers for key") t.Log("async'ly announce infinite stream of providers for key")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment