kubo/routing/wrapper_test.go
Antonio Navarro Perez 92c4dc61a8
feat(routing): Delegated Routing (#8997)
* Delegated Routing.

Implementation of Reframe specs (https://github.com/ipfs/specs/blob/master/REFRAME.md) using go-delegated-routing library.

* Requested changes.

* Init using op string

* Separate possible ContentRouters for TopicDiscovery.

If we don't do this, we have a ciclic dependency creating TieredRouter.
Now we can create first all possible content routers, and after that,
create Routers.

* Set dht default routing type

* Add tests and remove uneeded code

* Add documentation.

* docs: Routing.Routers

* Requested changes.

Signed-off-by: Antonio Navarro Perez <antnavper@gmail.com>

* Add some documentation on new fx functions.

* Add changelog entry and integration tests

* test: sharness for 'dht' in 'routing' commands

Since 'routing' is currently the same as 'dht' (minus query command)
we need to test both, that way we won't have unnoticed divergence
in the default behavior.

* test(sharness): delegated routing via reframe URL

* Add more tests for delegated routing.

* If any put operation fails, the tiered router will fail.

* refactor: Routing.Routers: Parameters.Endpoint

As agreed  in https://github.com/ipfs/kubo/pull/8997#issuecomment-1175684716

* Try to improve CHANGELOG entry.

* chore: update reframe spec link

* Update go-delegated-routing dependency

* Fix config error test

* use new changelog format

* Remove port conflict

* go mod tidy

* ProviderManyWrapper to ProviderMany

* Update docs/changelogs/v0.14.md

Co-authored-by: Adin Schmahmann <adin.schmahmann@gmail.com>

Co-authored-by: Marcin Rataj <lidel@lidel.org>
Co-authored-by: Adin Schmahmann <adin.schmahmann@gmail.com>
2022-07-07 17:10:25 -04:00

102 lines
2.0 KiB
Go

package routing
import (
"context"
"errors"
"testing"
"github.com/multiformats/go-multihash"
)
func TestProvideManyWrapper_ProvideMany(t *testing.T) {
type fields struct {
pms []ProvideMany
}
tests := []struct {
name string
fields fields
wantErr bool
ready bool
}{
{
name: "one provider",
fields: fields{
pms: []ProvideMany{
newDummyProvideMany(true, false),
},
},
wantErr: false,
ready: true,
},
{
name: "two providers, no errors and ready",
fields: fields{
pms: []ProvideMany{
newDummyProvideMany(true, false),
newDummyProvideMany(true, false),
},
},
wantErr: false,
ready: true,
},
{
name: "two providers, no ready, no error",
fields: fields{
pms: []ProvideMany{
newDummyProvideMany(true, false),
newDummyProvideMany(false, false),
},
},
wantErr: false,
ready: false,
},
{
name: "two providers, no ready, and one erroing",
fields: fields{
pms: []ProvideMany{
newDummyProvideMany(true, false),
newDummyProvideMany(false, true),
},
},
wantErr: true,
ready: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pmw := &ProvideManyWrapper{
pms: tt.fields.pms,
}
if err := pmw.ProvideMany(context.Background(), nil); (err != nil) != tt.wantErr {
t.Errorf("ProvideManyWrapper.ProvideMany() error = %v, wantErr %v", err, tt.wantErr)
}
if ready := pmw.Ready(); ready != tt.ready {
t.Errorf("ProvideManyWrapper.Ready() unexpected output = %v, want %v", ready, tt.ready)
}
})
}
}
func newDummyProvideMany(ready, failProviding bool) *dummyProvideMany {
return &dummyProvideMany{
ready: ready,
failProviding: failProviding,
}
}
type dummyProvideMany struct {
ready, failProviding bool
}
func (dpm *dummyProvideMany) ProvideMany(ctx context.Context, keys []multihash.Multihash) error {
if dpm.failProviding {
return errors.New("error providing many")
}
return nil
}
func (dpm *dummyProvideMany) Ready() bool {
return dpm.ready
}