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 (
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
// 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)
bs := &bitswap{
......@@ -54,7 +45,7 @@ type bitswap struct {
blockstore blockstore.Blockstore
// routing interface for communication
routing Routing
routing bsnet.Routing
notifications notifications.PubSub
......
......@@ -11,14 +11,15 @@ import (
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"
testnet "github.com/jbenet/go-ipfs/exchange/bitswap/testnet"
peer "github.com/jbenet/go-ipfs/peer"
testutil "github.com/jbenet/go-ipfs/util/testutil"
)
func TestGetBlockTimeout(t *testing.T) {
net := LocalNetwork()
rs := newRoutingServer()
net := testnet.VirtualNetwork()
rs := testnet.VirtualRoutingServer()
ipfs := session(net, rs, []byte("peer id"))
ctx, _ := context.WithTimeout(context.Background(), time.Nanosecond)
block := testutil.NewBlockOrFail(t, "block")
......@@ -31,8 +32,8 @@ func TestGetBlockTimeout(t *testing.T) {
func TestProviderForKeyButNetworkCannotFind(t *testing.T) {
net := LocalNetwork()
rs := newRoutingServer()
net := testnet.VirtualNetwork()
rs := testnet.VirtualRoutingServer()
block := testutil.NewBlockOrFail(t, "block")
rs.Announce(&peer.Peer{}, block.Key()) // but not on network
......@@ -52,8 +53,8 @@ func TestProviderForKeyButNetworkCannotFind(t *testing.T) {
func TestGetBlockFromPeerAfterPeerAnnounces(t *testing.T) {
t.Skip("Failing. Work in progress")
net := LocalNetwork()
rs := newRoutingServer()
net := testnet.VirtualNetwork()
rs := testnet.VirtualRoutingServer()
block := testutil.NewBlockOrFail(t, "block")
hasBlock := session(net, rs, []byte("hasBlock"))
......@@ -78,7 +79,7 @@ type ipfs struct {
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{}
adapter := net.Adapter(p)
......
......@@ -7,6 +7,7 @@ import (
bsmsg "github.com/jbenet/go-ipfs/exchange/bitswap/message"
netmsg "github.com/jbenet/go-ipfs/net/message"
peer "github.com/jbenet/go-ipfs/peer"
u "github.com/jbenet/go-ipfs/util"
)
// Adapter provides network connectivity for BitSwap sessions
......@@ -41,3 +42,12 @@ type NetMessageService interface {
SendMessage(ctx context.Context, m netmsg.NetMessage) error
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 {
// network impl
func LocalNetwork() Network {
func VirtualNetwork() Network {
return &network{
clients: make(map[util.Key]bsnet.Receiver),
}
......
......@@ -12,7 +12,7 @@ import (
)
func TestSendRequestToCooperativePeer(t *testing.T) {
net := LocalNetwork()
net := VirtualNetwork()
idOfRecipient := []byte("recipient")
......@@ -59,7 +59,7 @@ func TestSendRequestToCooperativePeer(t *testing.T) {
}
func TestSendMessageAsyncButWaitForResponse(t *testing.T) {
net := LocalNetwork()
net := VirtualNetwork()
idOfResponder := []byte("responder")
waiter := net.Adapter(&peer.Peer{ID: []byte("waiter")})
responder := net.Adapter(&peer.Peer{ID: idOfResponder})
......
......@@ -5,6 +5,7 @@ import (
"sync"
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"
u "github.com/jbenet/go-ipfs/util"
)
......@@ -18,10 +19,10 @@ type RoutingServer interface {
// TODO
// 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{
m: make(map[u.Key]map[*peer.Peer]bool),
}
......@@ -61,7 +62,7 @@ func (rs *hashTable) Providers(k u.Key) []*peer.Peer {
}
// TODO
func (rs *hashTable) Client(p *peer.Peer) Routing {
func (rs *hashTable) Client(p *peer.Peer) bsnet.Routing {
return &routingClient{
peer: p,
hashTable: rs,
......
......@@ -29,7 +29,7 @@ func TestSetAndGet(t *testing.T) {
ID: pid,
}
k := u.Key("42")
rs := newRoutingServer()
rs := VirtualRoutingServer()
err := rs.Announce(p, k)
if err != nil {
t.Fatal(err)
......@@ -50,7 +50,7 @@ func TestClientFindProviders(t *testing.T) {
peer := &peer.Peer{
ID: []byte("42"),
}
rs := newRoutingServer()
rs := VirtualRoutingServer()
client := rs.Client(peer)
k := u.Key("hello")
err := client.Provide(k)
......@@ -83,7 +83,7 @@ func TestClientFindProviders(t *testing.T) {
}
func TestClientOverMax(t *testing.T) {
rs := newRoutingServer()
rs := VirtualRoutingServer()
k := u.Key("hello")
numProvidersForHelloKey := 100
for i := 0; i < numProvidersForHelloKey; i++ {
......@@ -115,7 +115,7 @@ func TestClientOverMax(t *testing.T) {
// TODO does dht ensure won't receive self as a provider? probably not.
func TestCanceledContext(t *testing.T) {
rs := newRoutingServer()
rs := VirtualRoutingServer()
k := u.Key("hello")
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