@@ -12,16 +12,18 @@ import (
12
12
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
13
13
chunk "github.com/ipfs/go-ipfs/importer/chunk"
14
14
h "github.com/ipfs/go-ipfs/importer/helpers"
15
- merkledag "github.com/ipfs/go-ipfs/merkledag"
15
+ dag "github.com/ipfs/go-ipfs/merkledag"
16
16
mdtest "github.com/ipfs/go-ipfs/merkledag/test"
17
17
pin "github.com/ipfs/go-ipfs/pin"
18
18
uio "github.com/ipfs/go-ipfs/unixfs/io"
19
19
u "github.com/ipfs/go-ipfs/util"
20
20
)
21
21
22
- func buildTestDag (r io.Reader , ds merkledag.DAGService , spl chunk.BlockSplitter ) (* merkledag.Node , error ) {
22
+ // TODO: extract these tests and more as a generic layout test suite
23
+
24
+ func buildTestDag (ds dag.DAGService , spl chunk.Splitter ) (* dag.Node , error ) {
23
25
// Start the splitter
24
- blkch , errs := spl . Split ( r )
26
+ blkch , errs := chunk . Chan ( spl )
25
27
26
28
dbp := h.DagBuilderParams {
27
29
Dagserv : ds ,
@@ -31,14 +33,28 @@ func buildTestDag(r io.Reader, ds merkledag.DAGService, spl chunk.BlockSplitter)
31
33
return BalancedLayout (dbp .New (blkch , errs ))
32
34
}
33
35
36
+ func getTestDag (t * testing.T , ds dag.DAGService , size int64 , blksize int64 ) (* dag.Node , []byte ) {
37
+ data := make ([]byte , size )
38
+ u .NewTimeSeededRand ().Read (data )
39
+ r := bytes .NewReader (data )
40
+
41
+ nd , err := buildTestDag (ds , chunk .NewSizeSplitter (r , blksize ))
42
+ if err != nil {
43
+ t .Fatal (err )
44
+ }
45
+
46
+ return nd , data
47
+ }
48
+
34
49
//Test where calls to read are smaller than the chunk size
35
50
func TestSizeBasedSplit (t * testing.T ) {
36
51
if testing .Short () {
37
52
t .SkipNow ()
38
53
}
39
- bs := & chunk.SizeSplitter {Size : 512 }
54
+
55
+ bs := chunk .SizeSplitterGen (512 )
40
56
testFileConsistency (t , bs , 32 * 512 )
41
- bs = & chunk.SizeSplitter { Size : 4096 }
57
+ bs = chunk .SizeSplitterGen ( 4096 )
42
58
testFileConsistency (t , bs , 32 * 4096 )
43
59
44
60
// Uneven offset
@@ -51,13 +67,13 @@ func dup(b []byte) []byte {
51
67
return o
52
68
}
53
69
54
- func testFileConsistency (t * testing.T , bs chunk.BlockSplitter , nbytes int ) {
70
+ func testFileConsistency (t * testing.T , bs chunk.SplitterGen , nbytes int ) {
55
71
should := make ([]byte , nbytes )
56
72
u .NewTimeSeededRand ().Read (should )
57
73
58
74
read := bytes .NewReader (should )
59
75
ds := mdtest .Mock (t )
60
- nd , err := buildTestDag (read , ds , bs )
76
+ nd , err := buildTestDag (ds , bs ( read ) )
61
77
if err != nil {
62
78
t .Fatal (err )
63
79
}
@@ -79,15 +95,9 @@ func testFileConsistency(t *testing.T, bs chunk.BlockSplitter, nbytes int) {
79
95
}
80
96
81
97
func TestBuilderConsistency (t * testing.T ) {
82
- nbytes := 100000
83
- buf := new (bytes.Buffer )
84
- io .CopyN (buf , u .NewTimeSeededRand (), int64 (nbytes ))
85
- should := dup (buf .Bytes ())
86
98
dagserv := mdtest .Mock (t )
87
- nd , err := buildTestDag (buf , dagserv , chunk .DefaultSplitter )
88
- if err != nil {
89
- t .Fatal (err )
90
- }
99
+ nd , should := getTestDag (t , dagserv , 100000 , chunk .DefaultBlockSize )
100
+
91
101
r , err := uio .NewDagReader (context .Background (), nd , dagserv )
92
102
if err != nil {
93
103
t .Fatal (err )
@@ -117,23 +127,13 @@ func arrComp(a, b []byte) error {
117
127
}
118
128
119
129
type dagservAndPinner struct {
120
- ds merkledag .DAGService
130
+ ds dag .DAGService
121
131
mp pin.ManualPinner
122
132
}
123
133
124
134
func TestIndirectBlocks (t * testing.T ) {
125
- splitter := & chunk.SizeSplitter {512 }
126
- nbytes := 1024 * 1024
127
- buf := make ([]byte , nbytes )
128
- u .NewTimeSeededRand ().Read (buf )
129
-
130
- read := bytes .NewReader (buf )
131
-
132
135
ds := mdtest .Mock (t )
133
- dag , err := buildTestDag (read , ds , splitter )
134
- if err != nil {
135
- t .Fatal (err )
136
- }
136
+ dag , buf := getTestDag (t , ds , 1024 * 1024 , 512 )
137
137
138
138
reader , err := uio .NewDagReader (context .Background (), dag , ds )
139
139
if err != nil {
@@ -152,15 +152,8 @@ func TestIndirectBlocks(t *testing.T) {
152
152
153
153
func TestSeekingBasic (t * testing.T ) {
154
154
nbytes := int64 (10 * 1024 )
155
- should := make ([]byte , nbytes )
156
- u .NewTimeSeededRand ().Read (should )
157
-
158
- read := bytes .NewReader (should )
159
155
ds := mdtest .Mock (t )
160
- nd , err := buildTestDag (read , ds , & chunk.SizeSplitter {500 })
161
- if err != nil {
162
- t .Fatal (err )
163
- }
156
+ nd , should := getTestDag (t , ds , nbytes , 500 )
164
157
165
158
rs , err := uio .NewDagReader (context .Background (), nd , ds )
166
159
if err != nil {
@@ -188,16 +181,8 @@ func TestSeekingBasic(t *testing.T) {
188
181
}
189
182
190
183
func TestSeekToBegin (t * testing.T ) {
191
- nbytes := int64 (10 * 1024 )
192
- should := make ([]byte , nbytes )
193
- u .NewTimeSeededRand ().Read (should )
194
-
195
- read := bytes .NewReader (should )
196
184
ds := mdtest .Mock (t )
197
- nd , err := buildTestDag (read , ds , & chunk.SizeSplitter {500 })
198
- if err != nil {
199
- t .Fatal (err )
200
- }
185
+ nd , should := getTestDag (t , ds , 10 * 1024 , 500 )
201
186
202
187
rs , err := uio .NewDagReader (context .Background (), nd , ds )
203
188
if err != nil {
@@ -232,16 +217,8 @@ func TestSeekToBegin(t *testing.T) {
232
217
}
233
218
234
219
func TestSeekToAlmostBegin (t * testing.T ) {
235
- nbytes := int64 (10 * 1024 )
236
- should := make ([]byte , nbytes )
237
- u .NewTimeSeededRand ().Read (should )
238
-
239
- read := bytes .NewReader (should )
240
220
ds := mdtest .Mock (t )
241
- nd , err := buildTestDag (read , ds , & chunk.SizeSplitter {500 })
242
- if err != nil {
243
- t .Fatal (err )
244
- }
221
+ nd , should := getTestDag (t , ds , 10 * 1024 , 500 )
245
222
246
223
rs , err := uio .NewDagReader (context .Background (), nd , ds )
247
224
if err != nil {
@@ -277,15 +254,8 @@ func TestSeekToAlmostBegin(t *testing.T) {
277
254
278
255
func TestSeekEnd (t * testing.T ) {
279
256
nbytes := int64 (50 * 1024 )
280
- should := make ([]byte , nbytes )
281
- u .NewTimeSeededRand ().Read (should )
282
-
283
- read := bytes .NewReader (should )
284
257
ds := mdtest .Mock (t )
285
- nd , err := buildTestDag (read , ds , & chunk.SizeSplitter {500 })
286
- if err != nil {
287
- t .Fatal (err )
288
- }
258
+ nd , _ := getTestDag (t , ds , nbytes , 500 )
289
259
290
260
rs , err := uio .NewDagReader (context .Background (), nd , ds )
291
261
if err != nil {
@@ -303,15 +273,8 @@ func TestSeekEnd(t *testing.T) {
303
273
304
274
func TestSeekEndSingleBlockFile (t * testing.T ) {
305
275
nbytes := int64 (100 )
306
- should := make ([]byte , nbytes )
307
- u .NewTimeSeededRand ().Read (should )
308
-
309
- read := bytes .NewReader (should )
310
276
ds := mdtest .Mock (t )
311
- nd , err := buildTestDag (read , ds , & chunk.SizeSplitter {5000 })
312
- if err != nil {
313
- t .Fatal (err )
314
- }
277
+ nd , _ := getTestDag (t , ds , nbytes , 5000 )
315
278
316
279
rs , err := uio .NewDagReader (context .Background (), nd , ds )
317
280
if err != nil {
@@ -329,15 +292,8 @@ func TestSeekEndSingleBlockFile(t *testing.T) {
329
292
330
293
func TestSeekingStress (t * testing.T ) {
331
294
nbytes := int64 (1024 * 1024 )
332
- should := make ([]byte , nbytes )
333
- u .NewTimeSeededRand ().Read (should )
334
-
335
- read := bytes .NewReader (should )
336
295
ds := mdtest .Mock (t )
337
- nd , err := buildTestDag (read , ds , & chunk.SizeSplitter {1000 })
338
- if err != nil {
339
- t .Fatal (err )
340
- }
296
+ nd , should := getTestDag (t , ds , nbytes , 1000 )
341
297
342
298
rs , err := uio .NewDagReader (context .Background (), nd , ds )
343
299
if err != nil {
@@ -374,15 +330,8 @@ func TestSeekingStress(t *testing.T) {
374
330
375
331
func TestSeekingConsistency (t * testing.T ) {
376
332
nbytes := int64 (128 * 1024 )
377
- should := make ([]byte , nbytes )
378
- u .NewTimeSeededRand ().Read (should )
379
-
380
- read := bytes .NewReader (should )
381
333
ds := mdtest .Mock (t )
382
- nd , err := buildTestDag (read , ds , & chunk.SizeSplitter {500 })
383
- if err != nil {
384
- t .Fatal (err )
385
- }
334
+ nd , should := getTestDag (t , ds , nbytes , 500 )
386
335
387
336
rs , err := uio .NewDagReader (context .Background (), nd , ds )
388
337
if err != nil {
0 commit comments