forked from ipfs/go-blockservice
-
Notifications
You must be signed in to change notification settings - Fork 1
/
blockservice_test.go
121 lines (105 loc) · 3.16 KB
/
blockservice_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package blockservice
import (
"context"
"testing"
blocks "github.com/ipfs/go-block-format"
ds "github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
blockstore "github.com/ipfs/go-ipfs-blockstore"
butil "github.com/ipfs/go-ipfs-blocksutil"
exchange "github.com/ETHFSx/go-ipfs-exchange-interface"
offline "github.com/ETHFSx/go-ipfs-exchange-offline"
)
func TestWriteThroughWorks(t *testing.T) {
bstore := &PutCountingBlockstore{
blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore())),
0,
}
bstore2 := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
exch := offline.Exchange(bstore2)
bserv := NewWriteThrough(bstore, exch)
bgen := butil.NewBlockGenerator()
block := bgen.Next()
t.Logf("PutCounter: %d", bstore.PutCounter)
err := bserv.AddBlock(block)
if err != nil {
t.Fatal(err)
}
if bstore.PutCounter != 1 {
t.Fatalf("expected just one Put call, have: %d", bstore.PutCounter)
}
err = bserv.AddBlock(block)
if err != nil {
t.Fatal(err)
}
if bstore.PutCounter != 2 {
t.Fatalf("Put should have called again, should be 2 is: %d", bstore.PutCounter)
}
}
func TestLazySessionInitialization(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
bstore := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
bstore2 := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
bstore3 := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
session := offline.Exchange(bstore2)
exchange := offline.Exchange(bstore3)
sessionExch := &fakeSessionExchange{Interface: exchange, session: session}
bservSessEx := NewWriteThrough(bstore, sessionExch)
bgen := butil.NewBlockGenerator()
block := bgen.Next()
err := bstore.Put(block)
if err != nil {
t.Fatal(err)
}
block2 := bgen.Next()
err = session.HasBlock(block2)
if err != nil {
t.Fatal(err)
}
bsession := NewSession(ctx, bservSessEx)
if bsession.ses != nil {
t.Fatal("Session exchange should not instantiated session immediately")
}
returnedBlock, err := bsession.GetBlock(ctx, block.Cid())
if err != nil {
t.Fatal("Should have fetched block locally")
}
if returnedBlock.Cid() != block.Cid() {
t.Fatal("Got incorrect block")
}
if bsession.ses != nil {
t.Fatal("Session exchange should not instantiated session if local store had block")
}
returnedBlock, err = bsession.GetBlock(ctx, block2.Cid())
if err != nil {
t.Fatal("Should have fetched block remotely")
}
if returnedBlock.Cid() != block2.Cid() {
t.Fatal("Got incorrect block")
}
if bsession.ses != session {
t.Fatal("Should have initialized session to fetch block")
}
}
var _ blockstore.Blockstore = (*PutCountingBlockstore)(nil)
type PutCountingBlockstore struct {
blockstore.Blockstore
PutCounter int
}
func (bs *PutCountingBlockstore) Put(block blocks.Block) error {
bs.PutCounter++
return bs.Blockstore.Put(block)
}
var _ exchange.SessionExchange = (*fakeSessionExchange)(nil)
type fakeSessionExchange struct {
exchange.Interface
session exchange.Fetcher
}
func (fe *fakeSessionExchange) NewSession(ctx context.Context) exchange.Fetcher {
if ctx == nil {
panic("nil context")
}
return fe.session
}