forked from ETHFSx/go-ipfs-exchange-offline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
offline_test.go
79 lines (65 loc) · 1.45 KB
/
offline_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package offline
import (
"context"
"testing"
blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
ds "github.com/ipfs/go-datastore"
ds_sync "github.com/ipfs/go-datastore/sync"
blockstore "github.com/ipfs/go-ipfs-blockstore"
blocksutil "github.com/ipfs/go-ipfs-blocksutil"
u "github.com/ipfs/go-ipfs-util"
)
func TestBlockReturnsErr(t *testing.T) {
off := Exchange(bstore())
c := cid.NewCidV0(u.Hash([]byte("foo")))
_, err := off.GetBlock(context.Background(), c)
if err != nil {
return // as desired
}
t.Fail()
}
func TestHasBlockReturnsNil(t *testing.T) {
store := bstore()
ex := Exchange(store)
block := blocks.NewBlock([]byte("data"))
err := ex.HasBlock(block)
if err != nil {
t.Fail()
}
if _, err := store.Get(block.Cid()); err != nil {
t.Fatal(err)
}
}
func TestGetBlocks(t *testing.T) {
store := bstore()
ex := Exchange(store)
g := blocksutil.NewBlockGenerator()
expected := g.Blocks(2)
for _, b := range expected {
if err := ex.HasBlock(b); err != nil {
t.Fail()
}
}
request := func() []cid.Cid {
var ks []cid.Cid
for _, b := range expected {
ks = append(ks, b.Cid())
}
return ks
}()
received, err := ex.GetBlocks(context.Background(), request)
if err != nil {
t.Fatal(err)
}
var count int
for range received {
count++
}
if len(expected) != count {
t.Fail()
}
}
func bstore() blockstore.Blockstore {
return blockstore.NewBlockstore(ds_sync.MutexWrap(ds.NewMapDatastore()))
}