mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-08-13 00:02:57 -04:00
added the first grpc test, initially using the gprc gem stub client
This commit is contained in:
parent
9e10ee44eb
commit
ae4b7545b2
5
Gemfile
5
Gemfile
@ -37,6 +37,11 @@ group :test do
|
||||
gem "brotli"
|
||||
gem "ed25519"
|
||||
gem "net-ssh-gateway"
|
||||
|
||||
if RUBY_VERSION >= "2.3"
|
||||
gem "grpc"
|
||||
gem "logging"
|
||||
end
|
||||
end
|
||||
|
||||
platform :mri_21 do
|
||||
|
@ -29,6 +29,7 @@ class HTTPTest < Minitest::Test
|
||||
include Plugins::Stream
|
||||
include Plugins::AWSAuthentication
|
||||
include Plugins::Upgrade
|
||||
include Plugins::GRPC unless RUBY_ENGINE == "jruby" || RUBY_VERSION < "2.3"
|
||||
|
||||
def test_verbose_log
|
||||
log = StringIO.new
|
||||
|
76
test/support/grpc_helpers.rb
Normal file
76
test/support/grpc_helpers.rb
Normal file
@ -0,0 +1,76 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
begin
|
||||
require "grpc"
|
||||
require "logging"
|
||||
|
||||
# IF HTTPX_DEBUG ....
|
||||
module GRPC
|
||||
extend Logging.globally
|
||||
end
|
||||
Logging.logger.root.appenders = Logging.appenders.stdout
|
||||
Logging.logger.root.level = :info
|
||||
Logging.logger["GRPC"].level = :info
|
||||
Logging.logger["GRPC::ActiveCall"].level = :info
|
||||
Logging.logger["GRPC::BidiCall"].level = :info
|
||||
|
||||
module GRPCHelpers
|
||||
include ::GRPC::Core::StatusCodes
|
||||
include ::GRPC::Core::TimeConsts
|
||||
include ::GRPC::Core::CallOps
|
||||
|
||||
private
|
||||
|
||||
def teardown
|
||||
super
|
||||
return unless @grpc_server
|
||||
|
||||
@grpc_server.shutdown_and_notify(from_relative_time(2))
|
||||
@grpc_server.close
|
||||
@grpc_server_th.join if @grpc_server_th
|
||||
end
|
||||
|
||||
def run_request_response(resp, status, marshal: nil, server_args: {}, server_initial_md: {}, server_trailing_md: {})
|
||||
@grpc_server = ::GRPC::Core::Server.new(server_args.merge("grpc.so_reuseport" => 0))
|
||||
|
||||
server_port = @grpc_server.add_http2_port("0.0.0.0:0", :this_port_is_insecure)
|
||||
|
||||
@grpc_server_th = wakey_thread do |notifier|
|
||||
c = expect_server_to_be_invoked(notifier, metadata_to_send: server_initial_md, marshal: marshal)
|
||||
begin
|
||||
yield c
|
||||
ensure
|
||||
c.remote_send(resp)
|
||||
c.send_status(status, status == OK ? "OK" : "NOK", true, metadata: server_trailing_md)
|
||||
c.send(:set_input_stream_done)
|
||||
c.send(:set_output_stream_done)
|
||||
end
|
||||
end
|
||||
|
||||
server_port
|
||||
end
|
||||
|
||||
def wakey_thread(&blk)
|
||||
n = ::GRPC::Notifier.new
|
||||
t = Thread.new do
|
||||
blk.call(n)
|
||||
end
|
||||
t.abort_on_exception = true
|
||||
n.wait
|
||||
t
|
||||
end
|
||||
|
||||
def expect_server_to_be_invoked(notifier, metadata_to_send: nil, marshal: nil)
|
||||
@grpc_server.start
|
||||
notifier.notify(nil)
|
||||
recvd_rpc = @grpc_server.request_call
|
||||
recvd_call = recvd_rpc.call
|
||||
recvd_call.metadata = recvd_rpc.metadata
|
||||
recvd_call.run_batch(SEND_INITIAL_METADATA => metadata_to_send)
|
||||
::GRPC::ActiveCall.new(recvd_call, marshal, marshal, INFINITE_FUTURE, metadata_received: true)
|
||||
end
|
||||
end
|
||||
rescue LoadError
|
||||
module GRPCHelpers
|
||||
end
|
||||
end
|
32
test/support/requests/plugins/grpc.rb
Normal file
32
test/support/requests/plugins/grpc.rb
Normal file
@ -0,0 +1,32 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Requests
|
||||
module Plugins
|
||||
module GRPC
|
||||
include GRPCHelpers
|
||||
|
||||
def test_plugin_grpc_unary
|
||||
no_marshal = proc { |x| x }
|
||||
|
||||
server_port = run_request_response("a_reply", OK, marshal: no_marshal) do |call|
|
||||
assert call.remote_read == "a_request"
|
||||
assert call.metadata["k1"] == "v1"
|
||||
assert call.metadata["k2"] == "v2"
|
||||
end
|
||||
|
||||
# stub = ::GRPC::ClientStub.new("localhost:#{server_port}",
|
||||
# :this_channel_is_insecure)
|
||||
# grpc = HTTPX.plugin(:grpc)
|
||||
# # build service
|
||||
# stub = grpc.build_stub("localhost:#{server_port}", :this_channel_is_insecure)
|
||||
stub = ::GRPC::ClientStub.new("localhost:#{server_port}", :this_channel_is_insecure)
|
||||
|
||||
op = stub.request_response("an_rpc_method", "a_request", no_marshal, no_marshal, return_op: true, metadata: { k1: "v1", k2: "v2" })
|
||||
# op.start_call if run_start_call_first
|
||||
result = op.execute
|
||||
|
||||
assert result == "a_reply"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user