Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

feat(session): instantiated sessions lazily #27

Merged
merged 1 commit into from
May 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions blockservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,8 @@ func (s *blockService) Exchange() exchange.Interface {
func NewSession(ctx context.Context, bs BlockService) *Session {
exch := bs.Exchange()
if sessEx, ok := exch.(exchange.SessionExchange); ok {
ses := sessEx.NewSession(ctx)
return &Session{
ses: ses,
ses: nil,
sessEx: sessEx,
bs: bs.Blockstore(),
}
Expand Down
59 changes: 59 additions & 0 deletions blockservice_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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/ipfs/go-ipfs-exchange-interface"
offline "github.com/ipfs/go-ipfs-exchange-offline"
)

Expand Down Expand Up @@ -35,6 +37,52 @@ func TestWriteThroughWorks(t *testing.T) {
}
}

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()
bstore.Put(block)

block2 := bgen.Next()
session.HasBlock(block2)

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 {
Expand All @@ -46,3 +94,14 @@ 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(context.Context) exchange.Fetcher {
return fe.session
}