Skip to content

Commit

Permalink
add some more basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Dec 4, 2023
1 parent a71edeb commit 884e2d9
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
require (
github.com/ipfs/boxo v0.15.0
github.com/ipfs/go-cid v0.4.1
github.com/ipfs/go-log v1.0.5
github.com/libp2p/go-libp2p v0.32.1
github.com/libp2p/go-libp2p-kad-dht v0.25.1
github.com/libp2p/go-libp2p-record v0.2.0
Expand Down Expand Up @@ -45,7 +46,6 @@ require (
github.com/huin/goupnp v1.3.0 // indirect
github.com/ipfs/go-datastore v0.6.0 // indirect
github.com/ipfs/go-ipfs-util v0.0.3 // indirect
github.com/ipfs/go-log v1.0.5 // indirect
github.com/ipfs/go-log/v2 v2.5.1 // indirect
github.com/ipld/go-ipld-prime v0.21.0 // indirect
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
Expand Down
3 changes: 3 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import (

"github.com/ipfs/boxo/routing/http/client"
"github.com/ipfs/boxo/routing/http/server"
logging "github.com/ipfs/go-log"
"github.com/libp2p/go-libp2p"
dht "github.com/libp2p/go-libp2p-kad-dht"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/routing"
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
)

var logger = logging.Logger("someguy")

func start(ctx context.Context, port int, runAcceleratedDHTClient bool, contentEndpoints, peerEndpoints, ipnsEndpoints []string) error {
h, err := newHost(runAcceleratedDHTClient)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions server_routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ func find[T any](ctx context.Context, routers []router, call func(router) (iter.

// If all iterators failed to be created, then return the error.
if len(its) == 0 {
logger.Warnf("failed to create all iterators: %w", err)
return nil, err
} else if err != nil {
logger.Warnf("failed to create some iterators: %w", err)
}

// Otherwise return manyIter with remaining iterators.
Expand Down Expand Up @@ -191,6 +194,9 @@ func (mi *manyIter[T]) Close() error {
for _, it := range mi.its {
err = errors.Join(err, it.Close())
}
if err != nil {
logger.Warnf("errors on closing iterators: %w", err)
}
return err
}

Expand Down
90 changes: 85 additions & 5 deletions server_routers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/routing"
mh "github.com/multiformats/go-multihash"
"github.com/multiformats/go-multihash"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -264,14 +264,26 @@ func TestPutIPNS(t *testing.T) {
})
}

func makeCID() cid.Cid {
buf := make([]byte, 63)
_, err := rand.Read(buf)
if err != nil {
panic(err)
}
mh, err := multihash.Encode(buf, multihash.SHA2_256)
if err != nil {
panic(err)
}
c := cid.NewCidV1(cid.Raw, mh)
return c
}

func TestFindProviders(t *testing.T) {
t.Parallel()

t.Run("Basic", func(t *testing.T) {
prefix := cid.NewPrefixV1(cid.Raw, mh.SHA2_256)
c, _ := prefix.Sum([]byte("foo"))

ctx := context.Background()
c := makeCID()

d := parallelRouter{}
it, err := d.FindProviders(ctx, c, 10)
Expand All @@ -280,7 +292,8 @@ func TestFindProviders(t *testing.T) {
require.False(t, it.Next())

mr1 := &mockRouter{}
mr1.On("FindProviders", mock.Anything, c, 10).Return(iter.ToResultIter(iter.FromSlice([]types.Record{})), nil)
mr1Iter := newMockIter[types.Record](ctx)
mr1.On("FindProviders", mock.Anything, c, 10).Return(mr1Iter, nil)

d = parallelRouter{
routers: []router{
Expand All @@ -290,6 +303,73 @@ func TestFindProviders(t *testing.T) {
},
}

peers := []peer.ID{"peer1", "peer2", "peer3"}

go func() {
mr1Iter.ch <- iter.Result[types.Record]{Val: &types.PeerRecord{Schema: "peer", ID: &peers[0]}}
mr1Iter.ch <- iter.Result[types.Record]{Val: &types.PeerRecord{Schema: "peer", ID: &peers[1]}}
mr1Iter.ch <- iter.Result[types.Record]{Val: &types.PeerRecord{Schema: "peer", ID: &peers[2]}}
close(mr1Iter.ch)
}()

it, err = d.FindProviders(ctx, c, 10)
require.NoError(t, err)

results, err := iter.ReadAllResults(it)
require.NoError(t, err)
require.Len(t, results, 3)
})

t.Run("Failed to Create All Iterators", func(t *testing.T) {
ctx := context.Background()
c := makeCID()

d := parallelRouter{}
it, err := d.FindProviders(ctx, c, 10)

require.NoError(t, err)
require.False(t, it.Next())

mr1 := &mockRouter{}
mr1.On("FindProviders", mock.Anything, c, 10).Return(nil, errors.New("error a"))

mr2 := &mockRouter{}
mr2.On("FindProviders", mock.Anything, c, 10).Return(nil, errors.New("error b"))

d = parallelRouter{
routers: []router{
mr1, mr2,
},
}

_, err = d.FindProviders(ctx, c, 10)
require.ErrorContains(t, err, "error a")
require.ErrorContains(t, err, "error b")
})

t.Run("Failed to Create One Iterator", func(t *testing.T) {
ctx := context.Background()

c := makeCID()

d := parallelRouter{}
it, err := d.FindProviders(ctx, c, 10)

require.NoError(t, err)
require.False(t, it.Next())

mr1 := &mockRouter{}
mr1.On("FindProviders", mock.Anything, c, 10).Return(iter.ToResultIter(iter.FromSlice([]types.Record{})), nil)

mr2 := &mockRouter{}
mr2.On("FindProviders", mock.Anything, c, 10).Return(nil, errors.New("error b"))

d = parallelRouter{
routers: []router{
mr1, mr2,
},
}

it, err = d.FindProviders(ctx, c, 10)
require.NoError(t, err)
require.False(t, it.Next())
Expand Down

0 comments on commit 884e2d9

Please sign in to comment.