Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

Commit

Permalink
Merge pull request #65 from ipfs/ajnavarro/fix-content-routing-poc-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ajnavarro authored Nov 28, 2022
2 parents 2c9b6b8 + 2d015ee commit eb85a6d
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 155 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
fail-fast: false
matrix:
os: [ "ubuntu", "windows", "macos" ]
go: [ "1.17.x", "1.18.x" ]
go: [ "1.18.x", "1.19.x" ]
env:
COVERAGES: ""
runs-on: ${{ format('{0}-latest', matrix.os) }}
Expand Down
16 changes: 10 additions & 6 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"bytes"
"context"
"encoding/json"
"errors"
Expand All @@ -11,7 +12,6 @@ import (
"github.com/benbjohnson/clock"
"github.com/ipfs/go-cid"
delegatedrouting "github.com/ipfs/go-delegated-routing"
"github.com/ipfs/go-delegated-routing/internal/drjson"
ipns "github.com/ipfs/go-ipns"
logging "github.com/ipfs/go-log/v2"
record "github.com/libp2p/go-libp2p-record"
Expand Down Expand Up @@ -124,10 +124,10 @@ func (c *client) ProvideBitswap(ctx context.Context, keys []cid.Cid, ttl time.Du

req := delegatedrouting.BitswapWriteProviderRequest{
Protocol: "bitswap",
BitswapWriteProviderRequestPayload: delegatedrouting.BitswapWriteProviderRequestPayload{
Payload: delegatedrouting.BitswapWriteProviderRequestPayload{
Keys: ks,
AdvisoryTTL: delegatedrouting.Duration{Duration: ttl},
Timestamp: delegatedrouting.Time{Time: now},
AdvisoryTTL: &delegatedrouting.Duration{Duration: ttl},
Timestamp: &delegatedrouting.Time{Time: now},
ID: &c.peerID,
Addrs: c.addrs,
},
Expand Down Expand Up @@ -155,12 +155,15 @@ func (c *client) provideSignedBitswapRecord(ctx context.Context, bswp *delegated

url := c.baseURL + "/v1/providers"

reqBodyBuf, err := drjson.MarshalJSON(&req)
b, err := json.Marshal(req)
if err != nil {
return 0, err
}

httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, reqBodyBuf)
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBuffer(b))
if err != nil {
return 0, err
}

resp, err := c.httpClient.Do(httpReq)
if err != nil {
Expand All @@ -179,5 +182,6 @@ func (c *client) provideSignedBitswapRecord(ctx context.Context, bswp *delegated
if len(provideResult.ProvideResults) != 1 {
return 0, fmt.Errorf("expected 1 result but got %d", len(provideResult.ProvideResults))
}

return provideResult.ProvideResults[0].(*delegatedrouting.BitswapWriteProviderResponse).AdvisoryTTL, nil
}
65 changes: 33 additions & 32 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/rand"
"net/http/httptest"
"runtime"
"testing"
"time"

Expand Down Expand Up @@ -77,11 +78,6 @@ func makeCID() cid.Cid {
return c
}

func makeProvider() (peer.ID, []multiaddr.Multiaddr) {
peerID, addrs, _ := makeProviderAndIdentity()
return peerID, addrs
}

func addrsToDRAddrs(addrs []multiaddr.Multiaddr) (drmas []delegatedrouting.Multiaddr) {
for _, a := range addrs {
drmas = append(drmas, delegatedrouting.Multiaddr{Multiaddr: a})
Expand Down Expand Up @@ -127,16 +123,6 @@ func makeProviderAndIdentity() (peer.ID, []multiaddr.Multiaddr, crypto.PrivKey)
return peerID, []multiaddr.Multiaddr{ma1, ma2}, priv
}

func bsProvsToAIs(provs []delegatedrouting.BitswapReadProviderResponse) (ais []peer.AddrInfo) {
for _, prov := range provs {
ais = append(ais, peer.AddrInfo{
ID: *prov.ID,
Addrs: drAddrsToAddrs(prov.Addrs),
})
}
return
}

func TestClient_FindProviders(t *testing.T) {
bsReadProvResp := makeBSReadProviderResp()
bitswapProvs := []delegatedrouting.Provider{&bsReadProvResp}
Expand All @@ -148,8 +134,9 @@ func TestClient_FindProviders(t *testing.T) {
routerProvs []delegatedrouting.Provider
routerErr error

expProvs []delegatedrouting.Provider
expErrContains []string
expProvs []delegatedrouting.Provider
expErrContains []string
expWinErrContains []string
}{
{
name: "happy case",
Expand All @@ -162,9 +149,10 @@ func TestClient_FindProviders(t *testing.T) {
expErrContains: []string{"HTTP error with StatusCode=404: 404 page not found"},
},
{
name: "returns an error if the HTTP client returns a non-HTTP error",
stopServer: true,
expErrContains: []string{"connect: connection refused"},
name: "returns an error if the HTTP client returns a non-HTTP error",
stopServer: true,
expErrContains: []string{"connect: connection refused"},
expWinErrContains: []string{"connectex: No connection could be made because the target machine actively refused it."},
},
}
for _, c := range cases {
Expand All @@ -186,10 +174,17 @@ func TestClient_FindProviders(t *testing.T) {

provs, err := client.FindProviders(context.Background(), cid)

for _, exp := range c.expErrContains {
var errList []string
if runtime.GOOS == "windows" && len(c.expWinErrContains) != 0 {
errList = c.expWinErrContains
} else {
errList = c.expErrContains
}

for _, exp := range errList {
require.ErrorContains(t, err, exp)
}
if len(c.expErrContains) == 0 {
if len(errList) == 0 {
require.NoError(t, err)
}

Expand All @@ -213,7 +208,9 @@ func TestClient_Provide(t *testing.T) {
routerAdvisoryTTL time.Duration
routerErr error

expErrContains string
expErrContains string
expWinErrContains string

expAdvisoryTTL time.Duration
}{
{
Expand Down Expand Up @@ -247,9 +244,10 @@ func TestClient_Provide(t *testing.T) {
expErrContains: "HTTP error with StatusCode=404: 404 page not found",
},
{
name: "returns an error if the HTTP client returns a non-HTTP error",
stopServer: true,
expErrContains: "connect: connection refused",
name: "returns an error if the HTTP client returns a non-HTTP error",
stopServer: true,
expErrContains: "connect: connection refused",
expWinErrContains: "connectex: No connection could be made because the target machine actively refused it.",
},
}
for _, c := range cases {
Expand Down Expand Up @@ -290,10 +288,6 @@ func TestClient_Provide(t *testing.T) {
}
}

var cidStrs []string
for _, c := range c.cids {
cidStrs = append(cidStrs, c.String())
}
expectedProvReq := server.ProvideRequest{
Keys: c.cids,
Timestamp: clock.Now().Truncate(time.Millisecond),
Expand All @@ -307,8 +301,15 @@ func TestClient_Provide(t *testing.T) {

advisoryTTL, err := client.ProvideBitswap(ctx, c.cids, c.ttl)

if c.expErrContains != "" {
require.ErrorContains(t, err, c.expErrContains)
var errorString string
if runtime.GOOS == "windows" && c.expWinErrContains != "" {
errorString = c.expWinErrContains
} else {
errorString = c.expErrContains
}

if errorString != "" {
require.ErrorContains(t, err, errorString)
} else {
require.NoError(t, err)
}
Expand Down
4 changes: 0 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,3 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/blake3 v1.1.7 // indirect
)

replace github.com/libp2p/go-libp2p-gostream v0.5.0 => ../go-libp2p-gostream

replace github.com/libp2p/go-libp2p-http v0.4.0 => ../go-libp2p-http
22 changes: 0 additions & 22 deletions internal/drjson/json.go

This file was deleted.

16 changes: 0 additions & 16 deletions internal/drjson/json_test.go

This file was deleted.

2 changes: 0 additions & 2 deletions internal/goroutines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ func singleItemBatches(items []int) (batches [][]int) {
}

func TestDoBatch(t *testing.T) {
type batchHandler func(context.Context, sync.Mutex, [][]int, []int) error

cases := []struct {
name string
items []int
Expand Down
25 changes: 13 additions & 12 deletions server/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"bytes"
"context"
"encoding/json"
"errors"
Expand All @@ -12,7 +13,6 @@ import (
"github.com/gorilla/mux"
"github.com/ipfs/go-cid"
delegatedrouting "github.com/ipfs/go-delegated-routing"
"github.com/ipfs/go-delegated-routing/internal/drjson"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"

Expand Down Expand Up @@ -53,8 +53,7 @@ func Handler(svc ContentRouter, opts ...serverOption) http.Handler {
}

type server struct {
svc ContentRouter
router *mux.Router
svc ContentRouter
}

func (s *server) provide(w http.ResponseWriter, httpReq *http.Request) {
Expand All @@ -77,20 +76,20 @@ func (s *server) provide(w http.ResponseWriter, httpReq *http.Request) {
return
}

keys := make([]cid.Cid, len(v.Keys))
for i, k := range v.Keys {
keys := make([]cid.Cid, len(v.Payload.Keys))
for i, k := range v.Payload.Keys {
keys[i] = k.Cid

}
addrs := make([]multiaddr.Multiaddr, len(v.Addrs))
for i, a := range v.Addrs {
addrs := make([]multiaddr.Multiaddr, len(v.Payload.Addrs))
for i, a := range v.Payload.Addrs {
addrs[i] = a.Multiaddr
}
advisoryTTL, err := s.svc.Provide(httpReq.Context(), ProvideRequest{
Keys: keys,
Timestamp: v.Timestamp.Time,
AdvisoryTTL: v.AdvisoryTTL.Duration,
ID: *v.ID,
Timestamp: v.Payload.Timestamp.Time,
AdvisoryTTL: v.Payload.AdvisoryTTL.Duration,
ID: *v.Payload.ID,
Addrs: addrs,
})
if err != nil {
Expand Down Expand Up @@ -130,12 +129,13 @@ func (s *server) findProviders(w http.ResponseWriter, httpReq *http.Request) {
func writeResult(w http.ResponseWriter, method string, val any) {
// keep the marshaling separate from the writing, so we can distinguish bugs (which surface as 500)
// from transient network issues (which surface as transport errors)
buf, err := drjson.MarshalJSON(val)
b, err := json.Marshal(val)
if err != nil {
writeErr(w, method, http.StatusInternalServerError, fmt.Errorf("marshaling response: %w", err))
return
}
_, err = io.Copy(w, buf)

_, err = io.Copy(w, bytes.NewBuffer(b))
if err != nil {
logErr("Provide", "writing response body", err)
}
Expand All @@ -155,5 +155,6 @@ func writeErr(w http.ResponseWriter, method string, statusCode int, cause error)
}

func logErr(method, msg string, err error) {
fmt.Printf("err: %s", err)
logger.Infow(msg, "Method", method, "Error", err)
}
21 changes: 9 additions & 12 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ import (
"time"

"github.com/ipfs/go-cid"
"github.com/ipfs/go-delegated-routing/internal/drjson"
logging "github.com/ipfs/go-log/v2"
"github.com/multiformats/go-multiaddr"
)

var logger = logging.Logger("service/delegatedrouting")

type Time struct{ time.Time }

func (t Time) MarshalJSON() ([]byte, error) { return drjson.MarshalJSONBytes(t.UnixMilli()) }
func (t *Time) MarshalJSON() ([]byte, error) {
return json.Marshal(t.Time.UnixMilli())
}
func (t *Time) UnmarshalJSON(b []byte) error {
var timestamp int64
err := json.Unmarshal(b, &timestamp)
Expand All @@ -28,22 +26,20 @@ func (t *Time) UnmarshalJSON(b []byte) error {

type Duration struct{ time.Duration }

func (d Duration) MarshalJSON() ([]byte, error) {
return drjson.MarshalJSONBytes(d.Duration.Milliseconds())
}
func (d *Duration) MarshalJSON() ([]byte, error) { return json.Marshal(d.Duration) }
func (d *Duration) UnmarshalJSON(b []byte) error {
var dur int64
err := json.Unmarshal(b, &dur)
if err != nil {
return err
}
d.Duration = time.Duration(dur) * time.Millisecond
d.Duration = time.Duration(dur)
return nil
}

type CID struct{ cid.Cid }

func (c CID) MarshalJSON() ([]byte, error) { return drjson.MarshalJSONBytes(c.String()) }
func (c *CID) MarshalJSON() ([]byte, error) { return json.Marshal(c.String()) }
func (c *CID) UnmarshalJSON(b []byte) error {
var s string
err := json.Unmarshal(b, &s)
Expand Down Expand Up @@ -93,7 +89,7 @@ func (r UnknownWriteProviderResponse) MarshalJSON() ([]byte, error) {
if err != nil {
return nil, err
}
return drjson.MarshalJSONBytes(m)
return json.Marshal(m)
}

type WriteProvidersRequest struct {
Expand Down Expand Up @@ -270,5 +266,6 @@ func (u UnknownProvider) MarshalJSON() ([]byte, error) {
return nil, err
}
m["Protocol"] = u.Protocol
return drjson.MarshalJSONBytes(m)

return json.Marshal(m)
}
Loading

0 comments on commit eb85a6d

Please sign in to comment.