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

add a context to HasBlock, pass one to the datastore #30

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 5 additions & 5 deletions offline.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ type offlineExchange struct {
// GetBlock returns nil to signal that a block could not be retrieved for the
// given key.
// NB: This function may return before the timeout expires.
func (e *offlineExchange) GetBlock(_ context.Context, k cid.Cid) (blocks.Block, error) {
return e.bs.Get(k)
func (e *offlineExchange) GetBlock(ctx context.Context, k cid.Cid) (blocks.Block, error) {
return e.bs.Get(ctx, k)
}

// HasBlock always returns nil.
func (e *offlineExchange) HasBlock(b blocks.Block) error {
return e.bs.Put(b)
func (e *offlineExchange) HasBlock(ctx context.Context, b blocks.Block) error {
return e.bs.Put(ctx, b)
}

// Close always returns nil.
Expand All @@ -46,7 +46,7 @@ func (e *offlineExchange) GetBlocks(ctx context.Context, ks []cid.Cid) (<-chan b
defer close(out)
var misses []cid.Cid
for _, k := range ks {
hit, err := e.bs.Get(k)
hit, err := e.bs.Get(ctx, k)
if err != nil {
misses = append(misses, k)
// a long line of misses should abort when context is cancelled.
Expand Down
8 changes: 5 additions & 3 deletions offline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,31 @@ func TestBlockReturnsErr(t *testing.T) {
}

func TestHasBlockReturnsNil(t *testing.T) {
ctx := context.Background()
store := bstore()
ex := Exchange(store)
block := blocks.NewBlock([]byte("data"))

err := ex.HasBlock(block)
err := ex.HasBlock(ctx, block)
if err != nil {
t.Fail()
}

if _, err := store.Get(block.Cid()); err != nil {
if _, err := store.Get(ctx, block.Cid()); err != nil {
t.Fatal(err)
}
}

func TestGetBlocks(t *testing.T) {
ctx := context.Background()
store := bstore()
ex := Exchange(store)
g := blocksutil.NewBlockGenerator()

expected := g.Blocks(2)

for _, b := range expected {
if err := ex.HasBlock(b); err != nil {
if err := ex.HasBlock(ctx, b); err != nil {
t.Fail()
}
}
Expand Down