Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(put): fail if any router fails #19

Merged
merged 2 commits into from
Mar 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions dummy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ import (
ropts "github.com/libp2p/go-libp2p-routing/options"
)

type failValueStore struct{}

var failValueErr = errors.New("fail valuestore error")

func (f failValueStore) PutValue(ctx context.Context, key string, value []byte, opts ...ropts.Option) error {
return failValueErr
}
func (f failValueStore) GetValue(ctx context.Context, key string, opts ...ropts.Option) ([]byte, error) {
return nil, failValueErr
}

func (f failValueStore) SearchValue(ctx context.Context, key string, opts ...ropts.Option) (<-chan []byte, error) {
return nil, failValueErr
}

type dummyValueStore sync.Map

func (d *dummyValueStore) PutValue(ctx context.Context, key string, value []byte, opts ...ropts.Option) error {
Expand Down
14 changes: 11 additions & 3 deletions parallel.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,15 @@ func (r Parallel) put(do func(routing.IpfsRouting) error) error {
}
wg.Wait()

var errs []error
var (
errs []error
success bool
)
for _, err := range results {
switch err {
case nil:
// Success!
return nil
// at least one router supports this.
success = true
case routing.ErrNotSupported:
default:
errs = append(errs, err)
Expand All @@ -144,6 +147,11 @@ func (r Parallel) put(do func(routing.IpfsRouting) error) error {

switch len(errs) {
case 0:
if success {
// No errors and at least one router succeeded.
return nil
}
// No routers supported this operation.
return routing.ErrNotSupported
case 1:
return errs[0]
Expand Down
18 changes: 18 additions & 0 deletions parallel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,24 @@ func TestParallelPutGet(t *testing.T) {
cancel()
}

func TestParallelPutFailure(t *testing.T) {
ctx := context.Background()
router := Parallel{
Routers: []routing.IpfsRouting{
&Compose{
ValueStore: new(failValueStore),
},
&Compose{
ValueStore: new(dummyValueStore),
},
},
}
err := router.PutValue(ctx, "/some/thing", []byte("thing"))
if err != failValueErr {
t.Fatalf("exected put to fail with %q, got %q", failValueErr, err)
}
}

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