Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-blockservice
Commits
e9103628
Unverified
Commit
e9103628
authored
May 15, 2019
by
Steven Allen
Committed by
GitHub
May 15, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #27 from ipfs/feat/lazy-sessions
feat(session): instantiated sessions lazily
parents
6ca3282a
4b230aa4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
2 deletions
+60
-2
blockservice.go
blockservice.go
+1
-2
blockservice_test.go
blockservice_test.go
+59
-0
No files found.
blockservice.go
View file @
e9103628
...
...
@@ -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
(),
}
...
...
blockservice_test.go
View file @
e9103628
package
blockservice
import
(
"context"
"testing"
blocks
"github.com/ipfs/go-block-format"
...
...
@@ -8,6 +9,7 @@ import (
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"
)
...
...
@@ -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
{
...
...
@@ -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
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment