|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/ipfs/go-cid" |
| 8 | + "github.com/libp2p/go-libp2p-core/peer" |
| 9 | +) |
| 10 | + |
| 11 | +type TestDelegatedRoutingClient struct { |
| 12 | + NumResults int |
| 13 | +} |
| 14 | + |
| 15 | +func (t TestDelegatedRoutingClient) FindProviders(ctx context.Context, key cid.Cid) ([]peer.AddrInfo, error) { |
| 16 | + panic("not supported") |
| 17 | +} |
| 18 | + |
| 19 | +func (t TestDelegatedRoutingClient) FindProvidersAsync(ctx context.Context, key cid.Cid) (<-chan FindProvidersAsyncResult, error) { |
| 20 | + ch := make(chan FindProvidersAsyncResult) |
| 21 | + go func() { |
| 22 | + defer close(ch) |
| 23 | + for i := 0; i < t.NumResults; i++ { |
| 24 | + ch <- FindProvidersAsyncResult{ |
| 25 | + AddrInfo: []peer.AddrInfo{{}}, |
| 26 | + } |
| 27 | + } |
| 28 | + }() |
| 29 | + return ch, nil |
| 30 | +} |
| 31 | + |
| 32 | +func (t TestDelegatedRoutingClient) GetIPNS(ctx context.Context, id []byte) ([]byte, error) { |
| 33 | + panic("not supported") |
| 34 | +} |
| 35 | + |
| 36 | +func (t TestDelegatedRoutingClient) GetIPNSAsync(ctx context.Context, id []byte) (<-chan GetIPNSAsyncResult, error) { |
| 37 | + panic("not supported") |
| 38 | +} |
| 39 | + |
| 40 | +func (t TestDelegatedRoutingClient) PutIPNS(ctx context.Context, id []byte, record []byte) error { |
| 41 | + panic("not supported") |
| 42 | +} |
| 43 | + |
| 44 | +func (t TestDelegatedRoutingClient) PutIPNSAsync(ctx context.Context, id []byte, record []byte) (<-chan PutIPNSAsyncResult, error) { |
| 45 | + panic("not supported") |
| 46 | +} |
| 47 | + |
| 48 | +// TestContentRoutingFindProvidersUnlimitedResults is testing that ContentRoutingClient.FindProvidersAsync |
| 49 | +// correctly wraps DelegatedRoutingClient.FindProvidersAsync in the regime when the former allows for unlimited results. |
| 50 | +// This is a test of async semantics only. This is why values are not checked for validity. |
| 51 | +// Non-test implementations of DelegatedRoutingClient are responsible for returning valid values. |
| 52 | +func TestContentRoutingFindProvidersUnlimitedResults(t *testing.T) { |
| 53 | + providedResults := 5 |
| 54 | + c := NewContentRoutingClient(TestDelegatedRoutingClient{providedResults}) |
| 55 | + ch := c.FindProvidersAsync(context.Background(), cid.Cid{}, 0) |
| 56 | + num := 0 |
| 57 | + for range ch { |
| 58 | + num++ |
| 59 | + } |
| 60 | + if num != providedResults { |
| 61 | + t.Errorf("expecting %v results, got %v", providedResults, num) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// TestContentRoutingFindProvidersFewerResults is testing that ContentRoutingClient.FindProvidersAsync |
| 66 | +// correctly wraps DelegatedRoutingClient.FindProvidersAsync in the regime when the former allows for |
| 67 | +// fewer results than are available. |
| 68 | +// This is a test of async semantics only. This is why values are not checked for validity. |
| 69 | +// Non-test implementations of DelegatedRoutingClient are responsible for returning valid values. |
| 70 | +func TestContentRoutingFindProvidersFewerResults(t *testing.T) { |
| 71 | + providedResults := 5 |
| 72 | + wantResults := 3 |
| 73 | + c := NewContentRoutingClient(TestDelegatedRoutingClient{providedResults}) |
| 74 | + ch := c.FindProvidersAsync(context.Background(), cid.Cid{}, wantResults) |
| 75 | + num := 0 |
| 76 | + for range ch { |
| 77 | + num++ |
| 78 | + } |
| 79 | + if num != wantResults { |
| 80 | + t.Errorf("expecting %v results, got %v", wantResults, num) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// TestContentRoutingFindProvidersMoreResults is testing that ContentRoutingClient.FindProvidersAsync |
| 85 | +// correctly wraps DelegatedRoutingClient.FindProvidersAsync in the regime when the former allows for |
| 86 | +// more results than are available. |
| 87 | +// This is a test of async semantics only. This is why values are not checked for validity. |
| 88 | +// Non-test implementations of DelegatedRoutingClient are responsible for returning valid values. |
| 89 | +func TestContentRoutingFindProvidersMoreResults(t *testing.T) { |
| 90 | + providedResults := 5 |
| 91 | + wantResults := 7 |
| 92 | + c := NewContentRoutingClient(TestDelegatedRoutingClient{providedResults}) |
| 93 | + ch := c.FindProvidersAsync(context.Background(), cid.Cid{}, wantResults) |
| 94 | + num := 0 |
| 95 | + for range ch { |
| 96 | + num++ |
| 97 | + } |
| 98 | + if num != providedResults { |
| 99 | + t.Errorf("expecting %v results, got %v", providedResults, num) |
| 100 | + } |
| 101 | +} |
0 commit comments