diff options
Diffstat (limited to 'src/ruby/spec/generic/rpc_server_spec.rb')
-rw-r--r-- | src/ruby/spec/generic/rpc_server_spec.rb | 139 |
1 files changed, 64 insertions, 75 deletions
diff --git a/src/ruby/spec/generic/rpc_server_spec.rb b/src/ruby/spec/generic/rpc_server_spec.rb index 5997fdb363..cd4888a3b4 100644 --- a/src/ruby/spec/generic/rpc_server_spec.rb +++ b/src/ruby/spec/generic/rpc_server_spec.rb @@ -37,33 +37,37 @@ def load_test_certs files.map { |f| File.open(File.join(test_root, f)).read } end +# A test message class EchoMsg - def self.marshal(o) + def self.marshal(_o) '' end - def self.unmarshal(o) + def self.unmarshal(_o) EchoMsg.new end end +# A test service with no methods. class EmptyService include GRPC::GenericService end +# A test service without an implementation. class NoRpcImplementation include GRPC::GenericService rpc :an_rpc, EchoMsg, EchoMsg end +# A test service with an implementation. class EchoService include GRPC::GenericService rpc :an_rpc, EchoMsg, EchoMsg - def initialize(default_var='ignored') + def initialize(_default_var = 'ignored') end - def an_rpc(req, call) + def an_rpc(req, _call) logger.info('echo service received a request') req end @@ -71,14 +75,15 @@ end EchoStub = EchoService.rpc_stub_class +# A slow test service. class SlowService include GRPC::GenericService rpc :an_rpc, EchoMsg, EchoMsg - def initialize(default_var='ignored') + def initialize(_default_var = 'ignored') end - def an_rpc(req, call) + def an_rpc(req, _call) delay = 0.25 logger.info("starting a slow #{delay} rpc") sleep delay @@ -89,7 +94,6 @@ end SlowStub = SlowService.rpc_stub_class describe GRPC::RpcServer do - RpcServer = GRPC::RpcServer StatusCodes = GRPC::Core::StatusCodes @@ -97,7 +101,7 @@ describe GRPC::RpcServer do @method = 'an_rpc_method' @pass = 0 @fail = 1 - @noop = Proc.new { |x| x } + @noop = proc { |x| x } @server_queue = GRPC::Core::CompletionQueue.new port = find_unused_tcp_port @@ -112,18 +116,17 @@ describe GRPC::RpcServer do end describe '#new' do - it 'can be created with just some args' do - opts = {:a_channel_arg => 'an_arg'} - blk = Proc.new do + opts = { a_channel_arg: 'an_arg' } + blk = proc do RpcServer.new(**opts) end expect(&blk).not_to raise_error end it 'can be created with a default deadline' do - opts = {:a_channel_arg => 'an_arg', :deadline => 5} - blk = Proc.new do + opts = { a_channel_arg: 'an_arg', deadline: 5 } + blk = proc do RpcServer.new(**opts) end expect(&blk).not_to raise_error @@ -131,20 +134,20 @@ describe GRPC::RpcServer do it 'can be created with a completion queue override' do opts = { - :a_channel_arg => 'an_arg', - :completion_queue_override => @server_queue + a_channel_arg: 'an_arg', + completion_queue_override: @server_queue } - blk = Proc.new do + blk = proc do RpcServer.new(**opts) end expect(&blk).not_to raise_error end it 'cannot be created with a bad completion queue override' do - blk = Proc.new do + blk = proc do opts = { - :a_channel_arg => 'an_arg', - :completion_queue_override => Object.new + a_channel_arg: 'an_arg', + completion_queue_override: Object.new } RpcServer.new(**opts) end @@ -152,10 +155,10 @@ describe GRPC::RpcServer do end it 'cannot be created with invalid ServerCredentials' do - blk = Proc.new do + blk = proc do opts = { - :a_channel_arg => 'an_arg', - :creds => Object.new + a_channel_arg: 'an_arg', + creds: Object.new } RpcServer.new(**opts) end @@ -165,10 +168,10 @@ describe GRPC::RpcServer do it 'can be created with the creds as valid ServerCedentials' do certs = load_test_certs server_creds = GRPC::Core::ServerCredentials.new(nil, certs[1], certs[2]) - blk = Proc.new do + blk = proc do opts = { - :a_channel_arg => 'an_arg', - :creds => server_creds + a_channel_arg: 'an_arg', + creds: server_creds } RpcServer.new(**opts) end @@ -176,30 +179,28 @@ describe GRPC::RpcServer do end it 'can be created with a server override' do - opts = {:a_channel_arg => 'an_arg', :server_override => @server} - blk = Proc.new do + opts = { a_channel_arg: 'an_arg', server_override: @server } + blk = proc do RpcServer.new(**opts) end expect(&blk).not_to raise_error end it 'cannot be created with a bad server override' do - blk = Proc.new do + blk = proc do opts = { - :a_channel_arg => 'an_arg', - :server_override => Object.new + a_channel_arg: 'an_arg', + server_override: Object.new } RpcServer.new(**opts) end expect(&blk).to raise_error end - end describe '#stopped?' do - before(:each) do - opts = {:a_channel_arg => 'an_arg', :poll_period => 1} + opts = { a_channel_arg: 'an_arg', poll_period: 1 } @srv = RpcServer.new(**opts) end @@ -229,33 +230,31 @@ describe GRPC::RpcServer do expect(@srv.stopped?).to be(true) t.join end - end describe '#running?' do - it 'starts out false' do - opts = {:a_channel_arg => 'an_arg', :server_override => @server} + opts = { a_channel_arg: 'an_arg', server_override: @server } r = RpcServer.new(**opts) expect(r.running?).to be(false) end it 'is false after run is called with no services registered' do opts = { - :a_channel_arg => 'an_arg', - :poll_period => 1, - :server_override => @server + a_channel_arg: 'an_arg', + poll_period: 1, + server_override: @server } r = RpcServer.new(**opts) - r.run() + r.run expect(r.running?).to be(false) end it 'is true after run is called with a registered service' do opts = { - :a_channel_arg => 'an_arg', - :poll_period => 1, - :server_override => @server + a_channel_arg: 'an_arg', + poll_period: 1, + server_override: @server } r = RpcServer.new(**opts) r.handle(EchoService) @@ -265,13 +264,11 @@ describe GRPC::RpcServer do r.stop t.join end - end describe '#handle' do - before(:each) do - @opts = {:a_channel_arg => 'an_arg', :poll_period => 1} + @opts = { a_channel_arg: 'an_arg', poll_period: 1 } @srv = RpcServer.new(**@opts) end @@ -309,33 +306,30 @@ describe GRPC::RpcServer do @srv.handle(EchoService) expect { r.handle(EchoService) }.to raise_error end - end describe '#run' do - before(:each) do @client_opts = { - :channel_override => @ch + channel_override: @ch } @marshal = EchoService.rpc_descs[:an_rpc].marshal_proc @unmarshal = EchoService.rpc_descs[:an_rpc].unmarshal_proc(:output) server_opts = { - :server_override => @server, - :completion_queue_override => @server_queue, - :poll_period => 1 + server_override: @server, + completion_queue_override: @server_queue, + poll_period: 1 } @srv = RpcServer.new(**server_opts) end describe 'when running' do - it 'should return NOT_FOUND status for requests on unknown methods' do @srv.handle(EchoService) t = Thread.new { @srv.run } @srv.wait_till_running req = EchoMsg.new - blk = Proc.new do + blk = proc do cq = GRPC::Core::CompletionQueue.new stub = GRPC::ClientStub.new(@host, cq, **@client_opts) stub.request_response('/unknown', req, @marshal, @unmarshal) @@ -352,20 +346,19 @@ describe GRPC::RpcServer do req = EchoMsg.new n = 5 # arbitrary stub = EchoStub.new(@host, **@client_opts) - n.times { |x| expect(stub.an_rpc(req)).to be_a(EchoMsg) } + n.times { expect(stub.an_rpc(req)).to be_a(EchoMsg) } @srv.stop t.join end it 'should obtain responses for multiple parallel requests' do @srv.handle(EchoService) - t = Thread.new { @srv.run } + Thread.new { @srv.run } @srv.wait_till_running req, q = EchoMsg.new, Queue.new n = 5 # arbitrary threads = [] - n.times do |x| - cq = GRPC::Core::CompletionQueue.new + n.times do threads << Thread.new do stub = EchoStub.new(@host, **@client_opts) q << stub.an_rpc(req) @@ -373,44 +366,40 @@ describe GRPC::RpcServer do end n.times { expect(q.pop).to be_a(EchoMsg) } @srv.stop - threads.each { |t| t.join } + threads.each(&:join) end it 'should return UNAVAILABLE status if there too many jobs' do opts = { - :a_channel_arg => 'an_arg', - :server_override => @server, - :completion_queue_override => @server_queue, - :pool_size => 1, - :poll_period => 1, - :max_waiting_requests => 0 + a_channel_arg: 'an_arg', + server_override: @server, + completion_queue_override: @server_queue, + pool_size: 1, + poll_period: 1, + max_waiting_requests: 0 } alt_srv = RpcServer.new(**opts) alt_srv.handle(SlowService) - t = Thread.new { alt_srv.run } + Thread.new { alt_srv.run } alt_srv.wait_till_running req = EchoMsg.new n = 5 # arbitrary, use as many to ensure the server pool is exceeded threads = [] - _1_failed_as_unavailable = false - n.times do |x| + one_failed_as_unavailable = false + n.times do threads << Thread.new do - cq = GRPC::Core::CompletionQueue.new stub = SlowStub.new(@host, **@client_opts) begin stub.an_rpc(req) rescue GRPC::BadStatus => e - _1_failed_as_unavailable = e.code == StatusCodes::UNAVAILABLE + one_failed_as_unavailable = e.code == StatusCodes::UNAVAILABLE end end end - threads.each { |t| t.join } + threads.each(&:join) alt_srv.stop - expect(_1_failed_as_unavailable).to be(true) + expect(one_failed_as_unavailable).to be(true) end - end - end - end |