@cryptotaxi247 / kubo / commits / f20976335

blockservice.New doesnt need to return an error

License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>

Jeromy committed Aug 14, 2015 at 16:25 UTC f20976335701df54b520cdabb2edcbdfd0ecb10f
15 files changed +53 -111
blockservice/blockservice.go
+2 -6
@@ -5,7 +5,6 @@ package blockservice
5
6 import (
7 "errors"
8 - "fmt"
8
9 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
10 blocks "github.com/ipfs/go-ipfs/blocks"
@@ -48,10 +47,7 @@ type BlockService struct {
47 }
48
49 // NewBlockService creates a BlockService with given datastore instance.
51 -func New(bs blockstore.Blockstore, rem exchange.Interface) (*BlockService, error) {
52 - if bs == nil {
53 - return nil, fmt.Errorf("BlockService requires valid blockstore")
54 - }
50 +func New(bs blockstore.Blockstore, rem exchange.Interface) *BlockService {
51 if rem == nil {
52 log.Warning("blockservice running in local (offline) mode.")
53 }
@@ -60,7 +56,7 @@ func New(bs blockstore.Blockstore, rem exchange.Interface) (*BlockService, error
56 Blockstore: bs,
57 Exchange: rem,
58 worker: worker.NewWorker(rem, wc),
63 - }, nil
59 + }
60 }
61
62 // AddBlock adds a particular block to the service, Putting it into the datastore.
blockservice/test/blocks_test.go
+2 -6
@@ -19,11 +19,7 @@ import (
19
20 func TestBlocks(t *testing.T) {
21 bstore := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
22 - bs, err := New(bstore, offline.Exchange(bstore))
23 - if err != nil {
24 - t.Error("failed to construct block service", err)
25 - return
26 - }
22 + bs := New(bstore, offline.Exchange(bstore))
23 defer bs.Close()
24
25 b := blocks.NewBlock([]byte("beep boop"))
@@ -63,7 +59,7 @@ func TestBlocks(t *testing.T) {
59 }
60
61 func TestGetBlocksSequential(t *testing.T) {
66 - var servs = Mocks(t, 4)
62 + var servs = Mocks(4)
63 for _, s := range servs {
64 defer s.Close()
65 }
blockservice/test/mock.go
+2 -10
@@ -8,12 +8,8 @@ import (
8 delay "github.com/ipfs/go-ipfs/thirdparty/delay"
9 )
10
11 -type fataler interface {
12 - Fatal(args ...interface{})
13 -}
14 -
11 // Mocks returns |n| connected mock Blockservices
16 -func Mocks(t fataler, n int) []*BlockService {
12 +func Mocks(n int) []*BlockService {
13 net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(0))
14 sg := bitswap.NewTestSessionGenerator(net)
15
@@ -21,11 +17,7 @@ func Mocks(t fataler, n int) []*BlockService {
17
18 var servs []*BlockService
19 for _, i := range instances {
24 - bserv, err := New(i.Blockstore(), i.Exchange)
25 - if err != nil {
26 - t.Fatal(err)
27 - }
28 - servs = append(servs, bserv)
20 + servs = append(servs, New(i.Blockstore(), i.Exchange))
21 }
22 return servs
23 }
core/core.go
+2 -4
@@ -148,10 +148,8 @@ func NewIPFSNode(ctx context.Context, option ConfigOption) (*IpfsNode, error) {
148 // to be initialized at this point, and 2) which variables will be
149 // initialized after this point.
150
151 - node.Blocks, err = bserv.New(node.Blockstore, node.Exchange)
152 - if err != nil {
153 - return nil, err
154 - }
151 + node.Blocks = bserv.New(node.Blockstore, node.Exchange)
152 +
153 if node.Peerstore == nil {
154 node.Peerstore = peer.NewPeerstore()
155 }
core/coreunix/metadata_test.go
+1 -4
@@ -25,10 +25,7 @@ import (
25 func getDagserv(t *testing.T) merkledag.DAGService {
26 db := dssync.MutexWrap(ds.NewMapDatastore())
27 bs := bstore.NewBlockstore(db)
28 - blockserv, err := bserv.New(bs, offline.Exchange(bs))
29 - if err != nil {
30 - t.Fatal(err)
31 - }
28 + blockserv := bserv.New(bs, offline.Exchange(bs))
29 return merkledag.NewDAGService(blockserv)
30 }
31
core/mock/mock.go
+1 -4
@@ -68,10 +68,7 @@ func NewMockNode() (*core.IpfsNode, error) {
68
69 // Bitswap
70 bstore := blockstore.NewBlockstore(nd.Repo.Datastore())
71 - bserv, err := blockservice.New(bstore, offline.Exchange(bstore))
72 - if err != nil {
73 - return nil, err
74 - }
71 + bserv := blockservice.New(bstore, offline.Exchange(bstore))
72
73 nd.DAG = mdag.NewDAGService(bserv)
74
importer/balanced/balanced_test.go
+10 -10
@@ -72,7 +72,7 @@ func testFileConsistency(t *testing.T, bs chunk.SplitterGen, nbytes int) {
72 u.NewTimeSeededRand().Read(should)
73
74 read := bytes.NewReader(should)
75 - ds := mdtest.Mock(t)
75 + ds := mdtest.Mock()
76 nd, err := buildTestDag(ds, bs(read))
77 if err != nil {
78 t.Fatal(err)
@@ -95,7 +95,7 @@ func testFileConsistency(t *testing.T, bs chunk.SplitterGen, nbytes int) {
95 }
96
97 func TestBuilderConsistency(t *testing.T) {
98 - dagserv := mdtest.Mock(t)
98 + dagserv := mdtest.Mock()
99 nd, should := getTestDag(t, dagserv, 100000, chunk.DefaultBlockSize)
100
101 r, err := uio.NewDagReader(context.Background(), nd, dagserv)
@@ -132,7 +132,7 @@ type dagservAndPinner struct {
132 }
133
134 func TestIndirectBlocks(t *testing.T) {
135 - ds := mdtest.Mock(t)
135 + ds := mdtest.Mock()
136 dag, buf := getTestDag(t, ds, 1024*1024, 512)
137
138 reader, err := uio.NewDagReader(context.Background(), dag, ds)
@@ -152,7 +152,7 @@ func TestIndirectBlocks(t *testing.T) {
152
153 func TestSeekingBasic(t *testing.T) {
154 nbytes := int64(10 * 1024)
155 - ds := mdtest.Mock(t)
155 + ds := mdtest.Mock()
156 nd, should := getTestDag(t, ds, nbytes, 500)
157
158 rs, err := uio.NewDagReader(context.Background(), nd, ds)
@@ -181,7 +181,7 @@ func TestSeekingBasic(t *testing.T) {
181 }
182
183 func TestSeekToBegin(t *testing.T) {
184 - ds := mdtest.Mock(t)
184 + ds := mdtest.Mock()
185 nd, should := getTestDag(t, ds, 10*1024, 500)
186
187 rs, err := uio.NewDagReader(context.Background(), nd, ds)
@@ -217,7 +217,7 @@ func TestSeekToBegin(t *testing.T) {
217 }
218
219 func TestSeekToAlmostBegin(t *testing.T) {
220 - ds := mdtest.Mock(t)
220 + ds := mdtest.Mock()
221 nd, should := getTestDag(t, ds, 10*1024, 500)
222
223 rs, err := uio.NewDagReader(context.Background(), nd, ds)
@@ -254,7 +254,7 @@ func TestSeekToAlmostBegin(t *testing.T) {
254
255 func TestSeekEnd(t *testing.T) {
256 nbytes := int64(50 * 1024)
257 - ds := mdtest.Mock(t)
257 + ds := mdtest.Mock()
258 nd, _ := getTestDag(t, ds, nbytes, 500)
259
260 rs, err := uio.NewDagReader(context.Background(), nd, ds)
@@ -273,7 +273,7 @@ func TestSeekEnd(t *testing.T) {
273
274 func TestSeekEndSingleBlockFile(t *testing.T) {
275 nbytes := int64(100)
276 - ds := mdtest.Mock(t)
276 + ds := mdtest.Mock()
277 nd, _ := getTestDag(t, ds, nbytes, 5000)
278
279 rs, err := uio.NewDagReader(context.Background(), nd, ds)
@@ -292,7 +292,7 @@ func TestSeekEndSingleBlockFile(t *testing.T) {
292
293 func TestSeekingStress(t *testing.T) {
294 nbytes := int64(1024 * 1024)
295 - ds := mdtest.Mock(t)
295 + ds := mdtest.Mock()
296 nd, should := getTestDag(t, ds, nbytes, 1000)
297
298 rs, err := uio.NewDagReader(context.Background(), nd, ds)
@@ -330,7 +330,7 @@ func TestSeekingStress(t *testing.T) {
330
331 func TestSeekingConsistency(t *testing.T) {
332 nbytes := int64(128 * 1024)
333 - ds := mdtest.Mock(t)
333 + ds := mdtest.Mock()
334 nd, should := getTestDag(t, ds, nbytes, 500)
335
336 rs, err := uio.NewDagReader(context.Background(), nd, ds)
importer/importer_test.go
+3 -3
@@ -15,7 +15,7 @@ import (
15 )
16
17 func getBalancedDag(t testing.TB, size int64, blksize int64) (*dag.Node, dag.DAGService) {
18 - ds := mdtest.Mock(t)
18 + ds := mdtest.Mock()
19 r := io.LimitReader(u.NewTimeSeededRand(), size)
20 nd, err := BuildDagFromReader(ds, chunk.NewSizeSplitter(r, blksize), nil)
21 if err != nil {
@@ -25,7 +25,7 @@ func getBalancedDag(t testing.TB, size int64, blksize int64) (*dag.Node, dag.DAG
25 }
26
27 func getTrickleDag(t testing.TB, size int64, blksize int64) (*dag.Node, dag.DAGService) {
28 - ds := mdtest.Mock(t)
28 + ds := mdtest.Mock()
29 r := io.LimitReader(u.NewTimeSeededRand(), size)
30 nd, err := BuildTrickleDagFromReader(ds, chunk.NewSizeSplitter(r, blksize), nil)
31 if err != nil {
@@ -35,7 +35,7 @@ func getTrickleDag(t testing.TB, size int64, blksize int64) (*dag.Node, dag.DAGS
35 }
36
37 func TestBalancedDag(t *testing.T) {
38 - ds := mdtest.Mock(t)
38 + ds := mdtest.Mock()
39 buf := make([]byte, 10000)
40 u.NewTimeSeededRand().Read(buf)
41 r := bytes.NewReader(buf)
importer/trickle/trickle_test.go
+13 -13
@@ -63,7 +63,7 @@ func testFileConsistency(t *testing.T, bs chunk.SplitterGen, nbytes int) {
63 u.NewTimeSeededRand().Read(should)
64
65 read := bytes.NewReader(should)
66 - ds := mdtest.Mock(t)
66 + ds := mdtest.Mock()
67 nd, err := buildTestDag(ds, bs(read))
68 if err != nil {
69 t.Fatal(err)
@@ -90,7 +90,7 @@ func TestBuilderConsistency(t *testing.T) {
90 buf := new(bytes.Buffer)
91 io.CopyN(buf, u.NewTimeSeededRand(), int64(nbytes))
92 should := dup(buf.Bytes())
93 - dagserv := mdtest.Mock(t)
93 + dagserv := mdtest.Mock()
94 nd, err := buildTestDag(dagserv, chunk.DefaultSplitter(buf))
95 if err != nil {
96 t.Fatal(err)
@@ -136,7 +136,7 @@ func TestIndirectBlocks(t *testing.T) {
136
137 read := bytes.NewReader(buf)
138
139 - ds := mdtest.Mock(t)
139 + ds := mdtest.Mock()
140 dag, err := buildTestDag(ds, splitter(read))
141 if err != nil {
142 t.Fatal(err)
@@ -163,7 +163,7 @@ func TestSeekingBasic(t *testing.T) {
163 u.NewTimeSeededRand().Read(should)
164
165 read := bytes.NewReader(should)
166 - ds := mdtest.Mock(t)
166 + ds := mdtest.Mock()
167 nd, err := buildTestDag(ds, chunk.NewSizeSplitter(read, 512))
168 if err != nil {
169 t.Fatal(err)
@@ -200,7 +200,7 @@ func TestSeekToBegin(t *testing.T) {
200 u.NewTimeSeededRand().Read(should)
201
202 read := bytes.NewReader(should)
203 - ds := mdtest.Mock(t)
203 + ds := mdtest.Mock()
204 nd, err := buildTestDag(ds, chunk.NewSizeSplitter(read, 500))
205 if err != nil {
206 t.Fatal(err)
@@ -244,7 +244,7 @@ func TestSeekToAlmostBegin(t *testing.T) {
244 u.NewTimeSeededRand().Read(should)
245
246 read := bytes.NewReader(should)
247 - ds := mdtest.Mock(t)
247 + ds := mdtest.Mock()
248 nd, err := buildTestDag(ds, chunk.NewSizeSplitter(read, 500))
249 if err != nil {
250 t.Fatal(err)
@@ -288,7 +288,7 @@ func TestSeekEnd(t *testing.T) {
288 u.NewTimeSeededRand().Read(should)
289
290 read := bytes.NewReader(should)
291 - ds := mdtest.Mock(t)
291 + ds := mdtest.Mock()
292 nd, err := buildTestDag(ds, chunk.NewSizeSplitter(read, 500))
293 if err != nil {
294 t.Fatal(err)
@@ -314,7 +314,7 @@ func TestSeekEndSingleBlockFile(t *testing.T) {
314 u.NewTimeSeededRand().Read(should)
315
316 read := bytes.NewReader(should)
317 - ds := mdtest.Mock(t)
317 + ds := mdtest.Mock()
318 nd, err := buildTestDag(ds, chunk.NewSizeSplitter(read, 5000))
319 if err != nil {
320 t.Fatal(err)
@@ -340,7 +340,7 @@ func TestSeekingStress(t *testing.T) {
340 u.NewTimeSeededRand().Read(should)
341
342 read := bytes.NewReader(should)
343 - ds := mdtest.Mock(t)
343 + ds := mdtest.Mock()
344 nd, err := buildTestDag(ds, chunk.NewSizeSplitter(read, 1000))
345 if err != nil {
346 t.Fatal(err)
@@ -385,7 +385,7 @@ func TestSeekingConsistency(t *testing.T) {
385 u.NewTimeSeededRand().Read(should)
386
387 read := bytes.NewReader(should)
388 - ds := mdtest.Mock(t)
388 + ds := mdtest.Mock()
389 nd, err := buildTestDag(ds, chunk.NewSizeSplitter(read, 500))
390 if err != nil {
391 t.Fatal(err)
@@ -429,7 +429,7 @@ func TestAppend(t *testing.T) {
429
430 // Reader for half the bytes
431 read := bytes.NewReader(should[:nbytes/2])
432 - ds := mdtest.Mock(t)
432 + ds := mdtest.Mock()
433 nd, err := buildTestDag(ds, chunk.NewSizeSplitter(read, 500))
434 if err != nil {
435 t.Fatal(err)
@@ -471,7 +471,7 @@ func TestAppend(t *testing.T) {
471
472 // This test appends one byte at a time to an empty file
473 func TestMultipleAppends(t *testing.T) {
474 - ds := mdtest.Mock(t)
474 + ds := mdtest.Mock()
475
476 // TODO: fix small size appends and make this number bigger
477 nbytes := int64(1000)
@@ -522,7 +522,7 @@ func TestMultipleAppends(t *testing.T) {
522 }
523
524 func TestAppendSingleBytesToEmpty(t *testing.T) {
525 - ds := mdtest.Mock(t)
525 + ds := mdtest.Mock()
526
527 data := []byte("AB")
528
merkledag/merkledag_test.go
+2 -5
@@ -33,10 +33,7 @@ type dagservAndPinner struct {
33 func getDagservAndPinner(t *testing.T) dagservAndPinner {
34 db := dssync.MutexWrap(ds.NewMapDatastore())
35 bs := bstore.NewBlockstore(db)
36 - blockserv, err := bserv.New(bs, offline.Exchange(bs))
37 - if err != nil {
38 - t.Fatal(err)
39 - }
36 + blockserv := bserv.New(bs, offline.Exchange(bs))
37 dserv := NewDAGService(blockserv)
38 mpin := pin.NewPinner(db, dserv).GetManual()
39 return dagservAndPinner{
@@ -159,7 +156,7 @@ func TestBatchFetchDupBlock(t *testing.T) {
156
157 func runBatchFetchTest(t *testing.T, read io.Reader) {
158 var dagservs []DAGService
162 - for _, bsi := range bstest.Mocks(t, 5) {
159 + for _, bsi := range bstest.Mocks(5) {
160 dagservs = append(dagservs, NewDAGService(bsi))
161 }
162
merkledag/test/utils.go
+2 -7
@@ -1,8 +1,6 @@
1 package mdutils
2
3 import (
4 - "testing"
5 -
4 ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
5 dssync "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
6 "github.com/ipfs/go-ipfs/blocks/blockstore"
@@ -11,11 +9,8 @@ import (
9 dag "github.com/ipfs/go-ipfs/merkledag"
10 )
11
14 -func Mock(t testing.TB) dag.DAGService {
12 +func Mock() dag.DAGService {
13 bstore := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
16 - bserv, err := bsrv.New(bstore, offline.Exchange(bstore))
17 - if err != nil {
18 - t.Fatal(err)
19 - }
14 + bserv := bsrv.New(bstore, offline.Exchange(bstore))
15 return dag.NewDAGService(bserv)
16 }
merkledag/utils/utils_test.go
+2 -2
@@ -12,7 +12,7 @@ import (
12 )
13
14 func TestAddLink(t *testing.T) {
15 - ds := mdtest.Mock(t)
15 + ds := mdtest.Mock()
16 fishnode := &dag.Node{
17 Data: []byte("fishcakes!"),
18 }
@@ -66,7 +66,7 @@ func assertNodeAtPath(t *testing.T, ds dag.DAGService, root *dag.Node, path stri
66 }
67
68 func TestInsertNode(t *testing.T) {
69 - ds := mdtest.Mock(t)
69 + ds := mdtest.Mock()
70 root := new(dag.Node)
71 e := NewDagEditor(ds, root)
72
path/resolver_test.go
+3 -14
@@ -4,15 +4,11 @@ import (
4 "fmt"
5 "testing"
6
7 - datastore "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
8 - sync "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
7 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
8
11 - blockstore "github.com/ipfs/go-ipfs/blocks/blockstore"
9 key "github.com/ipfs/go-ipfs/blocks/key"
13 - blockservice "github.com/ipfs/go-ipfs/blockservice"
14 - offline "github.com/ipfs/go-ipfs/exchange/offline"
10 merkledag "github.com/ipfs/go-ipfs/merkledag"
11 + dagmock "github.com/ipfs/go-ipfs/merkledag/test"
12 path "github.com/ipfs/go-ipfs/path"
13 util "github.com/ipfs/go-ipfs/util"
14 )
@@ -27,20 +23,13 @@ func randNode() (*merkledag.Node, key.Key) {
23
24 func TestRecurivePathResolution(t *testing.T) {
25 ctx := context.Background()
30 - dstore := sync.MutexWrap(datastore.NewMapDatastore())
31 - bstore := blockstore.NewBlockstore(dstore)
32 - bserv, err := blockservice.New(bstore, offline.Exchange(bstore))
33 - if err != nil {
34 - t.Fatal(err)
35 - }
36 -
37 - dagService := merkledag.NewDAGService(bserv)
26 + dagService := dagmock.Mock()
27
28 a, _ := randNode()
29 b, _ := randNode()
30 c, cKey := randNode()
31
43 - err = b.AddNodeLink("grandchild", c)
32 + err := b.AddNodeLink("grandchild", c)
33 if err != nil {
34 t.Fatal(err)
35 }
pin/pin_test.go
+6 -15
@@ -29,10 +29,7 @@ func TestPinnerBasic(t *testing.T) {
29
30 dstore := dssync.MutexWrap(ds.NewMapDatastore())
31 bstore := blockstore.NewBlockstore(dstore)
32 - bserv, err := bs.New(bstore, offline.Exchange(bstore))
33 - if err != nil {
34 - t.Fatal(err)
35 - }
32 + bserv := bs.New(bstore, offline.Exchange(bstore))
33
34 dserv := mdag.NewDAGService(bserv)
35
@@ -40,7 +37,7 @@ func TestPinnerBasic(t *testing.T) {
37 p := NewPinner(dstore, dserv)
38
39 a, ak := randNode()
43 - _, err = dserv.Add(a)
40 + _, err := dserv.Add(a)
41 if err != nil {
42 t.Fatal(err)
43 }
@@ -163,10 +160,7 @@ func TestDuplicateSemantics(t *testing.T) {
160 ctx := context.Background()
161 dstore := dssync.MutexWrap(ds.NewMapDatastore())
162 bstore := blockstore.NewBlockstore(dstore)
166 - bserv, err := bs.New(bstore, offline.Exchange(bstore))
167 - if err != nil {
168 - t.Fatal(err)
169 - }
163 + bserv := bs.New(bstore, offline.Exchange(bstore))
164
165 dserv := mdag.NewDAGService(bserv)
166
@@ -174,7 +168,7 @@ func TestDuplicateSemantics(t *testing.T) {
168 p := NewPinner(dstore, dserv)
169
170 a, _ := randNode()
177 - _, err = dserv.Add(a)
171 + _, err := dserv.Add(a)
172 if err != nil {
173 t.Fatal(err)
174 }
@@ -202,10 +196,7 @@ func TestPinRecursiveFail(t *testing.T) {
196 ctx := context.Background()
197 dstore := dssync.MutexWrap(ds.NewMapDatastore())
198 bstore := blockstore.NewBlockstore(dstore)
205 - bserv, err := bs.New(bstore, offline.Exchange(bstore))
206 - if err != nil {
207 - t.Fatal(err)
208 - }
199 + bserv := bs.New(bstore, offline.Exchange(bstore))
200
201 dserv := mdag.NewDAGService(bserv)
202
@@ -213,7 +204,7 @@ func TestPinRecursiveFail(t *testing.T) {
204
205 a, _ := randNode()
206 b, _ := randNode()
216 - err = a.AddNodeLinkClean("child", b)
207 + err := a.AddNodeLinkClean("child", b)
208 if err != nil {
209 t.Fatal(err)
210 }
unixfs/mod/dagmodifier_test.go
+2 -8
@@ -31,10 +31,7 @@ func getMockDagServ(t testing.TB) (mdag.DAGService, pin.ManualPinner) {
31 dstore := ds.NewMapDatastore()
32 tsds := sync.MutexWrap(dstore)
33 bstore := blockstore.NewBlockstore(tsds)
34 - bserv, err := bs.New(bstore, offline.Exchange(bstore))
35 - if err != nil {
36 - t.Fatal(err)
37 - }
34 + bserv := bs.New(bstore, offline.Exchange(bstore))
35 dserv := mdag.NewDAGService(bserv)
36 return dserv, pin.NewPinner(tsds, dserv).GetManual()
37 }
@@ -43,10 +40,7 @@ func getMockDagServAndBstore(t testing.TB) (mdag.DAGService, blockstore.Blocksto
40 dstore := ds.NewMapDatastore()
41 tsds := sync.MutexWrap(dstore)
42 bstore := blockstore.NewBlockstore(tsds)
46 - bserv, err := bs.New(bstore, offline.Exchange(bstore))
47 - if err != nil {
48 - t.Fatal(err)
49 - }
43 + bserv := bs.New(bstore, offline.Exchange(bstore))
44 dserv := mdag.NewDAGService(bserv)
45 return dserv, bstore, pin.NewPinner(tsds, dserv).GetManual()
46 }