Commit 770cdebf authored by Brian Tiger Chow's avatar Brian Tiger Chow

feat(bitswap) impl offline exchange

parent adf62a39
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
}
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("")
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment