offline_test.go 1.6 KB
Newer Older
Jeromy's avatar
Jeromy committed
1
package offline
2 3 4 5

import (
	"testing"

6 7
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
	ds_sync "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
8
	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
9
	blocks "github.com/jbenet/go-ipfs/blocks"
10 11
	"github.com/jbenet/go-ipfs/blocks/blockstore"
	"github.com/jbenet/go-ipfs/blocks/blocksutil"
12 13 14 15
	u "github.com/jbenet/go-ipfs/util"
)

func TestBlockReturnsErr(t *testing.T) {
16
	off := Exchange(bstore())
Jeromy's avatar
Jeromy committed
17
	_, err := off.GetBlock(context.Background(), u.Key("foo"))
18 19 20 21 22 23 24
	if err != nil {
		return // as desired
	}
	t.Fail()
}

func TestHasBlockReturnsNil(t *testing.T) {
25 26
	store := bstore()
	ex := Exchange(store)
27
	block := blocks.NewBlock([]byte("data"))
28 29

	err := ex.HasBlock(context.Background(), block)
30
	if err != nil {
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
		t.Fail()
	}

	if _, err := store.Get(block.Key()); err != nil {
		t.Fatal(err)
	}
}

func TestGetBlocks(t *testing.T) {
	store := bstore()
	ex := Exchange(store)
	g := blocksutil.NewBlockGenerator()

	expected := g.Blocks(2)

	for _, b := range expected {
		if err := ex.HasBlock(context.Background(), b); err != nil {
			t.Fail()
		}
50
	}
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

	request := func() []u.Key {
		var ks []u.Key

		for _, b := range expected {
			ks = append(ks, b.Key())
		}
		return ks
	}()

	received, err := ex.GetBlocks(context.Background(), request)
	if err != nil {
		t.Fatal(err)
	}

	var count int
	for _ = range received {
		count++
	}
	if len(expected) != count {
		t.Fail()
	}
}

func bstore() blockstore.Blockstore {
	return blockstore.NewBlockstore(ds_sync.MutexWrap(ds.NewMapDatastore()))
77
}