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

Return clear dial errors #445

Merged
merged 3 commits into from
Feb 28, 2024
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
9 changes: 5 additions & 4 deletions rpc/client/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (c *Client) createGRPCClient(ctx context.Context) (err error) {

var errInvalidEndpoint = errors.New("invalid endpoint options")

func (c *Client) openGRPCConn(ctx context.Context) error {
func (c *Client) openGRPCConn(ctx context.Context, extraDialOpts ...grpcstd.DialOption) error {
if c.conn != nil {
return nil
}
Expand All @@ -51,10 +51,11 @@ func (c *Client) openGRPCConn(ctx context.Context) error {
dialCtx, cancel := context.WithTimeout(ctx, c.dialTimeout)
var err error

c.conn, err = grpcstd.DialContext(dialCtx, c.addr,
c.conn, err = grpcstd.DialContext(dialCtx, c.addr, append([]grpcstd.DialOption{
grpcstd.WithTransportCredentials(creds),
grpcstd.WithBlock(),
)
grpcstd.WithReturnConnectionError(),
grpcstd.FailOnNonTempDialError(true),
}, extraDialOpts...)...)

cancel()

Expand Down
35 changes: 35 additions & 0 deletions rpc/client/connect_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package client

import (
"context"
"crypto/tls"
"net"
"testing"
"time"

"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/test/bufconn"
)

func TestClient_Init(t *testing.T) {
t.Run("TLS handshake failure", func(t *testing.T) {
lis := bufconn.Listen(1024) // size does not matter in this test

srv := grpc.NewServer()
t.Cleanup(srv.Stop)
go func() { _ = srv.Serve(lis) }()

c := New(WithNetworkURIAddress("grpcs://any:54321", new(tls.Config))...)

ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
t.Cleanup(cancel)

err := c.openGRPCConn(ctx, grpc.WithContextDialer(func(ctx context.Context, s string) (net.Conn, error) {
return lis.DialContext(ctx)
}))
// error is not wrapped properly, so we can do nothing more to check it.
// Text from stdlib tls.Conn.HandshakeContext.
require.ErrorContains(t, err, "first record does not look like a TLS handshake")
})
}
Loading