mirror of
https://github.com/ipfs/kubo.git
synced 2025-09-10 09:52:20 +08:00
refactor(mdag, bserv, bs) mocks, etc.
License: MIT Signed-off-by: Brian Tiger Chow <brian@perfmode.com>
This commit is contained in:
@ -11,10 +11,7 @@ import (
|
|||||||
blocks "github.com/jbenet/go-ipfs/blocks"
|
blocks "github.com/jbenet/go-ipfs/blocks"
|
||||||
blockstore "github.com/jbenet/go-ipfs/blocks/blockstore"
|
blockstore "github.com/jbenet/go-ipfs/blocks/blockstore"
|
||||||
blocksutil "github.com/jbenet/go-ipfs/blocks/blocksutil"
|
blocksutil "github.com/jbenet/go-ipfs/blocks/blocksutil"
|
||||||
bitswap "github.com/jbenet/go-ipfs/exchange/bitswap"
|
|
||||||
tn "github.com/jbenet/go-ipfs/exchange/bitswap/testnet"
|
|
||||||
offline "github.com/jbenet/go-ipfs/exchange/offline"
|
offline "github.com/jbenet/go-ipfs/exchange/offline"
|
||||||
"github.com/jbenet/go-ipfs/routing/mock"
|
|
||||||
u "github.com/jbenet/go-ipfs/util"
|
u "github.com/jbenet/go-ipfs/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -63,23 +60,9 @@ func TestBlocks(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGetBlocksSequential(t *testing.T) {
|
func TestGetBlocksSequential(t *testing.T) {
|
||||||
net := tn.VirtualNetwork()
|
var servs = Mocks(t, 4)
|
||||||
rs := mock.VirtualRoutingServer()
|
|
||||||
sg := bitswap.NewSessionGenerator(net, rs)
|
|
||||||
bg := blocksutil.NewBlockGenerator()
|
bg := blocksutil.NewBlockGenerator()
|
||||||
|
|
||||||
instances := sg.Instances(4)
|
|
||||||
blks := bg.Blocks(50)
|
blks := bg.Blocks(50)
|
||||||
// TODO: verify no duplicates
|
|
||||||
|
|
||||||
var servs []*BlockService
|
|
||||||
for _, i := range instances {
|
|
||||||
bserv, err := New(i.Blockstore, i.Exchange)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
servs = append(servs, bserv)
|
|
||||||
}
|
|
||||||
|
|
||||||
var keys []u.Key
|
var keys []u.Key
|
||||||
for _, blk := range blks {
|
for _, blk := range blks {
|
||||||
@ -89,7 +72,7 @@ func TestGetBlocksSequential(t *testing.T) {
|
|||||||
|
|
||||||
t.Log("one instance at a time, get blocks concurrently")
|
t.Log("one instance at a time, get blocks concurrently")
|
||||||
|
|
||||||
for i := 1; i < len(instances); i++ {
|
for i := 1; i < len(servs); i++ {
|
||||||
ctx, _ := context.WithTimeout(context.TODO(), time.Second*5)
|
ctx, _ := context.WithTimeout(context.TODO(), time.Second*5)
|
||||||
out := servs[i].GetBlocks(ctx, keys)
|
out := servs[i].GetBlocks(ctx, keys)
|
||||||
gotten := make(map[u.Key]*blocks.Block)
|
gotten := make(map[u.Key]*blocks.Block)
|
||||||
|
28
blockservice/mock.go
Normal file
28
blockservice/mock.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package blockservice
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
bitswap "github.com/jbenet/go-ipfs/exchange/bitswap"
|
||||||
|
tn "github.com/jbenet/go-ipfs/exchange/bitswap/testnet"
|
||||||
|
"github.com/jbenet/go-ipfs/routing/mock"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Mocks returns |n| connected mock Blockservices
|
||||||
|
func Mocks(t *testing.T, n int) []*BlockService {
|
||||||
|
net := tn.VirtualNetwork()
|
||||||
|
rs := mock.VirtualRoutingServer()
|
||||||
|
sg := bitswap.NewSessionGenerator(net, rs)
|
||||||
|
|
||||||
|
instances := sg.Instances(n)
|
||||||
|
|
||||||
|
var servs []*BlockService
|
||||||
|
for _, i := range instances {
|
||||||
|
bserv, err := New(i.Blockstore(), i.Exchange)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
servs = append(servs, bserv)
|
||||||
|
}
|
||||||
|
return servs
|
||||||
|
}
|
@ -28,10 +28,10 @@ import (
|
|||||||
pin "github.com/jbenet/go-ipfs/pin"
|
pin "github.com/jbenet/go-ipfs/pin"
|
||||||
routing "github.com/jbenet/go-ipfs/routing"
|
routing "github.com/jbenet/go-ipfs/routing"
|
||||||
dht "github.com/jbenet/go-ipfs/routing/dht"
|
dht "github.com/jbenet/go-ipfs/routing/dht"
|
||||||
u "github.com/jbenet/go-ipfs/util"
|
|
||||||
ctxc "github.com/jbenet/go-ipfs/util/ctxcloser"
|
ctxc "github.com/jbenet/go-ipfs/util/ctxcloser"
|
||||||
|
ds2 "github.com/jbenet/go-ipfs/util/datastore2"
|
||||||
debugerror "github.com/jbenet/go-ipfs/util/debugerror"
|
debugerror "github.com/jbenet/go-ipfs/util/debugerror"
|
||||||
"github.com/jbenet/go-ipfs/util/eventlog"
|
eventlog "github.com/jbenet/go-ipfs/util/eventlog"
|
||||||
)
|
)
|
||||||
|
|
||||||
const IpnsValidatorTag = "ipns"
|
const IpnsValidatorTag = "ipns"
|
||||||
@ -52,7 +52,7 @@ type IpfsNode struct {
|
|||||||
Peerstore peer.Peerstore
|
Peerstore peer.Peerstore
|
||||||
|
|
||||||
// the local datastore
|
// the local datastore
|
||||||
Datastore u.ThreadSafeDatastoreCloser
|
Datastore ds2.ThreadSafeDatastoreCloser
|
||||||
|
|
||||||
// the network message stream
|
// the network message stream
|
||||||
Network inet.Network
|
Network inet.Network
|
||||||
|
@ -10,10 +10,11 @@ import (
|
|||||||
|
|
||||||
config "github.com/jbenet/go-ipfs/config"
|
config "github.com/jbenet/go-ipfs/config"
|
||||||
u "github.com/jbenet/go-ipfs/util"
|
u "github.com/jbenet/go-ipfs/util"
|
||||||
|
ds2 "github.com/jbenet/go-ipfs/util/datastore2"
|
||||||
"github.com/jbenet/go-ipfs/util/debugerror"
|
"github.com/jbenet/go-ipfs/util/debugerror"
|
||||||
)
|
)
|
||||||
|
|
||||||
func makeDatastore(cfg config.Datastore) (u.ThreadSafeDatastoreCloser, error) {
|
func makeDatastore(cfg config.Datastore) (ds2.ThreadSafeDatastoreCloser, error) {
|
||||||
if len(cfg.Type) == 0 {
|
if len(cfg.Type) == 0 {
|
||||||
return nil, debugerror.Errorf("config datastore.type required")
|
return nil, debugerror.Errorf("config datastore.type required")
|
||||||
}
|
}
|
||||||
@ -23,7 +24,7 @@ func makeDatastore(cfg config.Datastore) (u.ThreadSafeDatastoreCloser, error) {
|
|||||||
return makeLevelDBDatastore(cfg)
|
return makeLevelDBDatastore(cfg)
|
||||||
|
|
||||||
case "memory":
|
case "memory":
|
||||||
return u.CloserWrap(syncds.MutexWrap(ds.NewMapDatastore())), nil
|
return ds2.CloserWrap(syncds.MutexWrap(ds.NewMapDatastore())), nil
|
||||||
|
|
||||||
case "fs":
|
case "fs":
|
||||||
log.Warning("using fs.Datastore at .datastore for testing.")
|
log.Warning("using fs.Datastore at .datastore for testing.")
|
||||||
@ -32,13 +33,13 @@ func makeDatastore(cfg config.Datastore) (u.ThreadSafeDatastoreCloser, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
ktd := ktds.Wrap(d, u.B58KeyConverter)
|
ktd := ktds.Wrap(d, u.B58KeyConverter)
|
||||||
return u.CloserWrap(syncds.MutexWrap(ktd)), nil
|
return ds2.CloserWrap(syncds.MutexWrap(ktd)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, debugerror.Errorf("Unknown datastore type: %s", cfg.Type)
|
return nil, debugerror.Errorf("Unknown datastore type: %s", cfg.Type)
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeLevelDBDatastore(cfg config.Datastore) (u.ThreadSafeDatastoreCloser, error) {
|
func makeLevelDBDatastore(cfg config.Datastore) (ds2.ThreadSafeDatastoreCloser, error) {
|
||||||
if len(cfg.Path) == 0 {
|
if len(cfg.Path) == 0 {
|
||||||
return nil, debugerror.Errorf("config datastore.path required for leveldb")
|
return nil, debugerror.Errorf("config datastore.path required for leveldb")
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ import (
|
|||||||
path "github.com/jbenet/go-ipfs/path"
|
path "github.com/jbenet/go-ipfs/path"
|
||||||
peer "github.com/jbenet/go-ipfs/peer"
|
peer "github.com/jbenet/go-ipfs/peer"
|
||||||
mdht "github.com/jbenet/go-ipfs/routing/mock"
|
mdht "github.com/jbenet/go-ipfs/routing/mock"
|
||||||
"github.com/jbenet/go-ipfs/util"
|
ds2 "github.com/jbenet/go-ipfs/util/datastore2"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewMockNode constructs an IpfsNode for use in tests.
|
// NewMockNode constructs an IpfsNode for use in tests.
|
||||||
@ -39,7 +39,7 @@ func NewMockNode() (*IpfsNode, error) {
|
|||||||
|
|
||||||
// Temp Datastore
|
// Temp Datastore
|
||||||
dstore := ds.NewMapDatastore()
|
dstore := ds.NewMapDatastore()
|
||||||
nd.Datastore = util.CloserWrap(syncds.MutexWrap(dstore))
|
nd.Datastore = ds2.CloserWrap(syncds.MutexWrap(dstore))
|
||||||
|
|
||||||
// Routing
|
// Routing
|
||||||
dht := mdht.NewMockRouter(nd.Identity, nd.Datastore)
|
dht := mdht.NewMockRouter(nd.Identity, nd.Datastore)
|
||||||
|
@ -76,7 +76,7 @@ func TestGetBlockFromPeerAfterPeerAnnounces(t *testing.T) {
|
|||||||
|
|
||||||
hasBlock := g.Next()
|
hasBlock := g.Next()
|
||||||
|
|
||||||
if err := hasBlock.Blockstore.Put(block); err != nil {
|
if err := hasBlock.Blockstore().Put(block); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := hasBlock.Exchange.HasBlock(context.Background(), block); err != nil {
|
if err := hasBlock.Exchange.HasBlock(context.Background(), block); err != nil {
|
||||||
@ -135,7 +135,7 @@ func PerformDistributionTest(t *testing.T, numInstances, numBlocks int) {
|
|||||||
|
|
||||||
first := instances[0]
|
first := instances[0]
|
||||||
for _, b := range blocks {
|
for _, b := range blocks {
|
||||||
first.Blockstore.Put(b)
|
first.Blockstore().Put(b)
|
||||||
first.Exchange.HasBlock(context.Background(), b)
|
first.Exchange.HasBlock(context.Background(), b)
|
||||||
rs.Announce(first.Peer, b.Key())
|
rs.Announce(first.Peer, b.Key())
|
||||||
}
|
}
|
||||||
@ -158,7 +158,7 @@ func PerformDistributionTest(t *testing.T, numInstances, numBlocks int) {
|
|||||||
|
|
||||||
for _, inst := range instances {
|
for _, inst := range instances {
|
||||||
for _, b := range blocks {
|
for _, b := range blocks {
|
||||||
if _, err := inst.Blockstore.Get(b.Key()); err != nil {
|
if _, err := inst.Blockstore().Get(b.Key()); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -166,7 +166,7 @@ func PerformDistributionTest(t *testing.T, numInstances, numBlocks int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getOrFail(bitswap Instance, b *blocks.Block, t *testing.T, wg *sync.WaitGroup) {
|
func getOrFail(bitswap Instance, b *blocks.Block, t *testing.T, wg *sync.WaitGroup) {
|
||||||
if _, err := bitswap.Blockstore.Get(b.Key()); err != nil {
|
if _, err := bitswap.Blockstore().Get(b.Key()); err != nil {
|
||||||
_, err := bitswap.Exchange.GetBlock(context.Background(), b.Key())
|
_, err := bitswap.Exchange.GetBlock(context.Background(), b.Key())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@ -208,7 +208,7 @@ func TestSendToWantingPeer(t *testing.T) {
|
|||||||
beta := bg.Next()
|
beta := bg.Next()
|
||||||
t.Logf("Peer %v announes availability of %v\n", w.Peer, beta.Key())
|
t.Logf("Peer %v announes availability of %v\n", w.Peer, beta.Key())
|
||||||
ctx, _ = context.WithTimeout(context.Background(), timeout)
|
ctx, _ = context.WithTimeout(context.Background(), timeout)
|
||||||
if err := w.Blockstore.Put(beta); err != nil {
|
if err := w.Blockstore().Put(beta); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
w.Exchange.HasBlock(ctx, beta)
|
w.Exchange.HasBlock(ctx, beta)
|
||||||
@ -221,7 +221,7 @@ func TestSendToWantingPeer(t *testing.T) {
|
|||||||
|
|
||||||
t.Logf("%v announces availability of %v\n", o.Peer, alpha.Key())
|
t.Logf("%v announces availability of %v\n", o.Peer, alpha.Key())
|
||||||
ctx, _ = context.WithTimeout(context.Background(), timeout)
|
ctx, _ = context.WithTimeout(context.Background(), timeout)
|
||||||
if err := o.Blockstore.Put(alpha); err != nil {
|
if err := o.Blockstore().Put(alpha); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
o.Exchange.HasBlock(ctx, alpha)
|
o.Exchange.HasBlock(ctx, alpha)
|
||||||
@ -233,7 +233,7 @@ func TestSendToWantingPeer(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
t.Logf("%v should now have %v\n", w.Peer, alpha.Key())
|
t.Logf("%v should now have %v\n", w.Peer, alpha.Key())
|
||||||
block, err := w.Blockstore.Get(alpha.Key())
|
block, err := w.Blockstore().Get(alpha.Key())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Should not have received an error: %s", err)
|
t.Fatalf("Should not have received an error: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,18 @@
|
|||||||
package bitswap
|
package bitswap
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
"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/go-datastore"
|
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
|
||||||
ds_sync "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
|
ds_sync "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
|
||||||
"github.com/jbenet/go-ipfs/blocks/blockstore"
|
blockstore "github.com/jbenet/go-ipfs/blocks/blockstore"
|
||||||
"github.com/jbenet/go-ipfs/exchange"
|
exchange "github.com/jbenet/go-ipfs/exchange"
|
||||||
tn "github.com/jbenet/go-ipfs/exchange/bitswap/testnet"
|
tn "github.com/jbenet/go-ipfs/exchange/bitswap/testnet"
|
||||||
"github.com/jbenet/go-ipfs/peer"
|
peer "github.com/jbenet/go-ipfs/peer"
|
||||||
"github.com/jbenet/go-ipfs/routing/mock"
|
mock "github.com/jbenet/go-ipfs/routing/mock"
|
||||||
|
datastore2 "github.com/jbenet/go-ipfs/util/datastore2"
|
||||||
|
delay "github.com/jbenet/go-ipfs/util/delay"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewSessionGenerator(
|
func NewSessionGenerator(
|
||||||
@ -45,7 +49,17 @@ func (g *SessionGenerator) Instances(n int) []Instance {
|
|||||||
type Instance struct {
|
type Instance struct {
|
||||||
Peer peer.Peer
|
Peer peer.Peer
|
||||||
Exchange exchange.Interface
|
Exchange exchange.Interface
|
||||||
Blockstore blockstore.Blockstore
|
blockstore blockstore.Blockstore
|
||||||
|
|
||||||
|
blockstoreDelay delay.D
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Instance) Blockstore() blockstore.Blockstore {
|
||||||
|
return i.blockstore
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Instance) SetBlockstoreLatency(t time.Duration) time.Duration {
|
||||||
|
return i.blockstoreDelay.Set(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
// session creates a test bitswap session.
|
// session creates a test bitswap session.
|
||||||
@ -58,7 +72,9 @@ func session(net tn.Network, rs mock.RoutingServer, ps peer.Peerstore, id peer.I
|
|||||||
|
|
||||||
adapter := net.Adapter(p)
|
adapter := net.Adapter(p)
|
||||||
htc := rs.Client(p)
|
htc := rs.Client(p)
|
||||||
bstore := blockstore.NewBlockstore(ds_sync.MutexWrap(ds.NewMapDatastore()))
|
|
||||||
|
bsdelay := delay.Fixed(0)
|
||||||
|
bstore := blockstore.NewBlockstore(ds_sync.MutexWrap(datastore2.WithDelay(ds.NewMapDatastore(), bsdelay)))
|
||||||
|
|
||||||
const alwaysSendToPeer = true
|
const alwaysSendToPeer = true
|
||||||
ctx := context.TODO()
|
ctx := context.TODO()
|
||||||
@ -66,8 +82,9 @@ func session(net tn.Network, rs mock.RoutingServer, ps peer.Peerstore, id peer.I
|
|||||||
bs := New(ctx, p, adapter, htc, bstore, alwaysSendToPeer)
|
bs := New(ctx, p, adapter, htc, bstore, alwaysSendToPeer)
|
||||||
|
|
||||||
return Instance{
|
return Instance{
|
||||||
Peer: p,
|
Peer: p,
|
||||||
Exchange: bs,
|
Exchange: bs,
|
||||||
Blockstore: bstore,
|
blockstore: bstore,
|
||||||
|
blockstoreDelay: bsdelay,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,10 +9,10 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/jbenet/go-ipfs/importer/chunk"
|
chunk "github.com/jbenet/go-ipfs/importer/chunk"
|
||||||
|
merkledag "github.com/jbenet/go-ipfs/merkledag"
|
||||||
uio "github.com/jbenet/go-ipfs/unixfs/io"
|
uio "github.com/jbenet/go-ipfs/unixfs/io"
|
||||||
u "github.com/jbenet/go-ipfs/util"
|
u "github.com/jbenet/go-ipfs/util"
|
||||||
testutil "github.com/jbenet/go-ipfs/util/testutil"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// NOTE:
|
// NOTE:
|
||||||
@ -91,7 +91,7 @@ func TestBuilderConsistency(t *testing.T) {
|
|||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
io.CopyN(buf, u.NewTimeSeededRand(), int64(nbytes))
|
io.CopyN(buf, u.NewTimeSeededRand(), int64(nbytes))
|
||||||
should := dup(buf.Bytes())
|
should := dup(buf.Bytes())
|
||||||
dagserv := testutil.GetDAGServ(t)
|
dagserv := merkledag.Mock(t)
|
||||||
nd, err := BuildDagFromReader(buf, dagserv, nil, chunk.DefaultSplitter)
|
nd, err := BuildDagFromReader(buf, dagserv, nil, chunk.DefaultSplitter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
@ -7,13 +7,10 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
bserv "github.com/jbenet/go-ipfs/blockservice"
|
blockservice "github.com/jbenet/go-ipfs/blockservice"
|
||||||
bs "github.com/jbenet/go-ipfs/exchange/bitswap"
|
|
||||||
tn "github.com/jbenet/go-ipfs/exchange/bitswap/testnet"
|
|
||||||
imp "github.com/jbenet/go-ipfs/importer"
|
imp "github.com/jbenet/go-ipfs/importer"
|
||||||
chunk "github.com/jbenet/go-ipfs/importer/chunk"
|
chunk "github.com/jbenet/go-ipfs/importer/chunk"
|
||||||
. "github.com/jbenet/go-ipfs/merkledag"
|
. "github.com/jbenet/go-ipfs/merkledag"
|
||||||
"github.com/jbenet/go-ipfs/routing/mock"
|
|
||||||
uio "github.com/jbenet/go-ipfs/unixfs/io"
|
uio "github.com/jbenet/go-ipfs/unixfs/io"
|
||||||
u "github.com/jbenet/go-ipfs/util"
|
u "github.com/jbenet/go-ipfs/util"
|
||||||
)
|
)
|
||||||
@ -79,20 +76,8 @@ func makeTestDag(t *testing.T) *Node {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestBatchFetch(t *testing.T) {
|
func TestBatchFetch(t *testing.T) {
|
||||||
net := tn.VirtualNetwork()
|
|
||||||
rs := mock.VirtualRoutingServer()
|
|
||||||
sg := bs.NewSessionGenerator(net, rs)
|
|
||||||
|
|
||||||
instances := sg.Instances(5)
|
|
||||||
|
|
||||||
var servs []*bserv.BlockService
|
|
||||||
var dagservs []DAGService
|
var dagservs []DAGService
|
||||||
for _, i := range instances {
|
for _, bsi := range blockservice.Mocks(t, 5) {
|
||||||
bsi, err := bserv.New(i.Blockstore, i.Exchange)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
servs = append(servs, bsi)
|
|
||||||
dagservs = append(dagservs, NewDAGService(bsi))
|
dagservs = append(dagservs, NewDAGService(bsi))
|
||||||
}
|
}
|
||||||
t.Log("finished setup.")
|
t.Log("finished setup.")
|
||||||
|
20
merkledag/mock.go
Normal file
20
merkledag/mock.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package merkledag
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
|
||||||
|
dssync "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
|
||||||
|
"github.com/jbenet/go-ipfs/blocks/blockstore"
|
||||||
|
bsrv "github.com/jbenet/go-ipfs/blockservice"
|
||||||
|
"github.com/jbenet/go-ipfs/exchange/offline"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Mock(t testing.TB) DAGService {
|
||||||
|
bstore := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
|
||||||
|
bserv, err := bsrv.New(bstore, offline.Exchange(bstore))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
return NewDAGService(bserv)
|
||||||
|
}
|
@ -30,10 +30,6 @@ func NewMockRouter(local peer.Peer, dstore ds.Datastore) routing.IpfsRouting {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mr *MockRouter) SetRoutingServer(rs RoutingServer) {
|
|
||||||
mr.hashTable = rs
|
|
||||||
}
|
|
||||||
|
|
||||||
func (mr *MockRouter) PutValue(ctx context.Context, key u.Key, val []byte) error {
|
func (mr *MockRouter) PutValue(ctx context.Context, key u.Key, val []byte) error {
|
||||||
log.Debugf("PutValue: %s", key)
|
log.Debugf("PutValue: %s", key)
|
||||||
return mr.datastore.Put(key.DsKey(), val)
|
return mr.datastore.Put(key.DsKey(), val)
|
||||||
@ -119,7 +115,8 @@ func (rs *hashTable) Announce(p peer.Peer, k u.Key) error {
|
|||||||
func (rs *hashTable) Providers(k u.Key) []peer.Peer {
|
func (rs *hashTable) Providers(k u.Key) []peer.Peer {
|
||||||
rs.lock.RLock()
|
rs.lock.RLock()
|
||||||
defer rs.lock.RUnlock()
|
defer rs.lock.RUnlock()
|
||||||
ret := make([]peer.Peer, 0)
|
|
||||||
|
var ret []peer.Peer
|
||||||
peerset, ok := rs.providers[k]
|
peerset, ok := rs.providers[k]
|
||||||
if !ok {
|
if !ok {
|
||||||
return ret
|
return ret
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package util
|
package datastore2
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
@ -2,28 +2,10 @@ package testutil
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
crand "crypto/rand"
|
crand "crypto/rand"
|
||||||
"testing"
|
peer "github.com/jbenet/go-ipfs/peer"
|
||||||
|
|
||||||
dssync "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
|
|
||||||
"github.com/jbenet/go-ipfs/exchange/offline"
|
|
||||||
"github.com/jbenet/go-ipfs/peer"
|
|
||||||
|
|
||||||
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
|
|
||||||
"github.com/jbenet/go-ipfs/blocks/blockstore"
|
|
||||||
bsrv "github.com/jbenet/go-ipfs/blockservice"
|
|
||||||
dag "github.com/jbenet/go-ipfs/merkledag"
|
|
||||||
u "github.com/jbenet/go-ipfs/util"
|
u "github.com/jbenet/go-ipfs/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetDAGServ(t testing.TB) dag.DAGService {
|
|
||||||
bstore := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
|
|
||||||
bserv, err := bsrv.New(bstore, offline.Exchange(bstore))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
return dag.NewDAGService(bserv)
|
|
||||||
}
|
|
||||||
|
|
||||||
func RandPeer() peer.Peer {
|
func RandPeer() peer.Peer {
|
||||||
id := make([]byte, 16)
|
id := make([]byte, 16)
|
||||||
crand.Read(id)
|
crand.Read(id)
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
package testutil
|
package testutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/jbenet/go-ipfs/peer"
|
|
||||||
|
|
||||||
ic "github.com/jbenet/go-ipfs/crypto"
|
ic "github.com/jbenet/go-ipfs/crypto"
|
||||||
|
peer "github.com/jbenet/go-ipfs/peer"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewPeerWithKeyPair(sk ic.PrivKey, pk ic.PubKey) (peer.Peer, error) {
|
func NewPeerWithKeyPair(sk ic.PrivKey, pk ic.PubKey) (peer.Peer, error) {
|
||||||
|
Reference in New Issue
Block a user