diff --git a/grpc/grpc.go b/grpc/grpc.go index 0e82e29..a877a15 100644 --- a/grpc/grpc.go +++ b/grpc/grpc.go @@ -87,6 +87,7 @@ func NewClient(serverAddr string, l logger) (*Client, error) { ) if err != nil { l.Error("Unable to dial new grpc client", "err", err) + return nil, fmt.Errorf("unable to create grpc client for %s: %w", serverAddr, err) } client := &Client{ conn: conn, @@ -196,7 +197,7 @@ func (c *Client) Check(ctx context.Context) error { resp, err := client.Check(tctx, &healthgrpc.HealthCheckRequest{}) if err != nil { // we do 1 retry (automagically with the next subconn thanks to the fallback LB) if it failed - resp, err = client.Check(ctx, &healthgrpc.HealthCheckRequest{}) + resp, err = client.Check(tctx, &healthgrpc.HealthCheckRequest{}) if err != nil { return err } diff --git a/grpc/grpc_test.go b/grpc/grpc_test.go index 5060cb9..a86c211 100644 --- a/grpc/grpc_test.go +++ b/grpc/grpc_test.go @@ -1,6 +1,9 @@ package grpc import ( + "context" + "log/slog" + "strings" "testing" "time" ) @@ -74,3 +77,38 @@ func TestNextBeaconTime(t *testing.T) { }) } } + +func TestNewClient_InvalidAddress(t *testing.T) { + l := slog.Default() + + _, err := NewClient("invalid://address that will fail", l) + if err == nil { + // gRPC uses lazy connections, so NewClient may not fail directly + t.Log("no error on creation (lazy conn); skipping") + return + } + + if !strings.Contains(err.Error(), "unable to create grpc client") && + !strings.Contains(err.Error(), "error") { + t.Errorf("unexpected error message: %v", err) + } +} + +func TestCheck_CancelledContext(t *testing.T) { + l := slog.Default() + + // connect to a non-existent server; conn is lazy so NewClient won't fail + client, err := NewClient("localhost:0", l) + if err != nil { + t.Skipf("NewClient failed: %v", err) + } + defer client.Close() + + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + err = client.Check(ctx) + if err == nil { + t.Error("expected error with cancelled context, got nil") + } +}