Skip to content

Commit 9342cb8

Browse files
authored
Merge pull request ipfs/go-blockservice#27 from ipfs/feat/lazy-sessions
feat(session): instantiated sessions lazily This commit was moved from ipfs/go-blockservice@e910362
2 parents 10d148a + 229d336 commit 9342cb8

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

blockservice/blockservice.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,8 @@ func (s *blockService) Exchange() exchange.Interface {
114114
func NewSession(ctx context.Context, bs BlockService) *Session {
115115
exch := bs.Exchange()
116116
if sessEx, ok := exch.(exchange.SessionExchange); ok {
117-
ses := sessEx.NewSession(ctx)
118117
return &Session{
119-
ses: ses,
118+
ses: nil,
120119
sessEx: sessEx,
121120
bs: bs.Blockstore(),
122121
}

blockservice/blockservice_test.go

+59
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package blockservice
22

33
import (
4+
"context"
45
"testing"
56

67
blocks "github.com/ipfs/go-block-format"
78
ds "github.com/ipfs/go-datastore"
89
dssync "github.com/ipfs/go-datastore/sync"
910
blockstore "github.com/ipfs/go-ipfs-blockstore"
1011
butil "github.com/ipfs/go-ipfs-blocksutil"
12+
exchange "github.com/ipfs/go-ipfs-exchange-interface"
1113
offline "github.com/ipfs/go-ipfs-exchange-offline"
1214
)
1315

@@ -35,6 +37,52 @@ func TestWriteThroughWorks(t *testing.T) {
3537
}
3638
}
3739

40+
func TestLazySessionInitialization(t *testing.T) {
41+
ctx := context.Background()
42+
ctx, cancel := context.WithCancel(ctx)
43+
defer cancel()
44+
45+
bstore := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
46+
bstore2 := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
47+
bstore3 := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
48+
session := offline.Exchange(bstore2)
49+
exchange := offline.Exchange(bstore3)
50+
sessionExch := &fakeSessionExchange{Interface: exchange, session: session}
51+
bservSessEx := NewWriteThrough(bstore, sessionExch)
52+
bgen := butil.NewBlockGenerator()
53+
54+
block := bgen.Next()
55+
bstore.Put(block)
56+
57+
block2 := bgen.Next()
58+
session.HasBlock(block2)
59+
60+
bsession := NewSession(ctx, bservSessEx)
61+
if bsession.ses != nil {
62+
t.Fatal("Session exchange should not instantiated session immediately")
63+
}
64+
returnedBlock, err := bsession.GetBlock(ctx, block.Cid())
65+
if err != nil {
66+
t.Fatal("Should have fetched block locally")
67+
}
68+
if returnedBlock.Cid() != block.Cid() {
69+
t.Fatal("Got incorrect block")
70+
}
71+
if bsession.ses != nil {
72+
t.Fatal("Session exchange should not instantiated session if local store had block")
73+
}
74+
returnedBlock, err = bsession.GetBlock(ctx, block2.Cid())
75+
if err != nil {
76+
t.Fatal("Should have fetched block remotely")
77+
}
78+
if returnedBlock.Cid() != block2.Cid() {
79+
t.Fatal("Got incorrect block")
80+
}
81+
if bsession.ses != session {
82+
t.Fatal("Should have initialized session to fetch block")
83+
}
84+
}
85+
3886
var _ blockstore.Blockstore = (*PutCountingBlockstore)(nil)
3987

4088
type PutCountingBlockstore struct {
@@ -46,3 +94,14 @@ func (bs *PutCountingBlockstore) Put(block blocks.Block) error {
4694
bs.PutCounter++
4795
return bs.Blockstore.Put(block)
4896
}
97+
98+
var _ exchange.SessionExchange = (*fakeSessionExchange)(nil)
99+
100+
type fakeSessionExchange struct {
101+
exchange.Interface
102+
session exchange.Fetcher
103+
}
104+
105+
func (fe *fakeSessionExchange) NewSession(context.Context) exchange.Fetcher {
106+
return fe.session
107+
}

0 commit comments

Comments
 (0)