mirror of
https://github.com/ipfs/kubo.git
synced 2025-09-10 09:52:20 +08:00
add test for reprovider and slight refactor
This commit is contained in:
@ -2,6 +2,7 @@ package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||
b58 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
|
||||
@ -252,7 +253,7 @@ func (n *IpfsNode) StartOnlineServices() error {
|
||||
|
||||
// Start up reprovider system
|
||||
n.Reprovider = rp.NewReprovider(n.Routing, n.Blockstore)
|
||||
go n.Reprovider.Run(ctx)
|
||||
go n.Reprovider.ProvideEvery(ctx, time.Hour*12)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -27,20 +27,20 @@ func NewReprovider(rsys routing.IpfsRouting, bstore blocks.Blockstore) *Reprovid
|
||||
}
|
||||
}
|
||||
|
||||
func (rp *Reprovider) Run(ctx context.Context) {
|
||||
func (rp *Reprovider) ProvideEvery(ctx context.Context, tick time.Duration) {
|
||||
after := time.After(0)
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-after:
|
||||
rp.reprovide(ctx)
|
||||
after = time.After(time.Hour * 12)
|
||||
rp.Reprovide(ctx)
|
||||
after = time.After(tick)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (rp *Reprovider) reprovide(ctx context.Context) {
|
||||
func (rp *Reprovider) Reprovide(ctx context.Context) {
|
||||
keychan, err := rp.bstore.AllKeysChan(ctx, 0, 1<<16)
|
||||
if err != nil {
|
||||
log.Errorf("Failed to get key chan from blockstore: %s", err)
|
||||
|
50
exchange/reprovide/reprovide_test.go
Normal file
50
exchange/reprovide/reprovide_test.go
Normal file
@ -0,0 +1,50 @@
|
||||
package reprovide_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
|
||||
dssync "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
|
||||
|
||||
blocks "github.com/jbenet/go-ipfs/blocks"
|
||||
blockstore "github.com/jbenet/go-ipfs/blocks/blockstore"
|
||||
mock "github.com/jbenet/go-ipfs/routing/mock"
|
||||
testutil "github.com/jbenet/go-ipfs/util/testutil"
|
||||
|
||||
. "github.com/jbenet/go-ipfs/exchange/reprovide"
|
||||
)
|
||||
|
||||
func TestReprovide(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
mrserv := mock.NewServer()
|
||||
|
||||
idA := testutil.RandIdentityOrFatal(t)
|
||||
idB := testutil.RandIdentityOrFatal(t)
|
||||
|
||||
clA := mrserv.Client(idA)
|
||||
clB := mrserv.Client(idB)
|
||||
|
||||
bstore := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
|
||||
|
||||
blk := blocks.NewBlock([]byte("this is a test"))
|
||||
bstore.Put(blk)
|
||||
|
||||
reprov := NewReprovider(clA, bstore)
|
||||
reprov.Reprovide(ctx)
|
||||
|
||||
provs, err := clB.FindProviders(ctx, blk.Key())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(provs) == 0 {
|
||||
t.Fatal("Should have gotten a provider")
|
||||
}
|
||||
|
||||
if provs[0].ID != idA.ID() {
|
||||
t.Fatal("Somehow got the wrong peer back as a provider.")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user