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

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

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

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

Jeromy's avatar
Jeromy committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35
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())
}

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

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

Jeromy's avatar
Jeromy committed
47
	if o.Key() != key.Key(h) {
48 49 50
		t.Error("Block key and data multihash key not equal")
	}

Jeromy's avatar
Jeromy committed
51
	k, err := bs.AddObject(o)
52 53 54 55 56
	if err != nil {
		t.Error("failed to add block to BlockService", err)
		return
	}

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

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

Jeromy's avatar
Jeromy committed
69
	if o.Key() != b2.Key() {
70 71 72
		t.Error("Block keys not equal.")
	}

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

Jeromy's avatar
Jeromy committed
78 79 80 81 82 83 84 85
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
}

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

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

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

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