blocks_test.go 2.8 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"
George Antoniadis's avatar
George Antoniadis committed
13
	key "gx/ipfs/Qmce4Y4zg3sYr7xKM5UueS67vhNni6EeWgCRnb7MbLJMew/go-key"
Jeromy's avatar
Jeromy committed
14

15
	u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
Jeromy's avatar
Jeromy committed
16
	"gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
George Antoniadis's avatar
George Antoniadis committed
17 18
	ds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore"
	dssync "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore/sync"
Jeromy's avatar
Jeromy committed
19
	cid "gx/ipfs/QmfSc2xehWmWLnwwYR91Y8QF4xdASypTFVknutoKQS3GHp/go-cid"
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
}