Skip to content

Commit

Permalink
coreapi: dht: test recursive provide
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
  • Loading branch information
magik6k committed Sep 1, 2018
1 parent 221d120 commit c1c8b4d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
2 changes: 1 addition & 1 deletion core/coreapi/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Bloc
return nil, err
}

err = api.node.Blocks.AddBlock(b)
err = api.node.Blockstore.Put(b)
if err != nil {
return nil, err
}
Expand Down
7 changes: 6 additions & 1 deletion core/coreapi/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,18 @@ func provideKeysRec(ctx context.Context, r routing.IpfsRouting, bs blockstore.Bl
err := dag.EnumerateChildrenAsync(ctx, dag.GetLinksDirect(dserv), c, provided.Visitor(ctx))
if err != nil {
errCh <- err
break
}
}
close(provided.New)
}()

for {
select {
case k := <-provided.New:
case k, ok := <-provided.New:
if !ok {
return nil
}
err := r.Provide(ctx, k, true)
if err != nil {
return err
Expand Down
34 changes: 20 additions & 14 deletions core/coreapi/dht_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ package coreapi_test
import (
"context"
"io"
"io/ioutil"
"testing"

"github.com/ipfs/go-ipfs/core/coreapi/interface"
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"

peer "gx/ipfs/QmQsErDt8Qgw1XrsXf2BpEzDgGWtB1YLsTAARBup5b6B9W/go-libp2p-peer"
blocks "gx/ipfs/QmWAzSEoqZ6xU6pu8yL8e5WaMb7wtbfbhhN4p1DknUPtr3/go-block-format"
"gx/ipfs/QmQsErDt8Qgw1XrsXf2BpEzDgGWtB1YLsTAARBup5b6B9W/go-libp2p-peer"
)

func TestDhtFindPeer(t *testing.T) {
Expand Down Expand Up @@ -70,33 +67,42 @@ func TestDhtProvide(t *testing.T) {
t.Fatal(err)
}

// TODO: replace once there is local add on unixfs or somewhere
data, err := ioutil.ReadAll(&io.LimitedReader{R: rnd, N: 4092})
p1, err := apis[0].Block().Put(ctx, &io.LimitedReader{R: rnd, N: 4092}, options.Block.Format("raw"))
p2, err := apis[0].Block().Put(ctx, &io.LimitedReader{R: rnd, N: 4092}, options.Block.Format("raw"))

out, err := apis[2].Dht().FindProviders(ctx, p1, options.Dht.NumProviders(1))
if err != nil {
t.Fatal(err)
}

b := blocks.NewBlock(data)
nds[0].Blockstore.Put(b)
p := iface.IpfsPath(b.Cid())
provider := <-out

out, err := apis[2].Dht().FindProviders(ctx, p, options.Dht.NumProviders(1))
if provider.ID.String() != "<peer.ID >" {
t.Errorf("got wrong provider: %s != %s", provider.ID.String(), nds[0].Identity.String())
}

err = apis[0].Dht().Provide(ctx, p1)
if err != nil {
t.Fatal(err)
}

provider := <-out
out, err = apis[2].Dht().FindProviders(ctx, p1, options.Dht.NumProviders(1))
if err != nil {
t.Fatal(err)
}

if provider.ID.String() != "<peer.ID >" {
provider = <-out

if provider.ID.String() != nds[0].Identity.String() {
t.Errorf("got wrong provider: %s != %s", provider.ID.String(), nds[0].Identity.String())
}

err = apis[0].Dht().Provide(ctx, p)
err = apis[0].Dht().Provide(ctx, p2, options.Dht.Recursive(true))
if err != nil {
t.Fatal(err)
}

out, err = apis[2].Dht().FindProviders(ctx, p, options.Dht.NumProviders(1))
out, err = apis[2].Dht().FindProviders(ctx, p2, options.Dht.NumProviders(1))
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit c1c8b4d

Please sign in to comment.