Commit ac7404a6 authored by Brian Tiger Chow's avatar Brian Tiger Chow

Merge branch 'feat/mock-dht'

parents f0019754 c044d9c3
......@@ -16,6 +16,7 @@ import (
strategy "github.com/jbenet/go-ipfs/exchange/bitswap/strategy"
tn "github.com/jbenet/go-ipfs/exchange/bitswap/testnet"
peer "github.com/jbenet/go-ipfs/peer"
mock "github.com/jbenet/go-ipfs/routing/mock"
util "github.com/jbenet/go-ipfs/util"
testutil "github.com/jbenet/go-ipfs/util/testutil"
)
......@@ -23,7 +24,7 @@ import (
func TestGetBlockTimeout(t *testing.T) {
net := tn.VirtualNetwork()
rs := tn.VirtualRoutingServer()
rs := mock.VirtualRoutingServer()
g := NewSessionGenerator(net, rs)
self := g.Next()
......@@ -40,7 +41,7 @@ func TestGetBlockTimeout(t *testing.T) {
func TestProviderForKeyButNetworkCannotFind(t *testing.T) {
net := tn.VirtualNetwork()
rs := tn.VirtualRoutingServer()
rs := mock.VirtualRoutingServer()
g := NewSessionGenerator(net, rs)
block := testutil.NewBlockOrFail(t, "block")
......@@ -61,7 +62,7 @@ func TestProviderForKeyButNetworkCannotFind(t *testing.T) {
func TestGetBlockFromPeerAfterPeerAnnounces(t *testing.T) {
net := tn.VirtualNetwork()
rs := tn.VirtualRoutingServer()
rs := mock.VirtualRoutingServer()
block := testutil.NewBlockOrFail(t, "block")
g := NewSessionGenerator(net, rs)
......@@ -90,7 +91,7 @@ func TestGetBlockFromPeerAfterPeerAnnounces(t *testing.T) {
func TestSwarm(t *testing.T) {
net := tn.VirtualNetwork()
rs := tn.VirtualRoutingServer()
rs := mock.VirtualRoutingServer()
sg := NewSessionGenerator(net, rs)
bg := NewBlockGenerator(t)
......@@ -151,7 +152,7 @@ func TestSendToWantingPeer(t *testing.T) {
util.Debug = true
net := tn.VirtualNetwork()
rs := tn.VirtualRoutingServer()
rs := mock.VirtualRoutingServer()
sg := NewSessionGenerator(net, rs)
bg := NewBlockGenerator(t)
......@@ -237,7 +238,7 @@ func (bg *BlockGenerator) Blocks(n int) []*blocks.Block {
}
func NewSessionGenerator(
net tn.Network, rs tn.RoutingServer) SessionGenerator {
net tn.Network, rs mock.RoutingServer) SessionGenerator {
return SessionGenerator{
net: net,
rs: rs,
......@@ -248,7 +249,7 @@ func NewSessionGenerator(
type SessionGenerator struct {
seq int
net tn.Network
rs tn.RoutingServer
rs mock.RoutingServer
}
func (g *SessionGenerator) Next() instance {
......@@ -276,7 +277,7 @@ type instance struct {
// NB: It's easy make mistakes by providing the same peer ID to two different
// sessions. To safeguard, use the SessionGenerator to generate sessions. It's
// just a much better idea.
func session(net tn.Network, rs tn.RoutingServer, id peer.ID) instance {
func session(net tn.Network, rs mock.RoutingServer, id peer.ID) instance {
p := &peer.Peer{ID: id}
adapter := net.Adapter(p)
......
package bitswap
package mock
import (
"errors"
"math/rand"
"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"
"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"
peer "github.com/jbenet/go-ipfs/peer"
routing "github.com/jbenet/go-ipfs/routing"
u "github.com/jbenet/go-ipfs/util"
)
var _ routing.IpfsRouting = &MockRouter{}
type MockRouter struct {
datastore ds.Datastore
hashTable RoutingServer
peer *peer.Peer
}
func NewMockRouter(local *peer.Peer, dstore ds.Datastore) routing.IpfsRouting {
return &MockRouter{
datastore: dstore,
peer: local,
hashTable: VirtualRoutingServer(),
}
}
func (mr *MockRouter) SetRoutingServer(rs RoutingServer) {
mr.hashTable = rs
}
func (mr *MockRouter) PutValue(ctx context.Context, key u.Key, val []byte) error {
return mr.datastore.Put(ds.NewKey(string(key)), val)
}
func (mr *MockRouter) GetValue(ctx context.Context, key u.Key) ([]byte, error) {
v, err := mr.datastore.Get(ds.NewKey(string(key)))
if err != nil {
return nil, err
}
data, ok := v.([]byte)
if !ok {
return nil, errors.New("could not cast value from datastore")
}
return data, nil
}
func (mr *MockRouter) FindProviders(ctx context.Context, key u.Key) ([]*peer.Peer, error) {
return nil, nil
}
func (mr *MockRouter) FindPeer(ctx context.Context, pid peer.ID) (*peer.Peer, error) {
return nil, nil
}
func (mr *MockRouter) FindProvidersAsync(ctx context.Context, k u.Key, max int) <-chan *peer.Peer {
out := make(chan *peer.Peer)
go func() {
defer close(out)
for i, p := range mr.hashTable.Providers(k) {
if max <= i {
return
}
select {
case out <- p:
case <-ctx.Done():
return
}
}
}()
return out
}
func (mr *MockRouter) Provide(_ context.Context, key u.Key) error {
return mr.hashTable.Announce(mr.peer, key)
}
type RoutingServer interface {
Announce(*peer.Peer, u.Key) error
Providers(u.Key) []*peer.Peer
// Returns a Routing instance configured to query this hash table
Client(*peer.Peer) bsnet.Routing
Client(p *peer.Peer) routing.IpfsRouting
}
func VirtualRoutingServer() RoutingServer {
......@@ -62,36 +131,9 @@ func (rs *hashTable) Providers(k u.Key) []*peer.Peer {
return ret
}
func (rs *hashTable) Client(p *peer.Peer) bsnet.Routing {
return &routingClient{
func (rs *hashTable) Client(p *peer.Peer) routing.IpfsRouting {
return &MockRouter{
peer: p,
hashTable: rs,
}
}
type routingClient struct {
peer *peer.Peer
hashTable RoutingServer
}
func (a *routingClient) FindProvidersAsync(ctx context.Context, k u.Key, max int) <-chan *peer.Peer {
out := make(chan *peer.Peer)
go func() {
defer close(out)
for i, p := range a.hashTable.Providers(k) {
if max <= i {
return
}
select {
case out <- p:
case <-ctx.Done():
return
}
}
}()
return out
}
func (a *routingClient) Provide(_ context.Context, key u.Key) error {
return a.hashTable.Announce(a.peer, key)
}
package bitswap
package mock
import (
"bytes"
"testing"
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
)
import (
"github.com/jbenet/go-ipfs/peer"
u "github.com/jbenet/go-ipfs/util"
)
func TestKeyNotFound(t *testing.T) {
rs := func() RoutingServer {
// TODO fields
return &hashTable{}
}()
empty := rs.Providers(u.Key("not there"))
vrs := VirtualRoutingServer()
empty := vrs.Providers(u.Key("not there"))
if len(empty) != 0 {
t.Fatal("should be empty")
}
......@@ -47,11 +42,10 @@ func TestSetAndGet(t *testing.T) {
}
func TestClientFindProviders(t *testing.T) {
peer := &peer.Peer{
ID: []byte("42"),
}
peer := &peer.Peer{ID: []byte("42")}
rs := VirtualRoutingServer()
client := rs.Client(peer)
k := u.Key("hello")
err := client.Provide(context.Background(), k)
if err != nil {
......@@ -102,7 +96,9 @@ func TestClientOverMax(t *testing.T) {
}
max := 10
client := rs.Client(&peer.Peer{ID: []byte("TODO")})
peer := &peer.Peer{ID: []byte("TODO")}
client := rs.Client(peer)
providersFromClient := client.FindProvidersAsync(context.Background(), k, max)
i := 0
for _ = range providersFromClient {
......@@ -133,7 +129,8 @@ func TestCanceledContext(t *testing.T) {
}
}()
client := rs.Client(&peer.Peer{ID: []byte("peer id doesn't matter")})
local := &peer.Peer{ID: []byte("peer id doesn't matter")}
client := rs.Client(local)
t.Log("warning: max is finite so this test is non-deterministic")
t.Log("context cancellation could simply take lower priority")
......
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