|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + "google.golang.org/grpc" |
| 10 | + "google.golang.org/grpc/credentials/insecure" |
| 11 | + |
| 12 | + "gofr.dev/pkg/gofr" |
| 13 | + "gofr.dev/pkg/gofr/container" |
| 14 | + "gofr.dev/pkg/gofr/testutil" |
| 15 | +) |
| 16 | + |
| 17 | +// createTestContext creates a test gofr.Context |
| 18 | +func createTestContext() *gofr.Context { |
| 19 | + container := &container.Container{} |
| 20 | + return &gofr.Context{ |
| 21 | + Context: context.Background(), |
| 22 | + Container: container, |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func TestGoFrHelloClientWrapper_Creation(t *testing.T) { |
| 27 | + configs := testutil.NewServerConfigs(t) |
| 28 | + |
| 29 | + t.Run("NewHelloGoFrClient", func(t *testing.T) { |
| 30 | + // Test GoFr's NewHelloGoFrClient function |
| 31 | + conn, err := grpc.Dial(configs.GRPCHost, grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 32 | + require.NoError(t, err, "Connection creation should not fail immediately") |
| 33 | + defer conn.Close() |
| 34 | + |
| 35 | + app := gofr.New() |
| 36 | + helloClient, err := NewHelloGoFrClient(configs.GRPCHost, app.Metrics()) |
| 37 | + require.NoError(t, err, "GoFr hello client creation should not fail") |
| 38 | + assert.NotNil(t, helloClient, "GoFr hello client should not be nil") |
| 39 | + |
| 40 | + // Test that it implements the GoFr interface |
| 41 | + var _ HelloGoFrClient = helloClient |
| 42 | + }) |
| 43 | + |
| 44 | + t.Run("HelloClientWrapperInterface", func(t *testing.T) { |
| 45 | + // Test GoFr's interface compliance |
| 46 | + conn, err := grpc.Dial(configs.GRPCHost, grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 47 | + require.NoError(t, err, "Connection creation should not fail immediately") |
| 48 | + defer conn.Close() |
| 49 | + |
| 50 | + app := gofr.New() |
| 51 | + helloClient, err := NewHelloGoFrClient(configs.GRPCHost, app.Metrics()) |
| 52 | + require.NoError(t, err, "GoFr hello client creation should not fail") |
| 53 | + |
| 54 | + // Test HelloGoFrClient interface compliance |
| 55 | + var _ HelloGoFrClient = helloClient |
| 56 | + |
| 57 | + // Test that wrapper has the correct GoFr type |
| 58 | + wrapper, ok := helloClient.(*HelloClientWrapper) |
| 59 | + assert.True(t, ok, "Should be able to cast to GoFr HelloClientWrapper") |
| 60 | + assert.NotNil(t, wrapper.client, "Underlying hello client should not be nil") |
| 61 | + }) |
| 62 | +} |
| 63 | + |
| 64 | +func TestGoFrHelloClientWrapper_Methods(t *testing.T) { |
| 65 | + configs := testutil.NewServerConfigs(t) |
| 66 | + |
| 67 | + // Test GoFr's wrapper methods without actual gRPC calls |
| 68 | + app := gofr.New() |
| 69 | + helloClient, err := NewHelloGoFrClient(configs.GRPCHost, app.Metrics()) |
| 70 | + require.NoError(t, err, "GoFr hello client creation should not fail") |
| 71 | + ctx := createTestContext() |
| 72 | + |
| 73 | + t.Run("SayHelloMethodExists", func(t *testing.T) { |
| 74 | + // Test that GoFr's SayHello method exists and accepts correct parameters |
| 75 | + req := &HelloRequest{ |
| 76 | + Name: "test-name", |
| 77 | + } |
| 78 | + |
| 79 | + // This will fail due to connection, but we're testing GoFr's method signature |
| 80 | + _, err := helloClient.SayHello(ctx, req) |
| 81 | + assert.Error(t, err, "Should fail with invalid connection, but method should exist") |
| 82 | + }) |
| 83 | + |
| 84 | + t.Run("HealthClientEmbedded", func(t *testing.T) { |
| 85 | + // Test that GoFr's HelloGoFrClient embeds HealthClient |
| 86 | + // The HelloGoFrClient interface should include HealthClient methods |
| 87 | + var _ HealthClient = helloClient |
| 88 | + }) |
| 89 | +} |
| 90 | + |
| 91 | +func TestGoFrHelloClientWrapper_ContextIntegration(t *testing.T) { |
| 92 | + configs := testutil.NewServerConfigs(t) |
| 93 | + |
| 94 | + // Test GoFr's context integration |
| 95 | + app := gofr.New() |
| 96 | + helloClient, err := NewHelloGoFrClient(configs.GRPCHost, app.Metrics()) |
| 97 | + require.NoError(t, err, "GoFr hello client creation should not fail") |
| 98 | + |
| 99 | + t.Run("ContextParameter", func(t *testing.T) { |
| 100 | + // Test that GoFr's methods accept *gofr.Context |
| 101 | + ctx := createTestContext() |
| 102 | + req := &HelloRequest{ |
| 103 | + Name: "test-name", |
| 104 | + } |
| 105 | + |
| 106 | + // Test that the method signature is correct for GoFr context |
| 107 | + _, err := helloClient.SayHello(ctx, req) |
| 108 | + assert.Error(t, err, "Should fail with invalid connection") |
| 109 | + |
| 110 | + // Test that context is properly passed (even though call fails) |
| 111 | + assert.NotNil(t, ctx, "GoFr context should not be nil") |
| 112 | + }) |
| 113 | + |
| 114 | + t.Run("ContextTypeCompliance", func(t *testing.T) { |
| 115 | + // Test that GoFr's methods expect *gofr.Context specifically |
| 116 | + ctx := createTestContext() |
| 117 | + req := &HelloRequest{ |
| 118 | + Name: "test-name", |
| 119 | + } |
| 120 | + |
| 121 | + // Verify the method signature expects *gofr.Context |
| 122 | + var _ func(*gofr.Context, *HelloRequest, ...grpc.CallOption) (*HelloResponse, error) = helloClient.SayHello |
| 123 | + |
| 124 | + // Ensure the call compiles (even if it fails at runtime) |
| 125 | + _, _ = helloClient.SayHello(ctx, req) |
| 126 | + }) |
| 127 | +} |
| 128 | + |
| 129 | +func TestGoFrHelloClientWrapper_MultipleInstances(t *testing.T) { |
| 130 | + configs := testutil.NewServerConfigs(t) |
| 131 | + |
| 132 | + // Test GoFr's client creation with multiple instances |
| 133 | + t.Run("MultipleHelloClients", func(t *testing.T) { |
| 134 | + app := gofr.New() |
| 135 | + |
| 136 | + client1, err := NewHelloGoFrClient(configs.GRPCHost, app.Metrics()) |
| 137 | + require.NoError(t, err, "First GoFr hello client creation should not fail") |
| 138 | + |
| 139 | + client2, err := NewHelloGoFrClient(configs.GRPCHost, app.Metrics()) |
| 140 | + require.NoError(t, err, "Second GoFr hello client creation should not fail") |
| 141 | + |
| 142 | + assert.NotNil(t, client1, "First GoFr hello client should not be nil") |
| 143 | + assert.NotNil(t, client2, "Second GoFr hello client should not be nil") |
| 144 | + assert.NotEqual(t, client1, client2, "GoFr hello client instances should be different") |
| 145 | + }) |
| 146 | +} |
| 147 | + |
| 148 | +func TestGoFrHelloClientWrapper_ErrorHandling(t *testing.T) { |
| 149 | + _ = testutil.NewServerConfigs(t) |
| 150 | + |
| 151 | + // Test GoFr's error handling patterns |
| 152 | + t.Run("InvalidAddressHandling", func(t *testing.T) { |
| 153 | + // Test GoFr's handling of invalid addresses |
| 154 | + app := gofr.New() |
| 155 | + helloClient, err := NewHelloGoFrClient("invalid:address", app.Metrics()) |
| 156 | + require.NoError(t, err, "Client creation should not fail immediately") |
| 157 | + |
| 158 | + ctx := createTestContext() |
| 159 | + req := &HelloRequest{ |
| 160 | + Name: "test-name", |
| 161 | + } |
| 162 | + |
| 163 | + // Test GoFr's error handling |
| 164 | + _, err = helloClient.SayHello(ctx, req) |
| 165 | + assert.Error(t, err, "GoFr should handle invalid address errors") |
| 166 | + }) |
| 167 | + |
| 168 | + t.Run("EmptyAddressHandling", func(t *testing.T) { |
| 169 | + // Test GoFr's handling of empty addresses |
| 170 | + app := gofr.New() |
| 171 | + helloClient, err := NewHelloGoFrClient("", app.Metrics()) |
| 172 | + require.NoError(t, err, "Client creation should not fail immediately") |
| 173 | + |
| 174 | + ctx := createTestContext() |
| 175 | + req := &HelloRequest{ |
| 176 | + Name: "test-name", |
| 177 | + } |
| 178 | + |
| 179 | + // Test GoFr's error handling |
| 180 | + _, err = helloClient.SayHello(ctx, req) |
| 181 | + assert.Error(t, err, "GoFr should handle empty address errors") |
| 182 | + }) |
| 183 | +} |
| 184 | + |
| 185 | +func TestGoFrHelloClientWrapper_ConcurrentAccess(t *testing.T) { |
| 186 | + configs := testutil.NewServerConfigs(t) |
| 187 | + |
| 188 | + // Test GoFr's concurrent access patterns |
| 189 | + t.Run("ConcurrentSayHelloCalls", func(t *testing.T) { |
| 190 | + app := gofr.New() |
| 191 | + helloClient, err := NewHelloGoFrClient(configs.GRPCHost, app.Metrics()) |
| 192 | + require.NoError(t, err, "GoFr hello client creation should not fail") |
| 193 | + |
| 194 | + numGoroutines := 5 |
| 195 | + done := make(chan bool, numGoroutines) |
| 196 | + |
| 197 | + for i := 0; i < numGoroutines; i++ { |
| 198 | + go func(id int) { |
| 199 | + ctx := createTestContext() |
| 200 | + req := &HelloRequest{ |
| 201 | + Name: "concurrent-test", |
| 202 | + } |
| 203 | + |
| 204 | + // This will fail due to connection, but we're testing GoFr's concurrency |
| 205 | + _, err := helloClient.SayHello(ctx, req) |
| 206 | + assert.Error(t, err, "Should fail with invalid connection") |
| 207 | + done <- true |
| 208 | + }(i) |
| 209 | + } |
| 210 | + |
| 211 | + // Wait for all goroutines to complete |
| 212 | + for i := 0; i < numGoroutines; i++ { |
| 213 | + <-done |
| 214 | + } |
| 215 | + }) |
| 216 | +} |
0 commit comments