From 770cdebf7b77c1228977a0a9c6082773c2da0707 Mon Sep 17 00:00:00 2001 From: Brian Tiger Chow Date: Mon, 15 Sep 2014 04:46:31 -0700 Subject: [PATCH] feat(bitswap) impl offline exchange --- bitswap/offline.go | 30 ++++++++++++++++++++++++++++++ bitswap/offline_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 bitswap/offline.go create mode 100644 bitswap/offline_test.go diff --git a/bitswap/offline.go b/bitswap/offline.go new file mode 100644 index 000000000..ce889ab26 --- /dev/null +++ b/bitswap/offline.go @@ -0,0 +1,30 @@ +package bitswap + +import ( + "errors" + "time" + + blocks "github.com/jbenet/go-ipfs/blocks" + u "github.com/jbenet/go-ipfs/util" +) + +func NewOfflineExchange() Exchange { + return &offlineExchange{} +} + +// offlineExchange implements the Exchange interface but doesn't return blocks. +// For use in offline mode. +type offlineExchange struct { +} + +// Block 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 (_ *offlineExchange) Block(k u.Key, timeout time.Duration) (*blocks.Block, error) { + return nil, errors.New("Block unavailable. Operating in offline mode") +} + +// HasBlock always returns nil. +func (_ *offlineExchange) HasBlock(*blocks.Block) error { + return nil +} diff --git a/bitswap/offline_test.go b/bitswap/offline_test.go new file mode 100644 index 000000000..c3d641d0a --- /dev/null +++ b/bitswap/offline_test.go @@ -0,0 +1,27 @@ +package bitswap + +import ( + "testing" + "time" + + u "github.com/jbenet/go-ipfs/util" + testutil "github.com/jbenet/go-ipfs/util/testutil" +) + +func TestBlockReturnsErr(t *testing.T) { + off := NewOfflineExchange() + _, err := off.Block(u.Key("foo"), time.Second) + if err != nil { + return // as desired + } + t.Fail() +} + +func TestHasBlockReturnsNil(t *testing.T) { + off := NewOfflineExchange() + block := testutil.NewBlockOrFail(t, "data") + err := off.HasBlock(&block) + if err != nil { + t.Fatal("") + } +}