add test for reprovider and slight refactor
Jeromy committed
Jan 14, 2015 at 22:14 UTC
a7650b259ddf52e6cb22aa1e499b2324e0c04285
3 files changed
+56
-5
core/core.go
+2
-1
@@ -2,6 +2,7 @@ package core
2
3
import (
4
"fmt"
5
+ "time"
6
7
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
8
b58 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
@@ -252,7 +253,7 @@ func (n *IpfsNode) StartOnlineServices() error {
253
254
// Start up reprovider system
255
n.Reprovider = rp.NewReprovider(n.Routing, n.Blockstore)
255
- go n.Reprovider.Run(ctx)
256
+ go n.Reprovider.ProvideEvery(ctx, time.Hour*12)
257
return nil
258
}
259
exchange/reprovide/reprovide.go
+4
-4
@@ -27,20 +27,20 @@ func NewReprovider(rsys routing.IpfsRouting, bstore blocks.Blockstore) *Reprovid
27
}
28
}
29
30
-func (rp *Reprovider) Run(ctx context.Context) {
30
+func (rp *Reprovider) ProvideEvery(ctx context.Context, tick time.Duration) {
31
after := time.After(0)
32
for {
33
select {
34
case <-ctx.Done():
35
return
36
case <-after:
37
- rp.reprovide(ctx)
38
- after = time.After(time.Hour * 12)
37
+ rp.Reprovide(ctx)
38
+ after = time.After(tick)
39
}
40
}
41
}
42
43
-func (rp *Reprovider) reprovide(ctx context.Context) {
43
+func (rp *Reprovider) Reprovide(ctx context.Context) {
44
keychan, err := rp.bstore.AllKeysChan(ctx, 0, 1<<16)
45
if err != nil {
46
log.Errorf("Failed to get key chan from blockstore: %s", err)
exchange/reprovide/reprovide_test.go
new
+50
@@ -0,0 +1,50 @@
1
+package reprovide_test
2
+
3
+import (
4
+ "testing"
5
+
6
+ context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
7
+ ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
8
+ dssync "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
9
+
10
+ blocks "github.com/jbenet/go-ipfs/blocks"
11
+ blockstore "github.com/jbenet/go-ipfs/blocks/blockstore"
12
+ mock "github.com/jbenet/go-ipfs/routing/mock"
13
+ testutil "github.com/jbenet/go-ipfs/util/testutil"
14
+
15
+ . "github.com/jbenet/go-ipfs/exchange/reprovide"
16
+)
17
+
18
+func TestReprovide(t *testing.T) {
19
+ ctx, cancel := context.WithCancel(context.Background())
20
+ defer cancel()
21
+
22
+ mrserv := mock.NewServer()
23
+
24
+ idA := testutil.RandIdentityOrFatal(t)
25
+ idB := testutil.RandIdentityOrFatal(t)
26
+
27
+ clA := mrserv.Client(idA)
28
+ clB := mrserv.Client(idB)
29
+
30
+ bstore := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
31
+
32
+ blk := blocks.NewBlock([]byte("this is a test"))
33
+ bstore.Put(blk)
34
+
35
+ reprov := NewReprovider(clA, bstore)
36
+ reprov.Reprovide(ctx)
37
+
38
+ provs, err := clB.FindProviders(ctx, blk.Key())
39
+ if err != nil {
40
+ t.Fatal(err)
41
+ }
42
+
43
+ if len(provs) == 0 {
44
+ t.Fatal("Should have gotten a provider")
45
+ }
46
+
47
+ if provs[0].ID != idA.ID() {
48
+ t.Fatal("Somehow got the wrong peer back as a provider.")
49
+ }
50
+}