blocks_test.go 2.52 KB
Newer Older
1
package bstest
2 3 4 5

import (
	"bytes"
	"testing"
Jeromy's avatar
Jeromy committed
6 7
	"time"

8 9 10
	blocks "github.com/ipfs/go-ipfs/blocks"
	blockstore "github.com/ipfs/go-ipfs/blocks/blockstore"
	blocksutil "github.com/ipfs/go-ipfs/blocks/blocksutil"
11
	key "github.com/ipfs/go-ipfs/blocks/key"
12
	. "github.com/ipfs/go-ipfs/blockservice"
13
	offline "github.com/ipfs/go-ipfs/exchange/offline"
Jakub Sztandera's avatar
Jakub Sztandera committed
14 15
	ds "gx/ipfs/QmZ6A6P6AMo8SR3jXAwzTuSU6B9R2Y4eqW2yW9VvfUayDN/go-datastore"
	dssync "gx/ipfs/QmZ6A6P6AMo8SR3jXAwzTuSU6B9R2Y4eqW2yW9VvfUayDN/go-datastore/sync"
16
	u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
Jeromy's avatar
Jeromy committed
17
	"gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
18 19 20
)

func TestBlocks(t *testing.T) {
21
	bstore := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
22
	bs := New(bstore, offline.Exchange(bstore))
23
	defer bs.Close()
24

jbenet's avatar
jbenet committed
25 26 27 28 29
	_, err := bs.GetBlock(context.Background(), key.Key(""))
	if err != ErrNotFound {
		t.Error("Empty String Key should error", err)
	}

Jeromy's avatar
Jeromy committed
30
	b := blocks.NewBlock([]byte("beep boop"))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
31
	h := u.Hash([]byte("beep boop"))
32
	if !bytes.Equal(b.Multihash(), h) {
33 34 35
		t.Error("Block Multihash and data multihash not equal")
	}

36
	if b.Key() != key.Key(h) {
37 38 39 40 41 42 43 44 45 46 47 48 49
		t.Error("Block key and data multihash key not equal")
	}

	k, err := bs.AddBlock(b)
	if err != nil {
		t.Error("failed to add block to BlockService", err)
		return
	}

	if k != b.Key() {
		t.Error("returned key is not equal to block key", err)
	}

50
	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
rht's avatar
rht committed
51
	defer cancel()
Jeromy's avatar
Jeromy committed
52
	b2, err := bs.GetBlock(ctx, b.Key())
53 54 55 56 57 58 59 60 61
	if err != nil {
		t.Error("failed to retrieve block from BlockService", err)
		return
	}

	if b.Key() != b2.Key() {
		t.Error("Block keys not equal.")
	}

62
	if !bytes.Equal(b.Data(), b2.Data()) {
63 64 65
		t.Error("Block data is not equal.")
	}
}
66

67
func TestGetBlocksSequential(t *testing.T) {
68
	var servs = Mocks(4)
69 70 71
	for _, s := range servs {
		defer s.Close()
	}
72
	bg := blocksutil.NewBlockGenerator()
73 74
	blks := bg.Blocks(50)

75
	var keys []key.Key
76 77 78 79 80
	for _, blk := range blks {
		keys = append(keys, blk.Key())
		servs[0].AddBlock(blk)
	}

81
	t.Log("one instance at a time, get blocks concurrently")
Brian Tiger Chow's avatar
Brian Tiger Chow committed
82

83
	for i := 1; i < len(servs); i++ {
84
		ctx, cancel := context.WithTimeout(context.Background(), time.Second*50)
rht's avatar
rht committed
85
		defer cancel()
86
		out := servs[i].GetBlocks(ctx, keys)
87
		gotten := make(map[key.Key]blocks.Block)
88 89 90 91 92 93 94 95 96 97
		for blk := range out {
			if _, ok := gotten[blk.Key()]; ok {
				t.Fatal("Got duplicate block!")
			}
			gotten[blk.Key()] = blk
		}
		if len(gotten) != len(blks) {
			t.Fatalf("Didnt get enough blocks back: %d/%d", len(gotten), len(blks))
		}
	}
98
}