|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + "google.golang.org/grpc" |
| 9 | + "google.golang.org/grpc/credentials/insecure" |
| 10 | + healthpb "google.golang.org/grpc/health/grpc_health_v1" |
| 11 | + |
| 12 | + "gofr.dev/pkg/gofr" |
| 13 | + "gofr.dev/pkg/gofr/testutil" |
| 14 | +) |
| 15 | + |
| 16 | +func TestGoFrHealthClientWrapper_Creation(t *testing.T) { |
| 17 | + t.Setenv("GOFR_TELEMETRY", "false") |
| 18 | + |
| 19 | + configs := testutil.NewServerConfigs(t) |
| 20 | + |
| 21 | + t.Run("NewHealthClient", func(t *testing.T) { |
| 22 | + // Test GoFr's NewHealthClient function |
| 23 | + conn, err := grpc.Dial(configs.GRPCHost, grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 24 | + require.NoError(t, err, "Connection creation should not fail immediately") |
| 25 | + defer conn.Close() |
| 26 | + |
| 27 | + healthClient := NewHealthClient(conn) |
| 28 | + assert.NotNil(t, healthClient, "GoFr health client should not be nil") |
| 29 | + |
| 30 | + // Test that it implements the GoFr interface |
| 31 | + var _ HealthClient = healthClient |
| 32 | + }) |
| 33 | + |
| 34 | + t.Run("HealthClientWrapperInterface", func(t *testing.T) { |
| 35 | + // Test GoFr's interface compliance |
| 36 | + conn, err := grpc.Dial(configs.GRPCHost, grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 37 | + require.NoError(t, err, "Connection creation should not fail immediately") |
| 38 | + defer conn.Close() |
| 39 | + |
| 40 | + healthClient := NewHealthClient(conn) |
| 41 | + |
| 42 | + // Test HealthClient interface compliance |
| 43 | + var _ HealthClient = healthClient |
| 44 | + |
| 45 | + // Test that wrapper has the correct GoFr type |
| 46 | + wrapper, ok := healthClient.(*HealthClientWrapper) |
| 47 | + assert.True(t, ok, "Should be able to cast to GoFr HealthClientWrapper") |
| 48 | + assert.NotNil(t, wrapper.client, "Underlying health client should not be nil") |
| 49 | + }) |
| 50 | +} |
| 51 | + |
| 52 | +func TestGoFrHealthClientWrapper_Methods(t *testing.T) { |
| 53 | + t.Setenv("GOFR_TELEMETRY", "false") |
| 54 | + |
| 55 | + configs := testutil.NewServerConfigs(t) |
| 56 | + |
| 57 | + // Test GoFr's wrapper methods without actual gRPC calls |
| 58 | + conn, err := grpc.Dial(configs.GRPCHost, grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 59 | + require.NoError(t, err, "Connection creation should not fail immediately") |
| 60 | + defer conn.Close() |
| 61 | + |
| 62 | + healthClient := NewHealthClient(conn) |
| 63 | + ctx := createTestContext() |
| 64 | + |
| 65 | + t.Run("CheckMethodExists", func(t *testing.T) { |
| 66 | + // Test that GoFr's Check method exists and accepts correct parameters |
| 67 | + req := &healthpb.HealthCheckRequest{ |
| 68 | + Service: "test-service", |
| 69 | + } |
| 70 | + |
| 71 | + // This will fail due to connection, but we're testing GoFr's method signature |
| 72 | + _, err := healthClient.Check(ctx, req) |
| 73 | + assert.Error(t, err, "Should fail with invalid connection, but method should exist") |
| 74 | + }) |
| 75 | + |
| 76 | + t.Run("WatchMethodExists", func(t *testing.T) { |
| 77 | + // Test that GoFr's Watch method exists and accepts correct parameters |
| 78 | + req := &healthpb.HealthCheckRequest{ |
| 79 | + Service: "test-service", |
| 80 | + } |
| 81 | + |
| 82 | + // This will fail due to connection, but we're testing GoFr's method signature |
| 83 | + _, err := healthClient.Watch(ctx, req) |
| 84 | + assert.Error(t, err, "Should fail with invalid connection, but method should exist") |
| 85 | + }) |
| 86 | +} |
| 87 | + |
| 88 | +func TestGoFrHealthClientWrapper_ContextIntegration(t *testing.T) { |
| 89 | + t.Setenv("GOFR_TELEMETRY", "false") |
| 90 | + |
| 91 | + configs := testutil.NewServerConfigs(t) |
| 92 | + |
| 93 | + // Test GoFr's context integration |
| 94 | + conn, err := grpc.Dial(configs.GRPCHost, grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 95 | + require.NoError(t, err, "Connection creation should not fail immediately") |
| 96 | + defer conn.Close() |
| 97 | + |
| 98 | + healthClient := NewHealthClient(conn) |
| 99 | + |
| 100 | + t.Run("ContextParameter", func(t *testing.T) { |
| 101 | + // Test that GoFr's methods accept *gofr.Context |
| 102 | + ctx := createTestContext() |
| 103 | + req := &healthpb.HealthCheckRequest{ |
| 104 | + Service: "test-service", |
| 105 | + } |
| 106 | + |
| 107 | + // Test that the method signature is correct for GoFr context |
| 108 | + _, err := healthClient.Check(ctx, req) |
| 109 | + assert.Error(t, err, "Should fail with invalid connection") |
| 110 | + |
| 111 | + // Test that context is properly passed (even though call fails) |
| 112 | + assert.NotNil(t, ctx, "GoFr context should not be nil") |
| 113 | + }) |
| 114 | + |
| 115 | + t.Run("ContextTypeCompliance", func(t *testing.T) { |
| 116 | + // Test that GoFr's methods expect *gofr.Context specifically |
| 117 | + ctx := createTestContext() |
| 118 | + req := &healthpb.HealthCheckRequest{ |
| 119 | + Service: "test-service", |
| 120 | + } |
| 121 | + |
| 122 | + // Verify the method signature expects *gofr.Context |
| 123 | + var _ func(*gofr.Context, *healthpb.HealthCheckRequest, ...grpc.CallOption) (*healthpb.HealthCheckResponse, error) = healthClient.Check |
| 124 | + var _ func(*gofr.Context, *healthpb.HealthCheckRequest, ...grpc.CallOption) (grpc.ServerStreamingClient[healthpb.HealthCheckResponse], error) = healthClient.Watch |
| 125 | + |
| 126 | + // Ensure the call compiles (even if it fails at runtime) |
| 127 | + _, _ = healthClient.Check(ctx, req) |
| 128 | + _, _ = healthClient.Watch(ctx, req) |
| 129 | + }) |
| 130 | +} |
| 131 | + |
| 132 | +func TestGoFrHealthClientWrapper_ErrorHandling(t *testing.T) { |
| 133 | + t.Setenv("GOFR_TELEMETRY", "false") |
| 134 | + |
| 135 | + // Test GoFr's error handling patterns |
| 136 | + t.Run("InvalidConnectionHandling", func(t *testing.T) { |
| 137 | + // Test GoFr's handling of invalid connections |
| 138 | + conn, err := grpc.Dial("invalid:address", grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 139 | + require.NoError(t, err, "Connection creation should not fail immediately") |
| 140 | + defer conn.Close() |
| 141 | + |
| 142 | + healthClient := NewHealthClient(conn) |
| 143 | + ctx := createTestContext() |
| 144 | + |
| 145 | + req := &healthpb.HealthCheckRequest{ |
| 146 | + Service: "test-service", |
| 147 | + } |
| 148 | + |
| 149 | + // Test GoFr's error handling |
| 150 | + _, err = healthClient.Check(ctx, req) |
| 151 | + assert.Error(t, err, "GoFr should handle invalid connection errors") |
| 152 | + }) |
| 153 | +} |
0 commit comments