feat(bitswap) impl offline exchange
Brian Tiger Chow committed
Sep 15, 2014 at 04:46 UTC
770cdebf7b77c1228977a0a9c6082773c2da0707
2 files changed
+57
bitswap/offline.go
new
+30
@@ -0,0 +1,30 @@
1
+package bitswap
2
+
3
+import (
4
+ "errors"
5
+ "time"
6
+
7
+ blocks "github.com/jbenet/go-ipfs/blocks"
8
+ u "github.com/jbenet/go-ipfs/util"
9
+)
10
+
11
+func NewOfflineExchange() Exchange {
12
+ return &offlineExchange{}
13
+}
14
+
15
+// offlineExchange implements the Exchange interface but doesn't return blocks.
16
+// For use in offline mode.
17
+type offlineExchange struct {
18
+}
19
+
20
+// Block returns nil to signal that a block could not be retrieved for the
21
+// given key.
22
+// NB: This function may return before the timeout expires.
23
+func (_ *offlineExchange) Block(k u.Key, timeout time.Duration) (*blocks.Block, error) {
24
+ return nil, errors.New("Block unavailable. Operating in offline mode")
25
+}
26
+
27
+// HasBlock always returns nil.
28
+func (_ *offlineExchange) HasBlock(*blocks.Block) error {
29
+ return nil
30
+}
bitswap/offline_test.go
new
+27
@@ -0,0 +1,27 @@
1
+package bitswap
2
+
3
+import (
4
+ "testing"
5
+ "time"
6
+
7
+ u "github.com/jbenet/go-ipfs/util"
8
+ testutil "github.com/jbenet/go-ipfs/util/testutil"
9
+)
10
+
11
+func TestBlockReturnsErr(t *testing.T) {
12
+ off := NewOfflineExchange()
13
+ _, err := off.Block(u.Key("foo"), time.Second)
14
+ if err != nil {
15
+ return // as desired
16
+ }
17
+ t.Fail()
18
+}
19
+
20
+func TestHasBlockReturnsNil(t *testing.T) {
21
+ off := NewOfflineExchange()
22
+ block := testutil.NewBlockOrFail(t, "data")
23
+ err := off.HasBlock(&block)
24
+ if err != nil {
25
+ t.Fatal("")
26
+ }
27
+}