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>
Brian Tiger Chow committed
Nov 26, 2014 at 12:08 UTC
7a3819a52809d0f4612accb6a0144f31f6b813b7
4 files changed
+31
-35
blocks/blocksutil/block_generator.go
new
+25
@@ -0,0 +1,25 @@
1
+package blocksutil
2
+
3
+import "github.com/jbenet/go-ipfs/blocks"
4
+
5
+func NewBlockGenerator() BlockGenerator {
6
+ return BlockGenerator{}
7
+}
8
+
9
+type BlockGenerator struct {
10
+ seq int
11
+}
12
+
13
+func (bg *BlockGenerator) Next() *blocks.Block {
14
+ bg.seq++
15
+ return blocks.NewBlock([]byte(string(bg.seq)))
16
+}
17
+
18
+func (bg *BlockGenerator) Blocks(n int) []*blocks.Block {
19
+ blocks := make([]*blocks.Block, 0)
20
+ for i := 0; i < n; i++ {
21
+ b := bg.Next()
22
+ blocks = append(blocks, b)
23
+ }
24
+ return blocks
25
+}
blockservice/blocks_test.go
+2
-1
@@ -10,6 +10,7 @@ import (
10
dssync "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
11
blocks "github.com/jbenet/go-ipfs/blocks"
12
blockstore "github.com/jbenet/go-ipfs/blocks/blockstore"
13
+ blocksutil "github.com/jbenet/go-ipfs/blocks/blocksutil"
14
bitswap "github.com/jbenet/go-ipfs/exchange/bitswap"
15
tn "github.com/jbenet/go-ipfs/exchange/bitswap/testnet"
16
offline "github.com/jbenet/go-ipfs/exchange/offline"
@@ -66,7 +67,7 @@ func TestGetBlocksSequential(t *testing.T) {
67
net := tn.VirtualNetwork()
68
rs := mock.VirtualRoutingServer()
69
sg := bitswap.NewSessionGenerator(net, rs)
69
- bg := bitswap.NewBlockGenerator()
70
+ bg := blocksutil.NewBlockGenerator()
71
72
instances := sg.Instances(4)
73
blks := bg.Blocks(50)
exchange/bitswap/bitswap_test.go
+4
-4
@@ -7,8 +7,8 @@ import (
7
"time"
8
9
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
10
-
10
blocks "github.com/jbenet/go-ipfs/blocks"
11
+ blocksutil "github.com/jbenet/go-ipfs/blocks/blocksutil"
12
tn "github.com/jbenet/go-ipfs/exchange/bitswap/testnet"
13
peer "github.com/jbenet/go-ipfs/peer"
14
mock "github.com/jbenet/go-ipfs/routing/mock"
@@ -20,7 +20,7 @@ func TestClose(t *testing.T) {
20
vnet := tn.VirtualNetwork()
21
rout := mock.VirtualRoutingServer()
22
sesgen := NewSessionGenerator(vnet, rout)
23
- bgen := NewBlockGenerator()
23
+ bgen := blocksutil.NewBlockGenerator()
24
25
block := bgen.Next()
26
bitswap := sesgen.Next()
@@ -124,7 +124,7 @@ func PerformDistributionTest(t *testing.T, numInstances, numBlocks int) {
124
net := tn.VirtualNetwork()
125
rs := mock.VirtualRoutingServer()
126
sg := NewSessionGenerator(net, rs)
127
- bg := NewBlockGenerator()
127
+ bg := blocksutil.NewBlockGenerator()
128
129
t.Log("Test a few nodes trying to get one file with a lot of blocks")
130
@@ -184,7 +184,7 @@ func TestSendToWantingPeer(t *testing.T) {
184
net := tn.VirtualNetwork()
185
rs := mock.VirtualRoutingServer()
186
sg := NewSessionGenerator(net, rs)
187
- bg := NewBlockGenerator()
187
+ bg := blocksutil.NewBlockGenerator()
188
189
me := sg.Next()
190
w := sg.Next()
exchange/bitswap/testutils.go
-30
@@ -4,7 +4,6 @@ import (
4
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
5
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
6
ds_sync "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
7
- "github.com/jbenet/go-ipfs/blocks"
7
"github.com/jbenet/go-ipfs/blocks/blockstore"
8
"github.com/jbenet/go-ipfs/exchange"
9
tn "github.com/jbenet/go-ipfs/exchange/bitswap/testnet"
@@ -12,35 +11,6 @@ import (
11
"github.com/jbenet/go-ipfs/routing/mock"
12
)
13
15
-/*
16
-TODO: This whole file needs somewhere better to live.
17
-The issue is that its very difficult to move it somewhere else
18
-without creating circular dependencies.
19
-Additional thought required.
20
-*/
21
-
22
-func NewBlockGenerator() BlockGenerator {
23
- return BlockGenerator{}
24
-}
25
-
26
-type BlockGenerator struct {
27
- seq int
28
-}
29
-
30
-func (bg *BlockGenerator) Next() *blocks.Block {
31
- bg.seq++
32
- return blocks.NewBlock([]byte(string(bg.seq)))
33
-}
34
-
35
-func (bg *BlockGenerator) Blocks(n int) []*blocks.Block {
36
- blocks := make([]*blocks.Block, 0)
37
- for i := 0; i < n; i++ {
38
- b := bg.Next()
39
- blocks = append(blocks, b)
40
- }
41
- return blocks
42
-}
43
-
14
func NewSessionGenerator(
15
net tn.Network, rs mock.RoutingServer) SessionGenerator {
16
return SessionGenerator{