Skip to content

Commit 1c19455

Browse files
authored
Merge branch 'development' into rewrite-new_test.go
2 parents b3f2956 + b64ab1e commit 1c19455

File tree

26 files changed

+2959
-415
lines changed

26 files changed

+2959
-415
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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+
}
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
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

Comments
 (0)