-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_grpc.rb
More file actions
320 lines (265 loc) · 10.7 KB
/
test_grpc.rb
File metadata and controls
320 lines (265 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# frozen_string_literal: true
require "test_helper"
require 'zlib'
require_relative "echo_pb"
require_relative "echo_services_pb"
class TestGrpc < HyperRubyTest
def test_grpc_request
buffer = String.new(capacity: 1024)
with_server(-> (request) { handler_grpc(request, buffer) }) do |_client|
stub = Echo::Echo::Stub.new(
"127.0.0.1:3010",
:this_channel_is_insecure,
channel_args: {
'grpc.enable_http_proxy' => 0
}
)
request = Echo::EchoRequest.new(message: "Hello GRPC")
response = stub.echo(request)
assert_instance_of Echo::EchoResponse, response
assert_equal "Hello GRPC response", response.message
end
end
def test_concurrent_grpc_requests
buffer = String.new(capacity: 1024)
with_server(-> (request) { handler_grpc(request, buffer) }) do |_client|
stub = Echo::Echo::Stub.new(
"127.0.0.1:3010",
:this_channel_is_insecure,
channel_args: {
'grpc.enable_http_proxy' => 0
}
)
threads = 5.times.map do |i|
Thread.new do
request = Echo::EchoRequest.new(message: "Hello GRPC #{i}")
response = stub.echo(request)
[i, response]
end
end
responses = threads.map(&:value)
responses.each do |i, response|
assert_instance_of Echo::EchoResponse, response
assert_equal "Hello GRPC #{i} response", response.message
end
end
end
def test_grpc_status_codes
with_server(-> (request) { handler_grpc_status(request) }) do |_client|
stub = Echo::Echo::Stub.new(
"127.0.0.1:3010",
:this_channel_is_insecure,
channel_args: {
'grpc.enable_http_proxy' => 0
}
)
# Test successful response (status 0)
request = Echo::EchoRequest.new(message: "success")
response = stub.echo(request)
assert_equal "success response", response.message
# Test error responses with different status codes
{
"invalid" => GRPC::InvalidArgument,
"not_found" => GRPC::NotFound,
"internal" => GRPC::Internal,
"unimplemented" => GRPC::Unimplemented
}.each do |message, expected_error|
error = assert_raises(expected_error) do
request = Echo::EchoRequest.new(message: message)
stub.echo(request)
end
assert_equal "#{message} error", error.details
end
end
end
def test_request_type_detection
with_server(-> (request) { handler_detect_type(request) }) do |client|
# Test gRPC request
stub = Echo::Echo::Stub.new(
"127.0.0.1:3010",
:this_channel_is_insecure,
channel_args: {
'grpc.enable_http_proxy' => 0
}
)
request = Echo::EchoRequest.new(message: "Hello gRPC")
grpc_response = stub.echo(request)
assert_instance_of Echo::EchoResponse, grpc_response
assert_equal "gRPC request: Hello gRPC", grpc_response.message
# Test regular HTTP request
http_response = client.post("/echo", body: "Hello HTTP")
assert_equal 200, http_response.status
assert_equal "text/plain", http_response.headers["content-type"]
assert_equal "HTTP request: Hello HTTP", http_response.body
end
end
def test_grpc_over_unix_socket
buffer = String.new(capacity: 1024)
with_unix_socket_server(-> (request) { handler_grpc(request, buffer) }) do |_client|
# Create a gRPC channel using the Unix socket
stub = Echo::Echo::Stub.new(
"unix:///tmp/hyper_ruby_test.sock",
:this_channel_is_insecure,
channel_args: {
'grpc.enable_http_proxy' => 0,
'grpc.default_authority' => 'localhost' # Required for Unix socket
}
)
request = Echo::EchoRequest.new(message: "Hello Unix Socket gRPC")
response = stub.echo(request)
assert_instance_of Echo::EchoResponse, response
assert_equal "Hello Unix Socket gRPC response", response.message
end
end
def test_grpc_compression
buffer = String.new(capacity: 1024)
compression_options = GRPC::Core::CompressionOptions.new(default_algorithm: :gzip)
compression_channel_args = compression_options.to_channel_arg_hash
with_server(-> (request) { handler_grpc_compressed(request, buffer) }) do |_client|
stub = Echo::Echo::Stub.new(
"127.0.0.1:3010",
:this_channel_is_insecure,
channel_args: {
'grpc.enable_http_proxy' => 0,
}.merge(compression_channel_args)
)
request = Echo::EchoRequest.new(message: "Hello Compressed GRPC " + ("a" * 10000))
response = stub.echo(request)
assert_instance_of Echo::EchoResponse, response
assert_equal "Decompressed: Hello Compressed GRPC " + ("a" * 10000), response.message
end
end
def test_max_connection_age_sends_goaway
# Test that max_connection_age causes the server to send GOAWAY after the configured time,
# forcing the client to establish a new connection
buffer = String.new(capacity: 1024)
server_config = {
bind_address: "127.0.0.1:3010",
tokio_threads: 1,
max_connection_age: 500 # 500ms max connection age
}
with_configured_server(server_config, -> (request) { handler_grpc(request, buffer) }) do |_client, server|
stub = Echo::Echo::Stub.new(
"127.0.0.1:3010",
:this_channel_is_insecure,
channel_args: {
'grpc.enable_http_proxy' => 0
}
)
# Record initial connection count
initial_connections = server.total_connections
# First request establishes a connection
request = Echo::EchoRequest.new(message: "Request 1")
response = stub.echo(request)
assert_equal "Request 1 response", response.message
# Should have one connection now
assert_equal initial_connections + 1, server.total_connections, "First request should establish one connection"
# Wait for max_connection_age to expire and GOAWAY to be sent
sleep 0.7
# Make another request - gRPC client should establish a new connection after GOAWAY
request = Echo::EchoRequest.new(message: "Request 2")
response = stub.echo(request)
assert_equal "Request 2 response", response.message
# Should have a second connection now (client reconnected after GOAWAY)
assert_equal initial_connections + 2, server.total_connections, "Second request after GOAWAY should establish a new connection"
end
end
def test_long_max_connection_age_reuses_connection
# Test that with a long max_connection_age, the connection is reused
# (opposite of test_max_connection_age_sends_goaway)
buffer = String.new(capacity: 1024)
server_config = {
bind_address: "127.0.0.1:3010",
tokio_threads: 1,
max_connection_age: 60000 # 60 seconds - much longer than test duration
}
with_configured_server(server_config, -> (request) { handler_grpc(request, buffer) }) do |_client, server|
stub = Echo::Echo::Stub.new(
"127.0.0.1:3010",
:this_channel_is_insecure,
channel_args: {
'grpc.enable_http_proxy' => 0
}
)
# Record initial connection count
initial_connections = server.total_connections
# First request establishes a connection
request = Echo::EchoRequest.new(message: "Request 1")
response = stub.echo(request)
assert_equal "Request 1 response", response.message
# Should have one connection now
assert_equal initial_connections + 1, server.total_connections, "First request should establish one connection"
# Wait a bit (but less than max_connection_age)
sleep 0.3
# Make another request - should reuse the same connection
request = Echo::EchoRequest.new(message: "Request 2")
response = stub.echo(request)
assert_equal "Request 2 response", response.message
# Should still have only one connection (connection reused, no GOAWAY sent)
assert_equal initial_connections + 1, server.total_connections, "Second request should reuse existing connection"
end
end
private
def handler_grpc(request, buffer)
assert_equal "application/grpc", request.header("content-type")
assert_equal "echo.Echo", request.service
assert_equal "Echo", request.method
request.fill_body(buffer)
echo_request = Echo::EchoRequest.decode(buffer)
echo_response = Echo::EchoResponse.new(message: echo_request.message + " response")
response_data = Echo::EchoResponse.encode(echo_response)
HyperRuby::GrpcResponse.new(0, response_data)
end
def handler_detect_type(request)
if request.is_a?(HyperRuby::GrpcRequest)
buffer = String.new(capacity: 1024)
request.fill_body(buffer)
echo_request = Echo::EchoRequest.decode(buffer)
echo_response = Echo::EchoResponse.new(message: "gRPC request: #{echo_request.message}")
response_data = Echo::EchoResponse.encode(echo_response)
HyperRuby::GrpcResponse.new(0, response_data)
else
buffer = String.new(capacity: 1024)
request.fill_body(buffer)
HyperRuby::Response.new(200, { 'Content-Type' => 'text/plain' }, "HTTP request: #{buffer}")
end
end
def handler_grpc_status(request)
buffer = String.new(capacity: 1024)
request.fill_body(buffer)
echo_request = Echo::EchoRequest.decode(buffer)
case echo_request.message
when "success"
echo_response = Echo::EchoResponse.new(message: "success response")
response_data = Echo::EchoResponse.encode(echo_response)
HyperRuby::GrpcResponse.new(0, response_data)
when "invalid"
HyperRuby::GrpcResponse.error(3, "invalid error") # INVALID_ARGUMENT = 3
when "not_found"
HyperRuby::GrpcResponse.error(5, "not_found error") # NOT_FOUND = 5
when "internal"
HyperRuby::GrpcResponse.error(13, "internal error") # INTERNAL = 13
when "unimplemented"
HyperRuby::GrpcResponse.error(12, "unimplemented error") # UNIMPLEMENTED = 12
else
HyperRuby::GrpcResponse.error(2, "unknown error") # UNKNOWN = 2
end
end
def handler_grpc_compressed(request, buffer)
assert_equal "application/grpc", request.header("content-type")
assert_equal "echo.Echo", request.service
assert_equal "Echo", request.method
# Check if the message is compressed
assert request.compressed?, "Expected request to be compressed"
# Get the compressed message
request.fill_body(buffer)
decompressed = Zlib.gunzip(buffer)
echo_request = Echo::EchoRequest.decode(decompressed)
echo_response = Echo::EchoResponse.new(message: "Decompressed: #{echo_request.message}")
response_data = Echo::EchoResponse.encode(echo_response)
HyperRuby::GrpcResponse.new(0, response_data)
rescue => e
pp e
raise e
end
end