Skip to content
Open
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
3 changes: 2 additions & 1 deletion grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down
38 changes: 38 additions & 0 deletions grpc/grpc_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package grpc

import (
"context"
"log/slog"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -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")
}
}