Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shannonwells committed Jan 14, 2020
1 parent 93f7ddf commit efd596d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
13 changes: 6 additions & 7 deletions retrievalmarket/impl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package retrievalimpl

import (
"context"
"errors"
"reflect"
"sync"

Expand Down Expand Up @@ -37,7 +36,11 @@ type client struct {
}

// NewClient creates a new retrieval client
func NewClient(network rmnet.RetrievalMarketNetwork, bs blockstore.Blockstore, node retrievalmarket.RetrievalClientNode, resolver retrievalmarket.PeerResolver) retrievalmarket.RetrievalClient {
func NewClient(
network rmnet.RetrievalMarketNetwork,
bs blockstore.Blockstore,
node retrievalmarket.RetrievalClientNode,
resolver retrievalmarket.PeerResolver) retrievalmarket.RetrievalClient {
return &client{
network: network,
bs: bs,
Expand All @@ -51,15 +54,11 @@ func NewClient(network rmnet.RetrievalMarketNetwork, bs blockstore.Blockstore, n
// TODO: Implement for retrieval provider V0 epic
// https://github.com/filecoin-project/go-retrieval-market-project/issues/12
func (c *client) FindProviders(pieceCIDBytes []byte) []retrievalmarket.RetrievalPeer {
cidLen, pieceCid, err := cid.CidFromBytes(pieceCIDBytes)
_, pieceCid, err := cid.CidFromBytes(pieceCIDBytes)
if err != nil {
log.Error(err)
return []retrievalmarket.RetrievalPeer{}
}
if cidLen == 0 {
log.Error(errors.New("zero-length CID"))
return []retrievalmarket.RetrievalPeer{}
}
peers, err := c.resolver.GetPeers(pieceCid)
if err != nil {
log.Error(err)
Expand Down
42 changes: 41 additions & 1 deletion retrievalmarket/impl/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-data-transfer/testutil"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
dss "github.com/ipfs/go-datastore/sync"
Expand Down Expand Up @@ -121,6 +122,45 @@ func TestClient_Query(t *testing.T) {
})
}

func TestClient_FindProviders(t *testing.T) {
bs := bstore.NewBlockstore(dss.MutexWrap(datastore.NewMapDatastore()))
expectedPeer := peer.ID("somevalue")

var qsb tut.QueryStreamBuilder = func(p peer.ID) (rmnet.RetrievalQueryStream, error) {
return tut.NewTestRetrievalQueryStream(tut.TestQueryStreamParams{
Writer: tut.TrivialQueryWriter,
RespReader: tut.TrivialQueryResponseReader,
}), nil
}
net := tut.NewTestRetrievalMarketNetwork(tut.TestNetworkParams{
QueryStreamBuilder: tut.ExpectPeerOnQueryStreamBuilder(t, expectedPeer, qsb, "Peers should match"),
})

t.Run("when providers are found, returns providers", func(t *testing.T) {
peers := tut.RequireGenerateRetrievalPeers(t, 3)
testResolver := testPeerResolver{peers: peers}

c := retrievalimpl.NewClient(net, bs, &testRetrievalNode{}, &testResolver)
testCid := testutil.GenerateCids(1)[0].Bytes()
assert.Len(t, c.FindProviders(testCid), 3)
})

t.Run("when there is an error, returns empty provider list", func(t *testing.T) {
peers := tut.RequireGenerateRetrievalPeers(t, 1)
testResolver := testPeerResolver{peers: peers}
c := retrievalimpl.NewClient(net, bs, &testRetrievalNode{}, &testResolver)
badCid := []byte("badcid")
assert.Len(t, c.FindProviders(badCid), 0)
})

t.Run("when there are no providers", func(t *testing.T) {
testResolver := testPeerResolver{peers: []retrievalmarket.RetrievalPeer{}}
c := retrievalimpl.NewClient(net, bs, &testRetrievalNode{}, &testResolver)
testCid := testutil.GenerateCids(1)[0].Bytes()
assert.Len(t, c.FindProviders(testCid), 0)
})
}

type testRetrievalNode struct {
}

Expand All @@ -139,9 +179,9 @@ func (t *testRetrievalNode) CreatePaymentVoucher(ctx context.Context, paymentCha
type testPeerResolver struct {
peers []retrievalmarket.RetrievalPeer
}

var _ retrievalmarket.PeerResolver = &testPeerResolver{}

func (t testPeerResolver) GetPeers(data cid.Cid) ([]retrievalmarket.RetrievalPeer, error) {
return t.peers, nil
}

18 changes: 18 additions & 0 deletions shared_testutil/test_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package shared_testutil
import (
"math/big"
"math/rand"
"testing"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-data-transfer/testutil"
"github.com/libp2p/go-libp2p-core/test"
"github.com/stretchr/testify/require"

"github.com/filecoin-project/go-fil-markets/retrievalmarket"
"github.com/filecoin-project/go-fil-markets/shared/tokenamount"
Expand Down Expand Up @@ -107,3 +110,18 @@ func MakeTestDealPayment() retrievalmarket.DealPayment {
PaymentVoucher: MakeTestSignedVoucher(),
}
}

func RequireGenerateRetrievalPeers(t *testing.T, numPeers int) []retrievalmarket.RetrievalPeer {
peers := make([]retrievalmarket.RetrievalPeer, numPeers)
for i := range peers {
pid, err := test.RandPeerID()
require.NoError(t, err)
addr, err := address.NewIDAddress(rand.Uint64())
require.NoError(t, err)
peers[i] = retrievalmarket.RetrievalPeer{
Address: addr,
ID: pid,
}
}
return peers
}

0 comments on commit efd596d

Please sign in to comment.