mirror of
https://github.com/ipfs/kubo.git
synced 2025-09-10 05:52:20 +08:00
refactor(util) move block generator
@whyrusleeping @jbenet Putting the block generator in a util dir until blocks. Can't put it in util/testutil because the util/testutil/dag-generator imports blockservice and blockservice uses the generator. Tough problem. This'll do for now. License: MIT Signed-off-by: Brian Tiger Chow <brian@perfmode.com>
This commit is contained in:
25
blocks/blocksutil/block_generator.go
Normal file
25
blocks/blocksutil/block_generator.go
Normal file
@ -0,0 +1,25 @@
|
||||
package blocksutil
|
||||
|
||||
import "github.com/jbenet/go-ipfs/blocks"
|
||||
|
||||
func NewBlockGenerator() BlockGenerator {
|
||||
return BlockGenerator{}
|
||||
}
|
||||
|
||||
type BlockGenerator struct {
|
||||
seq int
|
||||
}
|
||||
|
||||
func (bg *BlockGenerator) Next() *blocks.Block {
|
||||
bg.seq++
|
||||
return blocks.NewBlock([]byte(string(bg.seq)))
|
||||
}
|
||||
|
||||
func (bg *BlockGenerator) Blocks(n int) []*blocks.Block {
|
||||
blocks := make([]*blocks.Block, 0)
|
||||
for i := 0; i < n; i++ {
|
||||
b := bg.Next()
|
||||
blocks = append(blocks, b)
|
||||
}
|
||||
return blocks
|
||||
}
|
@ -10,6 +10,7 @@ import (
|
||||
dssync "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
|
||||
blocks "github.com/jbenet/go-ipfs/blocks"
|
||||
blockstore "github.com/jbenet/go-ipfs/blocks/blockstore"
|
||||
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"
|
||||
@ -66,7 +67,7 @@ func TestGetBlocksSequential(t *testing.T) {
|
||||
net := tn.VirtualNetwork()
|
||||
rs := mock.VirtualRoutingServer()
|
||||
sg := bitswap.NewSessionGenerator(net, rs)
|
||||
bg := bitswap.NewBlockGenerator()
|
||||
bg := blocksutil.NewBlockGenerator()
|
||||
|
||||
instances := sg.Instances(4)
|
||||
blks := bg.Blocks(50)
|
||||
|
@ -7,8 +7,8 @@ import (
|
||||
"time"
|
||||
|
||||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||
|
||||
blocks "github.com/jbenet/go-ipfs/blocks"
|
||||
blocksutil "github.com/jbenet/go-ipfs/blocks/blocksutil"
|
||||
tn "github.com/jbenet/go-ipfs/exchange/bitswap/testnet"
|
||||
peer "github.com/jbenet/go-ipfs/peer"
|
||||
mock "github.com/jbenet/go-ipfs/routing/mock"
|
||||
@ -20,7 +20,7 @@ func TestClose(t *testing.T) {
|
||||
vnet := tn.VirtualNetwork()
|
||||
rout := mock.VirtualRoutingServer()
|
||||
sesgen := NewSessionGenerator(vnet, rout)
|
||||
bgen := NewBlockGenerator()
|
||||
bgen := blocksutil.NewBlockGenerator()
|
||||
|
||||
block := bgen.Next()
|
||||
bitswap := sesgen.Next()
|
||||
@ -124,7 +124,7 @@ func PerformDistributionTest(t *testing.T, numInstances, numBlocks int) {
|
||||
net := tn.VirtualNetwork()
|
||||
rs := mock.VirtualRoutingServer()
|
||||
sg := NewSessionGenerator(net, rs)
|
||||
bg := NewBlockGenerator()
|
||||
bg := blocksutil.NewBlockGenerator()
|
||||
|
||||
t.Log("Test a few nodes trying to get one file with a lot of blocks")
|
||||
|
||||
@ -184,7 +184,7 @@ func TestSendToWantingPeer(t *testing.T) {
|
||||
net := tn.VirtualNetwork()
|
||||
rs := mock.VirtualRoutingServer()
|
||||
sg := NewSessionGenerator(net, rs)
|
||||
bg := NewBlockGenerator()
|
||||
bg := blocksutil.NewBlockGenerator()
|
||||
|
||||
me := sg.Next()
|
||||
w := sg.Next()
|
||||
|
@ -4,7 +4,6 @@ import (
|
||||
"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_sync "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
|
||||
"github.com/jbenet/go-ipfs/blocks"
|
||||
"github.com/jbenet/go-ipfs/blocks/blockstore"
|
||||
"github.com/jbenet/go-ipfs/exchange"
|
||||
tn "github.com/jbenet/go-ipfs/exchange/bitswap/testnet"
|
||||
@ -12,35 +11,6 @@ import (
|
||||
"github.com/jbenet/go-ipfs/routing/mock"
|
||||
)
|
||||
|
||||
/*
|
||||
TODO: This whole file needs somewhere better to live.
|
||||
The issue is that its very difficult to move it somewhere else
|
||||
without creating circular dependencies.
|
||||
Additional thought required.
|
||||
*/
|
||||
|
||||
func NewBlockGenerator() BlockGenerator {
|
||||
return BlockGenerator{}
|
||||
}
|
||||
|
||||
type BlockGenerator struct {
|
||||
seq int
|
||||
}
|
||||
|
||||
func (bg *BlockGenerator) Next() *blocks.Block {
|
||||
bg.seq++
|
||||
return blocks.NewBlock([]byte(string(bg.seq)))
|
||||
}
|
||||
|
||||
func (bg *BlockGenerator) Blocks(n int) []*blocks.Block {
|
||||
blocks := make([]*blocks.Block, 0)
|
||||
for i := 0; i < n; i++ {
|
||||
b := bg.Next()
|
||||
blocks = append(blocks, b)
|
||||
}
|
||||
return blocks
|
||||
}
|
||||
|
||||
func NewSessionGenerator(
|
||||
net tn.Network, rs mock.RoutingServer) SessionGenerator {
|
||||
return SessionGenerator{
|
||||
|
Reference in New Issue
Block a user