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: account for protocols that aren't tcp #611

Merged
merged 4 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 17 additions & 4 deletions pkg/grpc/client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package grpc

import (
"strings"
"fmt"
"net/url"
"net"

grpc "google.golang.org/grpc"
)
Expand All @@ -13,12 +15,23 @@
target string,
opts ...grpc.DialOption,
) (conn *grpc.ClientConn, err error) {
// strip the scheme from the target
target = strings.TrimPrefix(strings.TrimPrefix(target, "http://"), "https://")
// check if this is a host:port URI, if so continue,
// otherwise, parse the URL and extract the host and port
host, port, err := net.SplitHostPort(target)
if err != nil {
// parse the URL
ip, err := url.Parse(target)
if err != nil {
return nil, err

Check warning on line 25 in pkg/grpc/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/grpc/client.go#L25

Added line #L25 was not covered by tests
}

// extract the host and port
host, port = ip.Hostname(), ip.Port()
}

// create a new client
return grpc.NewClient(
target,
fmt.Sprintf("%s:%s", host, port),
opts...,
)
}
60 changes: 60 additions & 0 deletions pkg/grpc/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package grpc_test

import (
"fmt"
"net"
"testing"
"context"

slinkygrpc "github.com/skip-mev/slinky/pkg/grpc"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/reflection"
reflectionpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
)

func TestClient(t *testing.T) {
// spin up a mock grpc-server + test connecting to it via diff addresses
srv := grpc.NewServer()

// listen on a random open port
lis, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("failed to listen: %v", err)
}
// register reflection service on the server
reflection.Register(srv)

// start the server
go func() {

Check failure on line 30 in pkg/grpc/client_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

SA2002: the goroutine calls T.Fatalf, which must be called in the same goroutine as the test (staticcheck)
if err := srv.Serve(lis); err != nil {
t.Fatalf("failed to serve: %v", err)

Check failure on line 32 in pkg/grpc/client_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

testinggoroutine: call to (*testing.T).Fatalf from a non-test goroutine (govet)
}
}()

_, port, err := net.SplitHostPort(lis.Addr().String())
if err != nil {
t.Fatalf("failed to parse address: %v", err)
}

t.Run("try dialing via non supported GRPC target URL (i.e tcp prefix)", func(t *testing.T) {
// try dialing via non supported GRPC target URL (i.e tcp prefix)
client, err := slinkygrpc.NewClient(fmt.Sprintf("tcp://localhost:%s", port), grpc.WithTransportCredentials(insecure.NewCredentials()))
require.NoError(t, err)

// ping the server
_, err = reflectionpb.NewServerReflectionClient(client).ServerReflectionInfo(context.Background())
require.NoError(t, err)
})

t.Run("try dialing via supported GRPC target URL (i.e host:port)", func(t *testing.T) {
// try dialing via supported GRPC target URL (i.e host:port)
client, err := slinkygrpc.NewClient(fmt.Sprintf("localhost:%s", port), grpc.WithTransportCredentials(insecure.NewCredentials()))
require.NoError(t, err)

// ping the server
_, err = reflectionpb.NewServerReflectionClient(client).ServerReflectionInfo(context.Background())
require.NoError(t, err)
})
}
Loading