diff --git a/blocks/blocksutil/block_generator.go b/blocks/blocksutil/block_generator.go new file mode 100644 index 000000000..20f6a3edc --- /dev/null +++ b/blocks/blocksutil/block_generator.go @@ -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 +} diff --git a/blockservice/blocks_test.go b/blockservice/blocks_test.go index 9fd85725e..1a75723e2 100644 --- a/blockservice/blocks_test.go +++ b/blockservice/blocks_test.go @@ -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) diff --git a/exchange/bitswap/bitswap_test.go b/exchange/bitswap/bitswap_test.go index 1da69560e..ede87c474 100644 --- a/exchange/bitswap/bitswap_test.go +++ b/exchange/bitswap/bitswap_test.go @@ -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() diff --git a/exchange/bitswap/testutils.go b/exchange/bitswap/testutils.go index d0064173f..402a5b1d2 100644 --- a/exchange/bitswap/testutils.go +++ b/exchange/bitswap/testutils.go @@ -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{