blocks_test.go 2.5 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
	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
	blocks "gx/ipfs/QmSn9Td7xgxm9EV7iEjTckpUWmWApggzPxu7eFGWkkpwin/go-block-format"
Jeromy's avatar
Jeromy committed
14

15
	cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid"
16
	u "gx/ipfs/QmSU6eubNdhXjFBJBSksTp8kv8YRub8mGAPv8tVJHmL2EU/go-ipfs-util"
17 18
	ds "gx/ipfs/QmdHG8MAuARdGHxx4rPQASLcvhz24fzjSQq7AJRAQEorq5/go-datastore"
	dssync "gx/ipfs/QmdHG8MAuARdGHxx4rPQASLcvhz24fzjSQq7AJRAQEorq5/go-datastore/sync"
19 20
)

21 22
func newObject(data []byte) blocks.Block {
	return blocks.NewBlock(data)
Jeromy's avatar
Jeromy committed
23 24
}

25
func TestBlocks(t *testing.T) {
26
	bstore := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
27
	bs := New(bstore, offline.Exchange(bstore))
28
	defer bs.Close()
29

Jeromy's avatar
Jeromy committed
30
	o := newObject([]byte("beep boop"))
31 32
	h := cid.NewCidV0(u.Hash([]byte("beep boop")))
	if !o.Cid().Equals(h) {
33 34 35
		t.Error("Block key and data multihash key not equal")
	}

36
	k, err := bs.AddBlock(o)
37 38 39 40 41
	if err != nil {
		t.Error("failed to add block to BlockService", err)
		return
	}

Jeromy's avatar
Jeromy committed
42
	if !k.Equals(o.Cid()) {
43 44 45
		t.Error("returned key is not equal to block key", err)
	}

46
	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
rht's avatar
rht committed
47
	defer cancel()
Jeromy's avatar
Jeromy committed
48
	b2, err := bs.GetBlock(ctx, o.Cid())
49 50 51 52 53
	if err != nil {
		t.Error("failed to retrieve block from BlockService", err)
		return
	}

54
	if !o.Cid().Equals(b2.Cid()) {
55 56 57
		t.Error("Block keys not equal.")
	}

Jeromy's avatar
Jeromy committed
58
	if !bytes.Equal(o.RawData(), b2.RawData()) {
59 60 61
		t.Error("Block data is not equal.")
	}
}
62

63 64
func makeObjects(n int) []blocks.Block {
	var out []blocks.Block
Jeromy's avatar
Jeromy committed
65 66 67 68 69 70
	for i := 0; i < n; i++ {
		out = append(out, newObject([]byte(fmt.Sprintf("object %d", i))))
	}
	return out
}

71
func TestGetBlocksSequential(t *testing.T) {
72
	var servs = Mocks(4)
73 74 75
	for _, s := range servs {
		defer s.Close()
	}
Jeromy's avatar
Jeromy committed
76
	objs := makeObjects(50)
77

Jeromy's avatar
Jeromy committed
78 79 80
	var cids []*cid.Cid
	for _, o := range objs {
		cids = append(cids, o.Cid())
81
		servs[0].AddBlock(o)
82 83
	}

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

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