dagwriter_test.go 3.34 KB
Newer Older
1
package io
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"
10
	"github.com/jbenet/go-ipfs/importer"
11
	chunk "github.com/jbenet/go-ipfs/importer/chunk"
12 13 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
	mdag "github.com/jbenet/go-ipfs/merkledag"
)

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)
	}
57
	dag := mdag.NewDAGService(bserv)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
58
	dw := NewDagWriter(dag, &chunk.SizeSplitter{Size: 4096})
59 60 61 62 63 64 65 66 67 68 69 70 71 72

	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()
73
	read, err := NewDagReader(node, dag)
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
	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)
	}
91
	dag := mdag.NewDAGService(bserv)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
92
	dw := NewDagWriter(dag, &chunk.SizeSplitter{Size: 4096})
93 94 95 96 97 98 99 100 101 102 103

	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
104 105 106 107 108 109 110

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

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

}
129 130 131 132 133 134 135 136 137 138 139 140 141 142 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

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
	dw := NewDagWriter(dag, &chunk.SizeSplitter{4096})
	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)
	}
}