diff options
Diffstat (limited to 'src/ruby/spec')
-rw-r--r-- | src/ruby/spec/call_spec.rb | 8 | ||||
-rw-r--r-- | src/ruby/spec/generic/active_call_spec.rb | 57 | ||||
-rw-r--r-- | src/ruby/spec/generic/client_stub_spec.rb | 20 | ||||
-rw-r--r-- | src/ruby/spec/generic/rpc_server_spec.rb | 9 | ||||
-rw-r--r-- | src/ruby/spec/pb/health/checker_spec.rb | 233 | ||||
-rw-r--r-- | src/ruby/spec/spec_helper.rb | 14 |
6 files changed, 320 insertions, 21 deletions
diff --git a/src/ruby/spec/call_spec.rb b/src/ruby/spec/call_spec.rb index 3c5d33ffcd..dd3c45f754 100644 --- a/src/ruby/spec/call_spec.rb +++ b/src/ruby/spec/call_spec.rb @@ -31,6 +31,14 @@ require 'grpc' include GRPC::Core::StatusCodes +describe GRPC::Core::WriteFlags do + it 'should define the known write flag values' do + m = GRPC::Core::WriteFlags + expect(m.const_get(:BUFFER_HINT)).to_not be_nil + expect(m.const_get(:NO_COMPRESS)).to_not be_nil + end +end + describe GRPC::Core::RpcErrors do before(:each) do @known_types = { diff --git a/src/ruby/spec/generic/active_call_spec.rb b/src/ruby/spec/generic/active_call_spec.rb index 0bf65ba2e9..fcd7bd082f 100644 --- a/src/ruby/spec/generic/active_call_spec.rb +++ b/src/ruby/spec/generic/active_call_spec.rb @@ -35,6 +35,7 @@ describe GRPC::ActiveCall do ActiveCall = GRPC::ActiveCall Call = GRPC::Core::Call CallOps = GRPC::Core::CallOps + WriteFlags = GRPC::Core::WriteFlags before(:each) do @pass_through = proc { |x| x } @@ -57,7 +58,7 @@ describe GRPC::ActiveCall do describe 'restricted view methods' do before(:each) do call = make_test_call - md_tag = ActiveCall.client_invoke(call, @client_queue, deadline) + md_tag = ActiveCall.client_invoke(call, @client_queue) @client_call = ActiveCall.new(call, @client_queue, @pass_through, @pass_through, deadline, metadata_tag: md_tag) @@ -87,7 +88,7 @@ describe GRPC::ActiveCall do describe '#remote_send' do it 'allows a client to send a payload to the server' do call = make_test_call - md_tag = ActiveCall.client_invoke(call, @client_queue, deadline) + md_tag = ActiveCall.client_invoke(call, @client_queue) @client_call = ActiveCall.new(call, @client_queue, @pass_through, @pass_through, deadline, metadata_tag: md_tag) @@ -111,7 +112,7 @@ describe GRPC::ActiveCall do it 'marshals the payload using the marshal func' do call = make_test_call - ActiveCall.client_invoke(call, @client_queue, deadline) + ActiveCall.client_invoke(call, @client_queue) marshal = proc { |x| 'marshalled:' + x } client_call = ActiveCall.new(call, @client_queue, marshal, @pass_through, deadline) @@ -129,13 +130,37 @@ describe GRPC::ActiveCall do @pass_through, deadline) expect(server_call.remote_read).to eq('marshalled:' + msg) end + + TEST_WRITE_FLAGS = [WriteFlags::BUFFER_HINT, WriteFlags::NO_COMPRESS] + TEST_WRITE_FLAGS.each do |f| + it "successfully makes calls with write_flag set to #{f}" do + call = make_test_call + ActiveCall.client_invoke(call, @client_queue) + marshal = proc { |x| 'marshalled:' + x } + client_call = ActiveCall.new(call, @client_queue, marshal, + @pass_through, deadline) + msg = 'message is a string' + client_call.write_flag = f + client_call.remote_send(msg) + + # confirm that the message was marshalled + recvd_rpc = @server.request_call(@server_queue, @server_tag, deadline) + recvd_call = recvd_rpc.call + server_ops = { + CallOps::SEND_INITIAL_METADATA => nil + } + recvd_call.run_batch(@server_queue, @server_tag, deadline, server_ops) + server_call = ActiveCall.new(recvd_call, @server_queue, @pass_through, + @pass_through, deadline) + expect(server_call.remote_read).to eq('marshalled:' + msg) + end + end end describe '#client_invoke' do it 'sends keywords as metadata to the server when the are present' do call = make_test_call - ActiveCall.client_invoke(call, @client_queue, deadline, - k1: 'v1', k2: 'v2') + ActiveCall.client_invoke(call, @client_queue, k1: 'v1', k2: 'v2') recvd_rpc = @server.request_call(@server_queue, @server_tag, deadline) recvd_call = recvd_rpc.call expect(recvd_call).to_not be_nil @@ -148,7 +173,7 @@ describe GRPC::ActiveCall do describe '#remote_read' do it 'reads the response sent by a server' do call = make_test_call - md_tag = ActiveCall.client_invoke(call, @client_queue, deadline) + md_tag = ActiveCall.client_invoke(call, @client_queue) client_call = ActiveCall.new(call, @client_queue, @pass_through, @pass_through, deadline, metadata_tag: md_tag) @@ -161,7 +186,7 @@ describe GRPC::ActiveCall do it 'saves no metadata when the server adds no metadata' do call = make_test_call - md_tag = ActiveCall.client_invoke(call, @client_queue, deadline) + md_tag = ActiveCall.client_invoke(call, @client_queue) client_call = ActiveCall.new(call, @client_queue, @pass_through, @pass_through, deadline, metadata_tag: md_tag) @@ -176,7 +201,7 @@ describe GRPC::ActiveCall do it 'saves metadata add by the server' do call = make_test_call - md_tag = ActiveCall.client_invoke(call, @client_queue, deadline) + md_tag = ActiveCall.client_invoke(call, @client_queue) client_call = ActiveCall.new(call, @client_queue, @pass_through, @pass_through, deadline, metadata_tag: md_tag) @@ -192,7 +217,7 @@ describe GRPC::ActiveCall do it 'get a nil msg before a status when an OK status is sent' do call = make_test_call - md_tag = ActiveCall.client_invoke(call, @client_queue, deadline) + md_tag = ActiveCall.client_invoke(call, @client_queue) client_call = ActiveCall.new(call, @client_queue, @pass_through, @pass_through, deadline, metadata_tag: md_tag) @@ -209,7 +234,7 @@ describe GRPC::ActiveCall do it 'unmarshals the response using the unmarshal func' do call = make_test_call - md_tag = ActiveCall.client_invoke(call, @client_queue, deadline) + md_tag = ActiveCall.client_invoke(call, @client_queue) unmarshal = proc { |x| 'unmarshalled:' + x } client_call = ActiveCall.new(call, @client_queue, @pass_through, unmarshal, deadline, @@ -234,7 +259,7 @@ describe GRPC::ActiveCall do it 'the returns an enumerator that can read n responses' do call = make_test_call - md_tag = ActiveCall.client_invoke(call, @client_queue, deadline) + md_tag = ActiveCall.client_invoke(call, @client_queue) client_call = ActiveCall.new(call, @client_queue, @pass_through, @pass_through, deadline, metadata_tag: md_tag) @@ -252,7 +277,7 @@ describe GRPC::ActiveCall do it 'the returns an enumerator that stops after an OK Status' do call = make_test_call - md_tag = ActiveCall.client_invoke(call, @client_queue, deadline) + md_tag = ActiveCall.client_invoke(call, @client_queue) client_call = ActiveCall.new(call, @client_queue, @pass_through, @pass_through, deadline, metadata_tag: md_tag) @@ -262,7 +287,7 @@ describe GRPC::ActiveCall do client_call.writes_done(false) server_call = expect_server_to_receive(msg) e = client_call.each_remote_read - n = 3 # arbitrary value > 1 + n = 3 # arbitrary value > 1 n.times do server_call.remote_send(reply) expect(e.next).to eq(reply) @@ -275,7 +300,7 @@ describe GRPC::ActiveCall do describe '#writes_done' do it 'finishes ok if the server sends a status response' do call = make_test_call - md_tag = ActiveCall.client_invoke(call, @client_queue, deadline) + md_tag = ActiveCall.client_invoke(call, @client_queue) client_call = ActiveCall.new(call, @client_queue, @pass_through, @pass_through, deadline, metadata_tag: md_tag) @@ -291,7 +316,7 @@ describe GRPC::ActiveCall do it 'finishes ok if the server sends an early status response' do call = make_test_call - md_tag = ActiveCall.client_invoke(call, @client_queue, deadline) + md_tag = ActiveCall.client_invoke(call, @client_queue) client_call = ActiveCall.new(call, @client_queue, @pass_through, @pass_through, deadline, metadata_tag: md_tag) @@ -307,7 +332,7 @@ describe GRPC::ActiveCall do it 'finishes ok if writes_done is true' do call = make_test_call - md_tag = ActiveCall.client_invoke(call, @client_queue, deadline) + md_tag = ActiveCall.client_invoke(call, @client_queue) client_call = ActiveCall.new(call, @client_queue, @pass_through, @pass_through, deadline, metadata_tag: md_tag) diff --git a/src/ruby/spec/generic/client_stub_spec.rb b/src/ruby/spec/generic/client_stub_spec.rb index 68d4b11790..edcc962a7d 100644 --- a/src/ruby/spec/generic/client_stub_spec.rb +++ b/src/ruby/spec/generic/client_stub_spec.rb @@ -408,6 +408,26 @@ describe 'ClientStub' do it_behaves_like 'bidi streaming' end + + describe 'without enough time to run' do + before(:each) do + @sent_msgs = Array.new(3) { |i| 'msg_' + (i + 1).to_s } + @replys = Array.new(3) { |i| 'reply_' + (i + 1).to_s } + server_port = create_test_server + @host = "localhost:#{server_port}" + end + + it 'should fail with DeadlineExceeded', bidi: true do + @server.start + stub = GRPC::ClientStub.new(@host, @cq) + blk = proc do + e = stub.bidi_streamer(@method, @sent_msgs, noop, noop, + timeout: 0.001) + e.collect { |r| r } + end + expect(&blk).to raise_error GRPC::BadStatus, /Deadline Exceeded/ + end + end end def run_server_streamer(expected_input, replys, status, **kw) diff --git a/src/ruby/spec/generic/rpc_server_spec.rb b/src/ruby/spec/generic/rpc_server_spec.rb index 0326f6e894..1295fd7fdd 100644 --- a/src/ruby/spec/generic/rpc_server_spec.rb +++ b/src/ruby/spec/generic/rpc_server_spec.rb @@ -396,8 +396,9 @@ describe GRPC::RpcServer do @srv.wait_till_running req = EchoMsg.new stub = SlowStub.new(@host, **client_opts) - deadline = service.delay + 1.0 # wait for long enough - expect(stub.an_rpc(req, deadline, k1: 'v1', k2: 'v2')).to be_a(EchoMsg) + timeout = service.delay + 1.0 # wait for long enough + resp = stub.an_rpc(req, timeout: timeout, k1: 'v1', k2: 'v2') + expect(resp).to be_a(EchoMsg) wanted_md = [{ 'k1' => 'v1', 'k2' => 'v2' }] check_md(wanted_md, service.received_md) @srv.stop @@ -411,8 +412,8 @@ describe GRPC::RpcServer do @srv.wait_till_running req = EchoMsg.new stub = SlowStub.new(@host, **client_opts) - deadline = 0.1 # too short for SlowService to respond - blk = proc { stub.an_rpc(req, deadline, k1: 'v1', k2: 'v2') } + timeout = 0.1 # too short for SlowService to respond + blk = proc { stub.an_rpc(req, timeout: timeout, k1: 'v1', k2: 'v2') } expect(&blk).to raise_error GRPC::BadStatus wanted_md = [] expect(service.received_md).to eq(wanted_md) diff --git a/src/ruby/spec/pb/health/checker_spec.rb b/src/ruby/spec/pb/health/checker_spec.rb new file mode 100644 index 0000000000..6999a69105 --- /dev/null +++ b/src/ruby/spec/pb/health/checker_spec.rb @@ -0,0 +1,233 @@ +# Copyright 2015, Google Inc. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following disclaimer +# in the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Google Inc. nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +require 'grpc' +require 'grpc/health/v1alpha/health' +require 'grpc/health/checker' +require 'open3' + +def can_run_codegen_check + system('which grpc_ruby_plugin') && system('which protoc') +end + +describe 'Health protobuf code generation' do + context 'the health service file used by grpc/health/checker' do + if !can_run_codegen_check + skip 'protoc || grpc_ruby_plugin missing, cannot verify health code-gen' + else + it 'should already be loaded indirectly i.e, used by the other specs' do + expect(require('grpc/health/v1alpha/health_services')).to be(false) + end + + it 'should have the same content as created by code generation' do + root_dir = File.dirname( + File.dirname(File.dirname(File.dirname(__FILE__)))) + pb_dir = File.join(root_dir, 'pb') + + # Get the current content + service_path = File.join(pb_dir, 'grpc', 'health', 'v1alpha', + 'health_services.rb') + want = nil + File.open(service_path) { |f| want = f.read } + + # Regenerate it + plugin, = Open3.capture2('which', 'grpc_ruby_plugin') + plugin = plugin.strip + got = nil + Dir.mktmpdir do |tmp_dir| + gen_out = File.join(tmp_dir, 'grpc', 'health', 'v1alpha', + 'health_services.rb') + pid = spawn( + 'protoc', + '-I.', + 'grpc/health/v1alpha/health.proto', + "--grpc_out=#{tmp_dir}", + "--plugin=protoc-gen-grpc=#{plugin}", + chdir: pb_dir) + Process.wait(pid) + File.open(gen_out) { |f| got = f.read } + end + expect(got).to eq(want) + end + end + end +end + +describe Grpc::Health::Checker do + StatusCodes = GRPC::Core::StatusCodes + ServingStatus = Grpc::Health::V1alpha::HealthCheckResponse::ServingStatus + HCResp = Grpc::Health::V1alpha::HealthCheckResponse + HCReq = Grpc::Health::V1alpha::HealthCheckRequest + success_tests = + [ + { + desc: 'neither host or service are specified', + host: '', + service: '' + }, { + desc: 'only the host is specified', + host: 'test-fake-host', + service: '' + }, { + desc: 'the host and service are specified', + host: 'test-fake-host', + service: 'fake-service-1' + }, { + desc: 'only the service is specified', + host: '', + service: 'fake-service-2' + } + ] + + context 'initialization' do + it 'can be constructed with no args' do + expect(subject).to_not be(nil) + end + end + + context 'method `add_status` and `check`' do + success_tests.each do |t| + it "should succeed when #{t[:desc]}" do + subject.add_status(t[:host], t[:service], ServingStatus::NOT_SERVING) + got = subject.check(HCReq.new(host: t[:host], service: t[:service]), + nil) + want = HCResp.new(status: ServingStatus::NOT_SERVING) + expect(got).to eq(want) + end + end + end + + context 'method `check`' do + success_tests.each do |t| + it "should fail with NOT_FOUND when #{t[:desc]}" do + blk = proc do + subject.check(HCReq.new(host: t[:host], service: t[:service]), nil) + end + expected_msg = /#{StatusCodes::NOT_FOUND}/ + expect(&blk).to raise_error GRPC::BadStatus, expected_msg + end + end + end + + context 'method `clear_status`' do + success_tests.each do |t| + it "should fail after clearing status when #{t[:desc]}" do + subject.add_status(t[:host], t[:service], ServingStatus::NOT_SERVING) + got = subject.check(HCReq.new(host: t[:host], service: t[:service]), + nil) + want = HCResp.new(status: ServingStatus::NOT_SERVING) + expect(got).to eq(want) + + subject.clear_status(t[:host], t[:service]) + blk = proc do + subject.check(HCReq.new(host: t[:host], service: t[:service]), + nil) + end + expected_msg = /#{StatusCodes::NOT_FOUND}/ + expect(&blk).to raise_error GRPC::BadStatus, expected_msg + end + end + end + + context 'method `clear_all`' do + it 'should return NOT_FOUND after being invoked' do + success_tests.each do |t| + subject.add_status(t[:host], t[:service], ServingStatus::NOT_SERVING) + got = subject.check(HCReq.new(host: t[:host], service: t[:service]), + nil) + want = HCResp.new(status: ServingStatus::NOT_SERVING) + expect(got).to eq(want) + end + + subject.clear_all + + success_tests.each do |t| + blk = proc do + subject.check(HCReq.new(host: t[:host], service: t[:service]), nil) + end + expected_msg = /#{StatusCodes::NOT_FOUND}/ + expect(&blk).to raise_error GRPC::BadStatus, expected_msg + end + end + end + + describe 'running on RpcServer' do + RpcServer = GRPC::RpcServer + StatusCodes = GRPC::Core::StatusCodes + CheckerStub = Grpc::Health::Checker.rpc_stub_class + + before(:each) do + @server_queue = GRPC::Core::CompletionQueue.new + server_host = '0.0.0.0:0' + @server = GRPC::Core::Server.new(@server_queue, nil) + server_port = @server.add_http2_port(server_host) + @host = "localhost:#{server_port}" + @ch = GRPC::Core::Channel.new(@host, nil) + @client_opts = { channel_override: @ch } + server_opts = { + server_override: @server, + completion_queue_override: @server_queue, + poll_period: 1 + } + @srv = RpcServer.new(**server_opts) + end + + after(:each) do + @srv.stop + end + + it 'should receive the correct status', server: true do + @srv.handle(subject) + subject.add_status('', '', ServingStatus::NOT_SERVING) + t = Thread.new { @srv.run } + @srv.wait_till_running + + stub = CheckerStub.new(@host, **@client_opts) + got = stub.check(HCReq.new) + want = HCResp.new(status: ServingStatus::NOT_SERVING) + expect(got).to eq(want) + @srv.stop + t.join + end + + it 'should fail on unknown services', server: true do + @srv.handle(subject) + t = Thread.new { @srv.run } + @srv.wait_till_running + blk = proc do + stub = CheckerStub.new(@host, **@client_opts) + stub.check(HCReq.new(host: 'unknown', service: 'unknown')) + end + expected_msg = /#{StatusCodes::NOT_FOUND}/ + expect(&blk).to raise_error GRPC::BadStatus, expected_msg + @srv.stop + t.join + end + end +end diff --git a/src/ruby/spec/spec_helper.rb b/src/ruby/spec/spec_helper.rb index 270d2e97d3..c891c1bf5e 100644 --- a/src/ruby/spec/spec_helper.rb +++ b/src/ruby/spec/spec_helper.rb @@ -47,11 +47,23 @@ require 'rspec' require 'logging' require 'rspec/logging_helper' +# GRPC is the general RPC module +# +# Configure its logging for fine-grained log control during test runs +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 + # Configure RSpec to capture log messages for each test. The output from the # logs will be stored in the @log_output variable. It is a StringIO instance. RSpec.configure do |config| include RSpec::LoggingHelper - config.capture_log_messages + config.capture_log_messages # comment this out to see logs during test runs end RSpec::Expectations.configuration.warn_about_potential_false_positives = false |