diff --git a/blocks/blocksutil/block_generator.go b/blocks/blocksutil/block_generator.go new file mode 100644 index 0000000000000000000000000000000000000000..20f6a3edc07b2e1d3f6e9f715162a1ce2c72ec9c --- /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 9fd85725e8ee5d7eef0eda63aa6977cc79448176..1a75723e2bd4c809129e0efea687d4a800beea7c 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 1da69560ed3164bc3272eecc66dc6bb66a5d8c06..ede87c47477ba3f22ea32442115c48e1d712f05b 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 d0064173fcc664ad2df606d8d9a967bc56ec9651..402a5b1d26b2724547c7d9d389f32d512d0ad8f1 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{