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

import (
	"bytes"
5
	"context"
Jeromy's avatar
Jeromy committed
6
	"fmt"
7
	"testing"
Jeromy's avatar
Jeromy committed
8 9
	"time"

10 11
	blocks "github.com/ipfs/go-ipfs/blocks"
	blockstore "github.com/ipfs/go-ipfs/blocks/blockstore"
12
	. "github.com/ipfs/go-ipfs/blockservice"
13
	offline "github.com/ipfs/go-ipfs/exchange/offline"
Jeromy's avatar
Jeromy committed
14

15
	cid "gx/ipfs/QmXUuRadqDq5BuFWzVU6VuKaSjTcNm1gNCtLvvP1TJCW4z/go-cid"
16
	u "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
George Antoniadis's avatar
George Antoniadis committed
17 18
	ds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore"
	dssync "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore/sync"
19 20
)

Jeromy's avatar
Jeromy committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34
func newObject(data []byte) *testObject {
	return &testObject{
		Block: blocks.NewBlock(data),
	}
}

type testObject struct {
	blocks.Block
}

func (o *testObject) Cid() *cid.Cid {
	return cid.NewCidV0(o.Block.Multihash())
}

35
func TestBlocks(t *testing.T) {
36
	bstore := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
37
	bs := New(bstore, offline.Exchange(bstore))
38
	defer bs.Close()
39

Jeromy's avatar
Jeromy committed
40
	o := newObject([]byte("beep boop"))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
41
	h := u.Hash([]byte("beep boop"))
Jeromy's avatar
Jeromy committed
42
	if !bytes.Equal(o.Multihash(), h) {
43 44 45
		t.Error("Block Multihash and data multihash not equal")
	}

46
	if !o.Cid().Equals(cid.NewCidV0(h)) {
47 48 49
		t.Error("Block key and data multihash key not equal")
	}

50
	k, err := bs.AddBlock(o)
51 52 53 54 55
	if err != nil {
		t.Error("failed to add block to BlockService", err)
		return
	}

Jeromy's avatar
Jeromy committed
56
	if !k.Equals(o.Cid()) {
57 58 59
		t.Error("returned key is not equal to block key", err)
	}

60
	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
rht's avatar
rht committed
61
	defer cancel()
Jeromy's avatar
Jeromy committed
62
	b2, err := bs.GetBlock(ctx, o.Cid())
63 64 65 66 67
	if err != nil {
		t.Error("failed to retrieve block from BlockService", err)
		return
	}

68
	if !o.Cid().Equals(b2.Cid()) {
69 70 71
		t.Error("Block keys not equal.")
	}

Jeromy's avatar
Jeromy committed
72
	if !bytes.Equal(o.RawData(), b2.RawData()) {
73 74 75
		t.Error("Block data is not equal.")
	}
}
76

Jeromy's avatar
Jeromy committed
77 78 79 80 81 82 83 84
func makeObjects(n int) []*testObject {
	var out []*testObject
	for i := 0; i < n; i++ {
		out = append(out, newObject([]byte(fmt.Sprintf("object %d", i))))
	}
	return out
}

85
func TestGetBlocksSequential(t *testing.T) {
86
	var servs = Mocks(4)
87 88 89
	for _, s := range servs {
		defer s.Close()
	}
Jeromy's avatar
Jeromy committed
90
	objs := makeObjects(50)
91

Jeromy's avatar
Jeromy committed
92 93 94
	var cids []*cid.Cid
	for _, o := range objs {
		cids = append(cids, o.Cid())
95
		servs[0].AddBlock(o)
96 97
	}

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

100
	for i := 1; i < len(servs); i++ {
101
		ctx, cancel := context.WithTimeout(context.Background(), time.Second*50)
rht's avatar
rht committed
102
		defer cancel()
Jeromy's avatar
Jeromy committed
103
		out := servs[i].GetBlocks(ctx, cids)
104
		gotten := make(map[string]blocks.Block)
105
		for blk := range out {
106
			if _, ok := gotten[blk.Cid().KeyString()]; ok {
107 108
				t.Fatal("Got duplicate block!")
			}
109
			gotten[blk.Cid().KeyString()] = blk
110
		}
Jeromy's avatar
Jeromy committed
111 112
		if len(gotten) != len(objs) {
			t.Fatalf("Didnt get enough blocks back: %d/%d", len(gotten), len(objs))
113 114
		}
	}
115
}