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

import (
	"testing"

6 7 8
	blocks "github.com/ipfs/go-ipfs/blocks"
	"github.com/ipfs/go-ipfs/blocks/blockstore"
	"github.com/ipfs/go-ipfs/blocks/blocksutil"
9
	key "github.com/ipfs/go-ipfs/blocks/key"
Jeromy's avatar
Jeromy committed
10 11
	ds "gx/ipfs/QmNgqJarToRiq2GBaPJhkmW4B5BxS5B74E1rkGvv2JoaTp/go-datastore"
	ds_sync "gx/ipfs/QmNgqJarToRiq2GBaPJhkmW4B5BxS5B74E1rkGvv2JoaTp/go-datastore/sync"
Jeromy's avatar
Jeromy committed
12
	context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
13 14 15
)

func TestBlockReturnsErr(t *testing.T) {
16
	off := Exchange(bstore())
17
	_, err := off.GetBlock(context.Background(), key.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(block)
30
	if err != nil {
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
		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 {
47
		if err := ex.HasBlock(b); err != nil {
48 49
			t.Fail()
		}
50
	}
51

52 53
	request := func() []key.Key {
		var ks []key.Key
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

		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
}