dagwriter_test.go 3.43 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1
package io_test
2 3 4 5 6 7

import (
	"testing"

	"io"

8
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
9
	bs "github.com/jbenet/go-ipfs/blockservice"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10
	importer "github.com/jbenet/go-ipfs/importer"
11
	chunk "github.com/jbenet/go-ipfs/importer/chunk"
12
	mdag "github.com/jbenet/go-ipfs/merkledag"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13
	dagio "github.com/jbenet/go-ipfs/unixfs/io"
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
)

type datasource struct {
	i int
}

func (d *datasource) Read(b []byte) (int, error) {
	for i, _ := range b {
		b[i] = byte(d.i % 256)
		d.i++
	}
	return len(b), nil
}

func (d *datasource) Matches(t *testing.T, r io.Reader, length int) bool {
	b := make([]byte, 100)
	i := 0
	for {
		n, err := r.Read(b)
		if err != nil && err != io.EOF {
			t.Fatal(err)
		}
		for _, v := range b[:n] {
			if v != byte(i%256) {
				t.Fatalf("Buffers differed at byte: %d (%d != %d)", i, v, (i % 256))
			}
			i++
		}
		if err == io.EOF {
			break
		}
	}
	if i != length {
		t.Fatalf("Incorrect length. (%d != %d)", i, length)
	}
	return true
}

func TestDagWriter(t *testing.T) {
	dstore := ds.NewMapDatastore()
	bserv, err := bs.NewBlockService(dstore, nil)
	if err != nil {
		t.Fatal(err)
	}
58
	dag := mdag.NewDAGService(bserv)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
59
	dw := dagio.NewDagWriter(dag, &chunk.SizeSplitter{Size: 4096})
60 61 62 63 64 65 66 67 68 69 70 71 72 73

	nbytes := int64(1024 * 1024 * 2)
	n, err := io.CopyN(dw, &datasource{}, nbytes)
	if err != nil {
		t.Fatal(err)
	}

	if n != nbytes {
		t.Fatal("Copied incorrect amount of bytes!")
	}

	dw.Close()

	node := dw.GetNode()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
74
	read, err := dagio.NewDagReader(node, dag)
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
	if err != nil {
		t.Fatal(err)
	}

	d := &datasource{}
	if !d.Matches(t, read, int(nbytes)) {
		t.Fatal("Failed to validate!")
	}
}

func TestMassiveWrite(t *testing.T) {
	t.SkipNow()
	dstore := ds.NewNullDatastore()
	bserv, err := bs.NewBlockService(dstore, nil)
	if err != nil {
		t.Fatal(err)
	}
92
	dag := mdag.NewDAGService(bserv)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
93
	dw := dagio.NewDagWriter(dag, &chunk.SizeSplitter{Size: 4096})
94 95 96 97 98 99 100 101 102 103 104

	nbytes := int64(1024 * 1024 * 1024 * 16)
	n, err := io.CopyN(dw, &datasource{}, nbytes)
	if err != nil {
		t.Fatal(err)
	}
	if n != nbytes {
		t.Fatal("Incorrect copy size.")
	}
	dw.Close()
}
Jeromy's avatar
Jeromy committed
105 106 107 108 109 110 111

func BenchmarkDagWriter(b *testing.B) {
	dstore := ds.NewNullDatastore()
	bserv, err := bs.NewBlockService(dstore, nil)
	if err != nil {
		b.Fatal(err)
	}
112
	dag := mdag.NewDAGService(bserv)
Jeromy's avatar
Jeromy committed
113 114

	b.ResetTimer()
115
	nbytes := int64(100000)
Jeromy's avatar
Jeromy committed
116 117
	for i := 0; i < b.N; i++ {
		b.SetBytes(nbytes)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
118
		dw := dagio.NewDagWriter(dag, &chunk.SizeSplitter{Size: 4096})
Jeromy's avatar
Jeromy committed
119 120 121 122 123 124 125 126 127 128 129
		n, err := io.CopyN(dw, &datasource{}, nbytes)
		if err != nil {
			b.Fatal(err)
		}
		if n != nbytes {
			b.Fatal("Incorrect copy size.")
		}
		dw.Close()
	}

}
130 131 132 133 134 135 136 137 138 139 140 141

func TestAgainstImporter(t *testing.T) {
	dstore := ds.NewMapDatastore()
	bserv, err := bs.NewBlockService(dstore, nil)
	if err != nil {
		t.Fatal(err)
	}
	dag := mdag.NewDAGService(bserv)

	nbytes := int64(1024 * 1024 * 2)

	// DagWriter
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
142
	dw := dagio.NewDagWriter(dag, &chunk.SizeSplitter{4096})
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
	n, err := io.CopyN(dw, &datasource{}, nbytes)
	if err != nil {
		t.Fatal(err)
	}
	if n != nbytes {
		t.Fatal("Copied incorrect amount of bytes!")
	}

	dw.Close()
	dwNode := dw.GetNode()
	dwKey, err := dwNode.Key()
	if err != nil {
		t.Fatal(err)
	}

	// DagFromFile
	rl := &io.LimitedReader{&datasource{}, nbytes}

	dffNode, err := importer.NewDagFromReaderWithSplitter(rl, &chunk.SizeSplitter{4096})
	dffKey, err := dffNode.Key()
	if err != nil {
		t.Fatal(err)
	}
	if dwKey.String() != dffKey.String() {
		t.Errorf("\nDagWriter produced     %s\n"+
			"DagFromReader produced %s",
			dwKey, dffKey)
	}
}