diff options
Diffstat (limited to 'src/ruby')
36 files changed, 1497 insertions, 682 deletions
diff --git a/src/ruby/.rspec b/src/ruby/.rspec index cd7c5fb5b2..2320752db4 100755 --- a/src/ruby/.rspec +++ b/src/ruby/.rspec @@ -1,4 +1,5 @@ -I. +-Ipb --require spec_helper --format documentation --color diff --git a/src/ruby/.rubocop.yml b/src/ruby/.rubocop.yml index 47e382afa7..312bdca384 100644 --- a/src/ruby/.rubocop.yml +++ b/src/ruby/.rubocop.yml @@ -5,6 +5,7 @@ inherit_from: .rubocop_todo.yml AllCops: Exclude: - 'bin/apis/**/*' - - 'bin/interop/test/**/*' - 'bin/math.rb' - 'bin/math_services.rb' + - 'pb/grpc/health/v1alpha/*' + - 'pb/test/**/*' diff --git a/src/ruby/README.md b/src/ruby/README.md index 4b657c0bd4..f8902e34c5 100644 --- a/src/ruby/README.md +++ b/src/ruby/README.md @@ -12,12 +12,36 @@ PREREQUISITES ------------- - Ruby 2.x. The gRPC API uses keyword args. -- [homebrew][] on Mac OS X, [linuxbrew][] on Linux. These simplify the installation of the gRPC C core. +- [homebrew][] on Mac OS X. These simplify the installation of the gRPC C core. INSTALLATION --------------- -On Mac OS X, install [homebrew][]. On Linux, install [linuxbrew][]. -Run the following command to install gRPC Ruby. + +**Linux (Debian):** + +Add [Debian unstable][] to your `sources.list` file. Example: + +```sh +echo "deb http://ftp.us.debian.org/debian unstable main contrib non-free" | \ +sudo tee -a /etc/apt/sources.list +``` + +Install the gRPC Debian package + +```sh +sudo apt-get update +sudo apt-get install libgrpc-dev +``` + +Install the gRPC Ruby package + +```sh +gem install grpc +``` + +**Mac OS X** + +Install [homebrew][]. Run the following command to install gRPC Ruby. ```sh $ curl -fsSL https://goo.gl/getgrpc | bash -s ruby ``` @@ -26,12 +50,6 @@ This will download and run the [gRPC install script][], then install the latest BUILD FROM SOURCE --------------------- - Clone this repository -- Build the gRPC C core -E.g, from the root of the gRPC [Git repository](https://github.com/google/grpc) -```sh -$ cd ../.. -$ make && sudo make install -``` - Install Ruby 2.x. Consider doing this with [RVM](http://rvm.io), it's a nice way of controlling the exact ruby version that's used. @@ -77,8 +95,8 @@ Directory structure is the layout for [ruby extensions][] GRPC.logger.info("Answer: #{resp.inspect}") ``` [homebrew]:http://brew.sh -[linuxbrew]:https://github.com/Homebrew/linuxbrew#installation [gRPC install script]:https://raw.githubusercontent.com/grpc/homebrew-grpc/master/scripts/install [ruby extensions]:http://guides.rubygems.org/gems-with-extensions/ [rubydoc]: http://www.rubydoc.info/gems/grpc [grpc.io]: http://www.grpc.io/docs/installation/ruby.html +[Debian unstable]:https://www.debian.org/releases/sid/ diff --git a/src/ruby/Rakefile b/src/ruby/Rakefile index 02af9a84b8..cc7832b12d 100755 --- a/src/ruby/Rakefile +++ b/src/ruby/Rakefile @@ -20,7 +20,8 @@ SPEC_SUITES = [ { id: :bidi, title: 'bidi tests', dir: %w(spec/generic), tag: 'bidi' }, { id: :server, title: 'rpc server thread tests', dir: %w(spec/generic), - tag: 'server' } + tag: 'server' }, + { id: :pb, title: 'protobuf service tests', dir: %w(spec/pb) } ] namespace :suite do SPEC_SUITES.each do |suite| @@ -50,7 +51,8 @@ task 'suite:wrapper' => [:compile, :rubocop] task 'suite:idiomatic' => 'suite:wrapper' task 'suite:bidi' => 'suite:wrapper' task 'suite:server' => 'suite:wrapper' +task 'suite:pb' => 'suite:server' desc 'Compiles the gRPC extension then runs all the tests' -task all: ['suite:idiomatic', 'suite:bidi', 'suite:server'] +task all: ['suite:idiomatic', 'suite:bidi', 'suite:pb', 'suite:server'] task default: :all diff --git a/src/ruby/bin/interop/test/cpp/interop/test.rb b/src/ruby/bin/grpc_ruby_interop_client index 5948b50eaa..e79fd33aa5 100644..100755 --- a/src/ruby/bin/interop/test/cpp/interop/test.rb +++ b/src/ruby/bin/grpc_ruby_interop_client @@ -1,3 +1,5 @@ +#!/usr/bin/env ruby + # Copyright 2015, Google Inc. # All rights reserved. # @@ -27,17 +29,5 @@ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -# Generated by the protocol buffer compiler. DO NOT EDIT! -# source: test/cpp/interop/test.proto - -require 'google/protobuf' - -require 'test/cpp/interop/empty' -require 'test/cpp/interop/messages' -Google::Protobuf::DescriptorPool.generated_pool.build do -end - -module Grpc - module Testing - end -end +# Provides a gem binary entry point for the interop client. +require 'test/client' diff --git a/src/ruby/bin/interop/test/cpp/interop/empty.rb b/src/ruby/bin/grpc_ruby_interop_server index 3579fa5ded..656a5f7c99 100644..100755 --- a/src/ruby/bin/interop/test/cpp/interop/empty.rb +++ b/src/ruby/bin/grpc_ruby_interop_server @@ -1,3 +1,5 @@ +#!/usr/bin/env ruby + # Copyright 2015, Google Inc. # All rights reserved. # @@ -27,18 +29,5 @@ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -# Generated by the protocol buffer compiler. DO NOT EDIT! -# source: test/cpp/interop/empty.proto - -require 'google/protobuf' - -Google::Protobuf::DescriptorPool.generated_pool.build do - add_message "grpc.testing.Empty" do - end -end - -module Grpc - module Testing - Empty = Google::Protobuf::DescriptorPool.generated_pool.lookup("grpc.testing.Empty").msgclass - end -end +# Provides a gem binary entry point for the interop server +require 'test/server' diff --git a/src/ruby/bin/interop/README.md b/src/ruby/bin/interop/README.md deleted file mode 100644 index 84fc663620..0000000000 --- a/src/ruby/bin/interop/README.md +++ /dev/null @@ -1,8 +0,0 @@ -Interop test protos -=================== - -These ruby classes were generated with protoc v3, using grpc's ruby compiler -plugin. - -- As of 2015/01 protoc v3 is available in the -[google-protobuf](https://github.com/google/protobuf) repo diff --git a/src/ruby/bin/interop/interop_client.rb b/src/ruby/bin/interop/interop_client.rb index da4caa842b..239083f37f 100755 --- a/src/ruby/bin/interop/interop_client.rb +++ b/src/ruby/bin/interop/interop_client.rb @@ -29,6 +29,12 @@ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# ####################################################################### +# DEPRECATED: The behaviour in this file has been moved to pb/test/client.rb +# +# This file remains to support existing tools and scripts that use it. +# ###################################################################### +# # interop_client is a testing tool that accesses a gRPC interop testing # server and runs a test on it. # @@ -39,339 +45,7 @@ # --test_case=<testcase_name> this_dir = File.expand_path(File.dirname(__FILE__)) -lib_dir = File.join(File.dirname(File.dirname(this_dir)), 'lib') -$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir) -$LOAD_PATH.unshift(this_dir) unless $LOAD_PATH.include?(this_dir) - -require 'optparse' -require 'minitest' -require 'minitest/assertions' - -require 'grpc' -require 'googleauth' -require 'google/protobuf' - -require 'test/cpp/interop/test_services' -require 'test/cpp/interop/messages' -require 'test/cpp/interop/empty' - -require 'signet/ssl_config' - -AUTH_ENV = Google::Auth::CredentialsLoader::ENV_VAR - -# loads the certificates used to access the test server securely. -def load_test_certs - this_dir = File.expand_path(File.dirname(__FILE__)) - data_dir = File.join(File.dirname(File.dirname(this_dir)), 'spec/testdata') - files = ['ca.pem', 'server1.key', 'server1.pem'] - files.map { |f| File.open(File.join(data_dir, f)).read } -end - -# loads the certificates used to access the test server securely. -def load_prod_cert - fail 'could not find a production cert' if ENV['SSL_CERT_FILE'].nil? - GRPC.logger.info("loading prod certs from #{ENV['SSL_CERT_FILE']}") - File.open(ENV['SSL_CERT_FILE']).read -end - -# creates SSL Credentials from the test certificates. -def test_creds - certs = load_test_certs - GRPC::Core::Credentials.new(certs[0]) -end - -# creates SSL Credentials from the production certificates. -def prod_creds - cert_text = load_prod_cert - GRPC::Core::Credentials.new(cert_text) -end - -# creates the SSL Credentials. -def ssl_creds(use_test_ca) - return test_creds if use_test_ca - prod_creds -end - -# creates a test stub that accesses host:port securely. -def create_stub(opts) - address = "#{opts.host}:#{opts.port}" - if opts.secure - stub_opts = { - :creds => ssl_creds(opts.use_test_ca), - GRPC::Core::Channel::SSL_TARGET => opts.host_override - } - - # Add service account creds if specified - wants_creds = %w(all compute_engine_creds service_account_creds) - if wants_creds.include?(opts.test_case) - unless opts.oauth_scope.nil? - auth_creds = Google::Auth.get_application_default(opts.oauth_scope) - stub_opts[:update_metadata] = auth_creds.updater_proc - end - end - - if opts.test_case == 'jwt_token_creds' # don't use a scope - auth_creds = Google::Auth.get_application_default - stub_opts[:update_metadata] = auth_creds.updater_proc - end - - GRPC.logger.info("... connecting securely to #{address}") - Grpc::Testing::TestService::Stub.new(address, **stub_opts) - else - GRPC.logger.info("... connecting insecurely to #{address}") - Grpc::Testing::TestService::Stub.new(address) - end -end - -# produces a string of null chars (\0) of length l. -def nulls(l) - fail 'requires #{l} to be +ve' if l < 0 - [].pack('x' * l).force_encoding('utf-8') -end - -# a PingPongPlayer implements the ping pong bidi test. -class PingPongPlayer - include Minitest::Assertions - include Grpc::Testing - include Grpc::Testing::PayloadType - attr_accessor :assertions # required by Minitest::Assertions - attr_accessor :queue - attr_accessor :canceller_op - - # reqs is the enumerator over the requests - def initialize(msg_sizes) - @queue = Queue.new - @msg_sizes = msg_sizes - @assertions = 0 # required by Minitest::Assertions - @canceller_op = nil # used to cancel after the first response - end - - def each_item - return enum_for(:each_item) unless block_given? - req_cls, p_cls = StreamingOutputCallRequest, ResponseParameters # short - count = 0 - @msg_sizes.each do |m| - req_size, resp_size = m - req = req_cls.new(payload: Payload.new(body: nulls(req_size)), - response_type: :COMPRESSABLE, - response_parameters: [p_cls.new(size: resp_size)]) - yield req - resp = @queue.pop - assert_equal(:COMPRESSABLE, resp.payload.type, 'payload type is wrong') - assert_equal(resp_size, resp.payload.body.length, - "payload body #{count} has the wrong length") - p "OK: ping_pong #{count}" - count += 1 - unless @canceller_op.nil? - canceller_op.cancel - break - end - end - end -end - -# defines methods corresponding to each interop test case. -class NamedTests - include Minitest::Assertions - include Grpc::Testing - include Grpc::Testing::PayloadType - attr_accessor :assertions # required by Minitest::Assertions - - def initialize(stub, args) - @assertions = 0 # required by Minitest::Assertions - @stub = stub - @args = args - end - - def empty_unary - resp = @stub.empty_call(Empty.new) - assert resp.is_a?(Empty), 'empty_unary: invalid response' - p 'OK: empty_unary' - end - - def large_unary - perform_large_unary - p 'OK: large_unary' - end - - def service_account_creds - # ignore this test if the oauth options are not set - if @args.oauth_scope.nil? - p 'NOT RUN: service_account_creds; no service_account settings' - return - end - json_key = File.read(ENV[AUTH_ENV]) - wanted_email = MultiJson.load(json_key)['client_email'] - resp = perform_large_unary(fill_username: true, - fill_oauth_scope: true) - assert_equal(wanted_email, resp.username, - 'service_account_creds: incorrect username') - assert(@args.oauth_scope.include?(resp.oauth_scope), - 'service_account_creds: incorrect oauth_scope') - p 'OK: service_account_creds' - end - - def jwt_token_creds - json_key = File.read(ENV[AUTH_ENV]) - wanted_email = MultiJson.load(json_key)['client_email'] - resp = perform_large_unary(fill_username: true) - assert_equal(wanted_email, resp.username, - 'service_account_creds: incorrect username') - p 'OK: jwt_token_creds' - end - - def compute_engine_creds - resp = perform_large_unary(fill_username: true, - fill_oauth_scope: true) - assert_equal(@args.default_service_account, resp.username, - 'compute_engine_creds: incorrect username') - p 'OK: compute_engine_creds' - end - - def client_streaming - msg_sizes = [27_182, 8, 1828, 45_904] - wanted_aggregate_size = 74_922 - reqs = msg_sizes.map do |x| - req = Payload.new(body: nulls(x)) - StreamingInputCallRequest.new(payload: req) - end - resp = @stub.streaming_input_call(reqs) - assert_equal(wanted_aggregate_size, resp.aggregated_payload_size, - 'client_streaming: aggregate payload size is incorrect') - p 'OK: client_streaming' - end - - def server_streaming - msg_sizes = [31_415, 9, 2653, 58_979] - response_spec = msg_sizes.map { |s| ResponseParameters.new(size: s) } - req = StreamingOutputCallRequest.new(response_type: :COMPRESSABLE, - response_parameters: response_spec) - resps = @stub.streaming_output_call(req) - resps.each_with_index do |r, i| - assert i < msg_sizes.length, 'too many responses' - assert_equal(:COMPRESSABLE, r.payload.type, - 'payload type is wrong') - assert_equal(msg_sizes[i], r.payload.body.length, - 'payload body #{i} has the wrong length') - end - p 'OK: server_streaming' - end - - def ping_pong - msg_sizes = [[27_182, 31_415], [8, 9], [1828, 2653], [45_904, 58_979]] - ppp = PingPongPlayer.new(msg_sizes) - resps = @stub.full_duplex_call(ppp.each_item) - resps.each { |r| ppp.queue.push(r) } - p 'OK: ping_pong' - end - - def cancel_after_begin - msg_sizes = [27_182, 8, 1828, 45_904] - reqs = msg_sizes.map do |x| - req = Payload.new(body: nulls(x)) - StreamingInputCallRequest.new(payload: req) - end - op = @stub.streaming_input_call(reqs, return_op: true) - op.cancel - assert_raises(GRPC::Cancelled) { op.execute } - assert(op.cancelled, 'call operation should be CANCELLED') - p 'OK: cancel_after_begin' - end - - def cancel_after_first_response - msg_sizes = [[27_182, 31_415], [8, 9], [1828, 2653], [45_904, 58_979]] - ppp = PingPongPlayer.new(msg_sizes) - op = @stub.full_duplex_call(ppp.each_item, return_op: true) - ppp.canceller_op = op # causes ppp to cancel after the 1st message - op.execute.each { |r| ppp.queue.push(r) } - op.wait - assert(op.cancelled, 'call operation was not CANCELLED') - p 'OK: cancel_after_first_response' - end - - def all - all_methods = NamedTests.instance_methods(false).map(&:to_s) - all_methods.each do |m| - next if m == 'all' || m.start_with?('assert') - p "TESTCASE: #{m}" - method(m).call - end - end - - private - - def perform_large_unary(fill_username: false, fill_oauth_scope: false) - req_size, wanted_response_size = 271_828, 314_159 - payload = Payload.new(type: :COMPRESSABLE, body: nulls(req_size)) - req = SimpleRequest.new(response_type: :COMPRESSABLE, - response_size: wanted_response_size, - payload: payload) - req.fill_username = fill_username - req.fill_oauth_scope = fill_oauth_scope - resp = @stub.unary_call(req) - assert_equal(:COMPRESSABLE, resp.payload.type, - 'large_unary: payload had the wrong type') - assert_equal(wanted_response_size, resp.payload.body.length, - 'large_unary: payload had the wrong length') - assert_equal(nulls(wanted_response_size), resp.payload.body, - 'large_unary: payload content is invalid') - resp - end -end - -# Args is used to hold the command line info. -Args = Struct.new(:default_service_account, :host, :host_override, - :oauth_scope, :port, :secure, :test_case, - :use_test_ca) - -# validates the the command line options, returning them as a Hash. -def parse_args - args = Args.new - args.host_override = 'foo.test.google.fr' - OptionParser.new do |opts| - opts.on('--oauth_scope scope', - 'Scope for OAuth tokens') { |v| args['oauth_scope'] = v } - opts.on('--server_host SERVER_HOST', 'server hostname') do |v| - args['host'] = v - end - opts.on('--default_service_account email_address', - 'email address of the default service account') do |v| - args['default_service_account'] = v - end - opts.on('--server_host_override HOST_OVERRIDE', - 'override host via a HTTP header') do |v| - args['host_override'] = v - end - opts.on('--server_port SERVER_PORT', 'server port') { |v| args['port'] = v } - # instance_methods(false) gives only the methods defined in that class - test_cases = NamedTests.instance_methods(false).map(&:to_s) - test_case_list = test_cases.join(',') - opts.on('--test_case CODE', test_cases, {}, 'select a test_case', - " (#{test_case_list})") { |v| args['test_case'] = v } - opts.on('-s', '--use_tls', 'require a secure connection?') do |v| - args['secure'] = v - end - opts.on('-t', '--use_test_ca', - 'if secure, use the test certificate?') do |v| - args['use_test_ca'] = v - end - end.parse! - _check_args(args) -end - -def _check_args(args) - %w(host port test_case).each do |a| - if args[a].nil? - fail(OptionParser::MissingArgument, "please specify --#{arg}") - end - end - args -end - -def main - opts = parse_args - stub = create_stub(opts) - NamedTests.new(stub, opts).method(opts['test_case']).call -end +pb_dir = File.join(File.dirname(File.dirname(this_dir)), 'pb') +$LOAD_PATH.unshift(pb_dir) unless $LOAD_PATH.include?(pb_dir) -main +require 'test/client' diff --git a/src/ruby/bin/interop/interop_server.rb b/src/ruby/bin/interop/interop_server.rb index 2ba8d2c19e..c6b0d00ec6 100755 --- a/src/ruby/bin/interop/interop_server.rb +++ b/src/ruby/bin/interop/interop_server.rb @@ -29,6 +29,12 @@ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# ####################################################################### +# DEPRECATED: The behaviour in this file has been moved to pb/test/server.rb +# +# This file remains to support existing tools and scripts that use it. +# ###################################################################### +# # interop_server is a Testing app that runs a gRPC interop testing server. # # It helps validate interoperation b/w gRPC in different environments @@ -38,157 +44,7 @@ # Usage: $ path/to/interop_server.rb --port this_dir = File.expand_path(File.dirname(__FILE__)) -lib_dir = File.join(File.dirname(File.dirname(this_dir)), 'lib') -$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir) -$LOAD_PATH.unshift(this_dir) unless $LOAD_PATH.include?(this_dir) - -require 'forwardable' -require 'optparse' - -require 'grpc' - -require 'test/cpp/interop/test_services' -require 'test/cpp/interop/messages' -require 'test/cpp/interop/empty' - -# loads the certificates by the test server. -def load_test_certs - this_dir = File.expand_path(File.dirname(__FILE__)) - data_dir = File.join(File.dirname(File.dirname(this_dir)), 'spec/testdata') - files = ['ca.pem', 'server1.key', 'server1.pem'] - files.map { |f| File.open(File.join(data_dir, f)).read } -end - -# creates a ServerCredentials from the test certificates. -def test_server_creds - certs = load_test_certs - GRPC::Core::ServerCredentials.new(nil, certs[1], certs[2]) -end - -# produces a string of null chars (\0) of length l. -def nulls(l) - fail 'requires #{l} to be +ve' if l < 0 - [].pack('x' * l).force_encoding('utf-8') -end - -# A EnumeratorQueue wraps a Queue yielding the items added to it via each_item. -class EnumeratorQueue - extend Forwardable - def_delegators :@q, :push - - def initialize(sentinel) - @q = Queue.new - @sentinel = sentinel - end - - def each_item - return enum_for(:each_item) unless block_given? - loop do - r = @q.pop - break if r.equal?(@sentinel) - fail r if r.is_a? Exception - yield r - end - end -end - -# A runnable implementation of the schema-specified testing service, with each -# service method implemented as required by the interop testing spec. -class TestTarget < Grpc::Testing::TestService::Service - include Grpc::Testing - include Grpc::Testing::PayloadType - - def empty_call(_empty, _call) - Empty.new - end - - def unary_call(simple_req, _call) - req_size = simple_req.response_size - SimpleResponse.new(payload: Payload.new(type: :COMPRESSABLE, - body: nulls(req_size))) - end - - def streaming_input_call(call) - sizes = call.each_remote_read.map { |x| x.payload.body.length } - sum = sizes.inject { |s, x| s + x } - StreamingInputCallResponse.new(aggregated_payload_size: sum) - end - - def streaming_output_call(req, _call) - cls = StreamingOutputCallResponse - req.response_parameters.map do |p| - cls.new(payload: Payload.new(type: req.response_type, - body: nulls(p.size))) - end - end - - def full_duplex_call(reqs) - # reqs is a lazy Enumerator of the requests sent by the client. - q = EnumeratorQueue.new(self) - cls = StreamingOutputCallResponse - Thread.new do - begin - GRPC.logger.info('interop-server: started receiving') - reqs.each do |req| - resp_size = req.response_parameters[0].size - GRPC.logger.info("read a req, response size is #{resp_size}") - resp = cls.new(payload: Payload.new(type: req.response_type, - body: nulls(resp_size))) - q.push(resp) - end - GRPC.logger.info('interop-server: finished receiving') - q.push(self) - rescue StandardError => e - GRPC.logger.info('interop-server: failed') - GRPC.logger.warn(e) - q.push(e) # share the exception with the enumerator - end - end - q.each_item - end - - def half_duplex_call(reqs) - # TODO: update with unique behaviour of the half_duplex_call if that's - # ever required by any of the tests. - full_duplex_call(reqs) - end -end - -# validates the the command line options, returning them as a Hash. -def parse_options - options = { - 'port' => nil, - 'secure' => false - } - OptionParser.new do |opts| - opts.banner = 'Usage: --port port' - opts.on('--port PORT', 'server port') do |v| - options['port'] = v - end - opts.on('-s', '--use_tls', 'require a secure connection?') do |v| - options['secure'] = v - end - end.parse! - - if options['port'].nil? - fail(OptionParser::MissingArgument, 'please specify --port') - end - options -end - -def main - opts = parse_options - host = "0.0.0.0:#{opts['port']}" - s = GRPC::RpcServer.new - if opts['secure'] - s.add_http2_port(host, test_server_creds) - GRPC.logger.info("... running securely on #{host}") - else - s.add_http2_port(host) - GRPC.logger.info("... running insecurely on #{host}") - end - s.handle(TestTarget) - s.run_till_terminated -end +pb_dir = File.join(File.dirname(File.dirname(this_dir)), 'pb') +$LOAD_PATH.unshift(pb_dir) unless $LOAD_PATH.include?(pb_dir) -main +require 'test/server' diff --git a/src/ruby/ext/grpc/rb_call.c b/src/ruby/ext/grpc/rb_call.c index 88659da535..6b5beb6f5d 100644 --- a/src/ruby/ext/grpc/rb_call.c +++ b/src/ruby/ext/grpc/rb_call.c @@ -82,6 +82,10 @@ static ID id_metadata; * received by the call and subsequently saved on it. */ static ID id_status; +/* id_write_flag is name of the attribute used to access the write_flag + * saved on the call. */ +static ID id_write_flag; + /* sym_* are the symbol for attributes of grpc_rb_sBatchResult. */ static VALUE sym_send_message; static VALUE sym_send_metadata; @@ -170,7 +174,7 @@ static VALUE grpc_rb_call_cancel(VALUE self) { grpc_call *call = NULL; grpc_call_error err; TypedData_Get_Struct(self, grpc_call, &grpc_call_data_type, call); - err = grpc_call_cancel(call); + err = grpc_call_cancel(call, NULL); if (err != GRPC_CALL_OK) { rb_raise(grpc_rb_eCallError, "cancel failed: %s (code=%d)", grpc_call_error_detail_of(err), err); @@ -240,6 +244,30 @@ static VALUE grpc_rb_call_set_metadata(VALUE self, VALUE metadata) { return rb_ivar_set(self, id_metadata, metadata); } +/* + call-seq: + write_flag = call.write_flag + + Gets the write_flag value saved the call. */ +static VALUE grpc_rb_call_get_write_flag(VALUE self) { + return rb_ivar_get(self, id_write_flag); +} + +/* + call-seq: + call.write_flag = write_flag + + Saves the write_flag on the call. */ +static VALUE grpc_rb_call_set_write_flag(VALUE self, VALUE write_flag) { + if (!NIL_P(write_flag) && TYPE(write_flag) != T_FIXNUM) { + rb_raise(rb_eTypeError, "bad write_flag: got:<%s> want: <Fixnum>", + rb_obj_classname(write_flag)); + return Qnil; + } + + return rb_ivar_set(self, id_write_flag, write_flag); +} + /* grpc_rb_md_ary_fill_hash_cb is the hash iteration callback used to fill grpc_metadata_array. @@ -437,17 +465,19 @@ typedef struct run_batch_stack { grpc_status_code recv_status; char *recv_status_details; size_t recv_status_details_capacity; + uint write_flag; } run_batch_stack; /* grpc_run_batch_stack_init ensures the run_batch_stack is properly * initialized */ -static void grpc_run_batch_stack_init(run_batch_stack *st) { +static void grpc_run_batch_stack_init(run_batch_stack *st, uint write_flag) { MEMZERO(st, run_batch_stack, 1); grpc_metadata_array_init(&st->send_metadata); grpc_metadata_array_init(&st->send_trailing_metadata); grpc_metadata_array_init(&st->recv_metadata); grpc_metadata_array_init(&st->recv_trailing_metadata); st->op_num = 0; + st->write_flag = write_flag; } /* grpc_run_batch_stack_cleanup ensures the run_batch_stack is properly @@ -477,6 +507,7 @@ static void grpc_run_batch_stack_fill_ops(run_batch_stack *st, VALUE ops_hash) { for (i = 0; i < (size_t)RARRAY_LEN(ops_ary); i++) { this_op = rb_ary_entry(ops_ary, i); this_value = rb_hash_aref(ops_hash, this_op); + st->ops[st->op_num].flags = 0; switch (NUM2INT(this_op)) { case GRPC_OP_SEND_INITIAL_METADATA: /* N.B. later there is no need to explicitly delete the metadata keys @@ -490,6 +521,7 @@ static void grpc_run_batch_stack_fill_ops(run_batch_stack *st, VALUE ops_hash) { case GRPC_OP_SEND_MESSAGE: st->ops[st->op_num].data.send_message = grpc_rb_s_to_byte_buffer( RSTRING_PTR(this_value), RSTRING_LEN(this_value)); + st->ops[st->op_num].flags = st->write_flag; break; case GRPC_OP_SEND_CLOSE_FROM_CLIENT: break; @@ -525,7 +557,7 @@ static void grpc_run_batch_stack_fill_ops(run_batch_stack *st, VALUE ops_hash) { NUM2INT(this_op)); }; st->ops[st->op_num].op = (grpc_op_type)NUM2INT(this_op); - st->ops[st->op_num].flags = 0; + st->ops[st->op_num].reserved = NULL; st->op_num++; } } @@ -603,6 +635,8 @@ static VALUE grpc_rb_call_run_batch(VALUE self, VALUE cqueue, VALUE tag, grpc_event ev; grpc_call_error err; VALUE result = Qnil; + VALUE rb_write_flag = rb_ivar_get(self, id_write_flag); + uint write_flag = 0; TypedData_Get_Struct(self, grpc_call, &grpc_call_data_type, call); /* Validate the ops args, adding them to a ruby array */ @@ -610,12 +644,15 @@ static VALUE grpc_rb_call_run_batch(VALUE self, VALUE cqueue, VALUE tag, rb_raise(rb_eTypeError, "call#run_batch: ops hash should be a hash"); return Qnil; } - grpc_run_batch_stack_init(&st); + if (rb_write_flag != Qnil) { + write_flag = NUM2UINT(rb_write_flag); + } + grpc_run_batch_stack_init(&st, write_flag); grpc_run_batch_stack_fill_ops(&st, ops_hash); /* call grpc_call_start_batch, then wait for it to complete using * pluck_event */ - err = grpc_call_start_batch(call, st.ops, st.op_num, ROBJECT(tag)); + err = grpc_call_start_batch(call, st.ops, st.op_num, ROBJECT(tag), NULL); if (err != GRPC_CALL_OK) { grpc_run_batch_stack_cleanup(&st); rb_raise(grpc_rb_eCallError, @@ -629,18 +666,24 @@ static VALUE grpc_rb_call_run_batch(VALUE self, VALUE cqueue, VALUE tag, rb_raise(grpc_rb_eOutOfTime, "grpc_call_start_batch timed out"); return Qnil; } - if (!ev.success) { - grpc_run_batch_stack_cleanup(&st); - rb_raise(grpc_rb_eCallError, "start_batch completion failed"); - return Qnil; - } - /* Build and return the BatchResult struct result */ + /* Build and return the BatchResult struct result, + if there is an error, it's reflected in the status */ result = grpc_run_batch_stack_build_result(&st); grpc_run_batch_stack_cleanup(&st); return result; } +static void Init_grpc_write_flags() { + /* Constants representing the write flags in grpc.h */ + VALUE grpc_rb_mWriteFlags = + rb_define_module_under(grpc_rb_mGrpcCore, "WriteFlags"); + rb_define_const(grpc_rb_mWriteFlags, "BUFFER_HINT", + UINT2NUM(GRPC_WRITE_BUFFER_HINT)); + rb_define_const(grpc_rb_mWriteFlags, "NO_COMPRESS", + UINT2NUM(GRPC_WRITE_NO_COMPRESS)); +} + static void Init_grpc_error_codes() { /* Constants representing the error codes of grpc_call_error in grpc.h */ VALUE grpc_rb_mRpcErrors = @@ -738,10 +781,14 @@ void Init_grpc_call() { rb_define_method(grpc_rb_cCall, "status=", grpc_rb_call_set_status, 1); rb_define_method(grpc_rb_cCall, "metadata", grpc_rb_call_get_metadata, 0); rb_define_method(grpc_rb_cCall, "metadata=", grpc_rb_call_set_metadata, 1); + rb_define_method(grpc_rb_cCall, "write_flag", grpc_rb_call_get_write_flag, 0); + rb_define_method(grpc_rb_cCall, "write_flag=", grpc_rb_call_set_write_flag, + 1); /* Ids used to support call attributes */ id_metadata = rb_intern("metadata"); id_status = rb_intern("status"); + id_write_flag = rb_intern("write_flag"); /* Ids used by the c wrapping internals. */ id_cq = rb_intern("__cq"); @@ -769,6 +816,7 @@ void Init_grpc_call() { Init_grpc_error_codes(); Init_grpc_op_codes(); + Init_grpc_write_flags(); } /* Gets the call from the ruby object */ diff --git a/src/ruby/ext/grpc/rb_channel.c b/src/ruby/ext/grpc/rb_channel.c index 2129ba3485..6491aa4fb4 100644 --- a/src/ruby/ext/grpc/rb_channel.c +++ b/src/ruby/ext/grpc/rb_channel.c @@ -147,7 +147,7 @@ static VALUE grpc_rb_channel_init(int argc, VALUE *argv, VALUE self) { target_chars = StringValueCStr(target); grpc_rb_hash_convert_to_channel_args(channel_args, &args); if (credentials == Qnil) { - ch = grpc_insecure_channel_create(target_chars, &args); + ch = grpc_insecure_channel_create(target_chars, &args, NULL); } else { creds = grpc_rb_get_wrapped_credentials(credentials); ch = grpc_secure_channel_create(creds, target_chars, &args); @@ -288,7 +288,7 @@ static VALUE grpc_rb_channel_create_call(VALUE self, VALUE cqueue, call = grpc_channel_create_call(ch, parent_call, flags, cq, method_chars, host_chars, grpc_rb_time_timeval( deadline, - /* absolute time */ 0)); + /* absolute time */ 0), NULL); if (call == NULL) { rb_raise(rb_eRuntimeError, "cannot create call with method %s", method_chars); diff --git a/src/ruby/ext/grpc/rb_completion_queue.c b/src/ruby/ext/grpc/rb_completion_queue.c index b6674d7682..0bc9eb2a97 100644 --- a/src/ruby/ext/grpc/rb_completion_queue.c +++ b/src/ruby/ext/grpc/rb_completion_queue.c @@ -56,7 +56,7 @@ typedef struct next_call_stack { static void *grpc_rb_completion_queue_next_no_gil(void *param) { next_call_stack *const next_call = (next_call_stack*)param; next_call->event = - grpc_completion_queue_next(next_call->cq, next_call->timeout); + grpc_completion_queue_next(next_call->cq, next_call->timeout, NULL); return NULL; } @@ -64,7 +64,7 @@ static void *grpc_rb_completion_queue_next_no_gil(void *param) { static void *grpc_rb_completion_queue_pluck_no_gil(void *param) { next_call_stack *const next_call = (next_call_stack*)param; next_call->event = grpc_completion_queue_pluck(next_call->cq, next_call->tag, - next_call->timeout); + next_call->timeout, NULL); return NULL; } @@ -128,7 +128,7 @@ static rb_data_type_t grpc_rb_completion_queue_data_type = { /* Allocates a completion queue. */ static VALUE grpc_rb_completion_queue_alloc(VALUE cls) { - grpc_completion_queue *cq = grpc_completion_queue_create(); + grpc_completion_queue *cq = grpc_completion_queue_create(NULL); if (cq == NULL) { rb_raise(rb_eArgError, "could not create a completion queue: not sure why"); } diff --git a/src/ruby/ext/grpc/rb_server.c b/src/ruby/ext/grpc/rb_server.c index 79a4ae8757..7e76349d2e 100644 --- a/src/ruby/ext/grpc/rb_server.c +++ b/src/ruby/ext/grpc/rb_server.c @@ -128,7 +128,7 @@ static VALUE grpc_rb_server_init(VALUE self, VALUE cqueue, VALUE channel_args) { TypedData_Get_Struct(self, grpc_rb_server, &grpc_rb_server_data_type, wrapper); grpc_rb_hash_convert_to_channel_args(channel_args, &args); - srv = grpc_server_create(&args); + srv = grpc_server_create(&args, NULL); if (args.args != NULL) { xfree(args.args); /* Allocated by grpc_rb_hash_convert_to_channel_args */ @@ -136,7 +136,7 @@ static VALUE grpc_rb_server_init(VALUE self, VALUE cqueue, VALUE channel_args) { if (srv == NULL) { rb_raise(rb_eRuntimeError, "could not create a gRPC server, not sure why"); } - grpc_server_register_completion_queue(srv, cq); + grpc_server_register_completion_queue(srv, cq, NULL); wrapper->wrapped = srv; /* Add the cq as the server's mark object. This ensures the ruby cq can't be diff --git a/src/ruby/grpc.gemspec b/src/ruby/grpc.gemspec index eb748458b9..20a6206e7e 100755 --- a/src/ruby/grpc.gemspec +++ b/src/ruby/grpc.gemspec @@ -24,16 +24,16 @@ Gem::Specification.new do |s| %w(math noproto).each do |b| s.executables += ["#{b}_client.rb", "#{b}_server.rb"] end - s.require_paths = %w( bin lib ) + s.executables += %w(grpc_ruby_interop_client grpc_ruby_interop_server) + s.require_paths = %w( bin lib pb ) s.platform = Gem::Platform::RUBY s.add_dependency 'google-protobuf', '~> 3.0.0alpha.1.1' - s.add_dependency 'googleauth', '~> 0.4' # reqd for interop tests - s.add_dependency 'logging', '~> 2.0' - s.add_dependency 'minitest', '~> 5.4' # reqd for interop tests + s.add_dependency 'googleauth', '~> 0.4' - s.add_development_dependency 'simplecov', '~> 0.9' s.add_development_dependency 'bundler', '~> 1.9' + s.add_development_dependency 'logging', '~> 2.0' + s.add_development_dependency 'simplecov', '~> 0.9' s.add_development_dependency 'rake', '~> 10.4' s.add_development_dependency 'rake-compiler', '~> 0.9' s.add_development_dependency 'rspec', '~> 3.2' diff --git a/src/ruby/lib/grpc/generic/active_call.rb b/src/ruby/lib/grpc/generic/active_call.rb index 215c0069a3..d9cb924735 100644 --- a/src/ruby/lib/grpc/generic/active_call.rb +++ b/src/ruby/lib/grpc/generic/active_call.rb @@ -59,7 +59,7 @@ module GRPC include Core::CallOps extend Forwardable attr_reader(:deadline) - def_delegators :@call, :cancel, :metadata + def_delegators :@call, :cancel, :metadata, :write_flag, :write_flag= # client_invoke begins a client invocation. # @@ -74,8 +74,7 @@ module GRPC # # @param call [Call] a call on which to start and invocation # @param q [CompletionQueue] the completion queue - # @param deadline [Fixnum,TimeSpec] the deadline - def self.client_invoke(call, q, _deadline, **kw) + def self.client_invoke(call, q, **kw) fail(TypeError, '!Core::Call') unless call.is_a? Core::Call unless q.is_a? Core::CompletionQueue fail(TypeError, '!Core::CompletionQueue') @@ -418,7 +417,7 @@ module GRPC # @return [Enumerator, nil] a response Enumerator def bidi_streamer(requests, **kw, &blk) start_call(**kw) unless @started - bd = BidiCall.new(@call, @cq, @marshal, @unmarshal, @deadline) + bd = BidiCall.new(@call, @cq, @marshal, @unmarshal) bd.run_on_client(requests, @op_notifier, &blk) end @@ -434,7 +433,7 @@ module GRPC # # @param gen_each_reply [Proc] generates the BiDi stream replies def run_server_bidi(gen_each_reply) - bd = BidiCall.new(@call, @cq, @marshal, @unmarshal, @deadline) + bd = BidiCall.new(@call, @cq, @marshal, @unmarshal) bd.run_on_server(gen_each_reply) end @@ -456,7 +455,7 @@ module GRPC # Starts the call if not already started def start_call(**kw) return if @started - @metadata_tag = ActiveCall.client_invoke(@call, @cq, @deadline, **kw) + @metadata_tag = ActiveCall.client_invoke(@call, @cq, **kw) @started = true end @@ -485,6 +484,7 @@ module GRPC # Operation limits access to an ActiveCall's methods for use as # a Operation on the client. Operation = view_class(:cancel, :cancelled, :deadline, :execute, - :metadata, :status, :start_call, :wait) + :metadata, :status, :start_call, :wait, :write_flag, + :write_flag=) end end diff --git a/src/ruby/lib/grpc/generic/bidi_call.rb b/src/ruby/lib/grpc/generic/bidi_call.rb index 3b0c71395c..9dbbb74caf 100644 --- a/src/ruby/lib/grpc/generic/bidi_call.rb +++ b/src/ruby/lib/grpc/generic/bidi_call.rb @@ -56,15 +56,13 @@ module GRPC # the call # @param marshal [Function] f(obj)->string that marshal requests # @param unmarshal [Function] f(string)->obj that unmarshals responses - # @param deadline [Fixnum] the deadline for the call to complete - def initialize(call, q, marshal, unmarshal, deadline) + def initialize(call, q, marshal, unmarshal) fail(ArgumentError, 'not a call') unless call.is_a? Core::Call unless q.is_a? Core::CompletionQueue fail(ArgumentError, 'not a CompletionQueue') end @call = call @cq = q - @deadline = deadline @marshal = marshal @op_notifier = nil # signals completion on clients @readq = Queue.new @@ -99,7 +97,7 @@ module GRPC # @param gen_each_reply [Proc] generates the BiDi stream replies. def run_on_server(gen_each_reply) replys = gen_each_reply.call(each_queued_msg) - @loop_th = start_read_loop + @loop_th = start_read_loop(is_client: false) write_loop(replys, is_client: false) end @@ -127,7 +125,7 @@ module GRPC count += 1 req = @readq.pop GRPC.logger.debug("each_queued_msg: req = #{req}") - throw req if req.is_a? StandardError + fail req if req.is_a? StandardError break if req.equal?(END_OF_READS) yield req end @@ -147,12 +145,9 @@ module GRPC GRPC.logger.debug("bidi-write-loop: #{count} writes done") if is_client GRPC.logger.debug("bidi-write-loop: client sent #{count}, waiting") - batch_result = @call.run_batch(@cq, write_tag, INFINITE_FUTURE, - SEND_CLOSE_FROM_CLIENT => nil, - RECV_STATUS_ON_CLIENT => nil) - @call.status = batch_result.status - batch_result.check_status - GRPC.logger.debug("bidi-write-loop: done status #{@call.status}") + @call.run_batch(@cq, write_tag, INFINITE_FUTURE, + SEND_CLOSE_FROM_CLIENT => nil) + GRPC.logger.debug('bidi-write-loop: done') notify_done end GRPC.logger.debug('bidi-write-loop: finished') @@ -164,7 +159,7 @@ module GRPC end # starts the read loop - def start_read_loop + def start_read_loop(is_client: true) Thread.new do GRPC.logger.debug('bidi-read-loop: starting') begin @@ -177,9 +172,19 @@ module GRPC # TODO: ensure metadata is read if available, currently it's not batch_result = @call.run_batch(@cq, read_tag, INFINITE_FUTURE, RECV_MESSAGE => nil) + # handle the next message if batch_result.message.nil? GRPC.logger.debug("bidi-read-loop: null batch #{batch_result}") + + if is_client + batch_result = @call.run_batch(@cq, read_tag, INFINITE_FUTURE, + RECV_STATUS_ON_CLIENT => nil) + @call.status = batch_result.status + batch_result.check_status + GRPC.logger.debug("bidi-read-loop: done status #{@call.status}") + end + @readq.push(END_OF_READS) GRPC.logger.debug('bidi-read-loop: done reading!') break diff --git a/src/ruby/lib/grpc/generic/client_stub.rb b/src/ruby/lib/grpc/generic/client_stub.rb index cce718537c..24ec1793f6 100644 --- a/src/ruby/lib/grpc/generic/client_stub.rb +++ b/src/ruby/lib/grpc/generic/client_stub.rb @@ -161,15 +161,21 @@ module GRPC # @param marshal [Function] f(obj)->string that marshals requests # @param unmarshal [Function] f(string)->obj that unmarshals responses # @param timeout [Numeric] (optional) the max completion time in seconds + # @param deadline [Time] (optional) the time the request should complete # @param parent [Core::Call] a prior call whose reserved metadata # will be propagated by this one. # @param return_op [true|false] return an Operation if true # @return [Object] the response received from the server - def request_response(method, req, marshal, unmarshal, timeout = nil, + def request_response(method, req, marshal, unmarshal, + deadline: nil, + timeout: nil, return_op: false, parent: parent, **kw) - c = new_active_call(method, marshal, unmarshal, timeout, parent: parent) + c = new_active_call(method, marshal, unmarshal, + deadline: deadline, + timeout: timeout, + parent: parent) kw_with_jwt_uri = self.class.update_with_jwt_aud_uri(kw, @host, method) md = @update_metadata.nil? ? kw : @update_metadata.call(kw_with_jwt_uri) return c.request_response(req, **md) unless return_op @@ -222,16 +228,22 @@ module GRPC # @param requests [Object] an Enumerable of requests to send # @param marshal [Function] f(obj)->string that marshals requests # @param unmarshal [Function] f(string)->obj that unmarshals responses - # @param timeout [Numeric] the max completion time in seconds + # @param timeout [Numeric] (optional) the max completion time in seconds + # @param deadline [Time] (optional) the time the request should complete # @param return_op [true|false] return an Operation if true # @param parent [Core::Call] a prior call whose reserved metadata # will be propagated by this one. # @return [Object|Operation] the response received from the server - def client_streamer(method, requests, marshal, unmarshal, timeout = nil, + def client_streamer(method, requests, marshal, unmarshal, + deadline: nil, + timeout: nil, return_op: false, parent: nil, **kw) - c = new_active_call(method, marshal, unmarshal, timeout, parent: parent) + c = new_active_call(method, marshal, unmarshal, + deadline: deadline, + timeout: timeout, + parent: parent) kw_with_jwt_uri = self.class.update_with_jwt_aud_uri(kw, @host, method) md = @update_metadata.nil? ? kw : @update_metadata.call(kw_with_jwt_uri) return c.client_streamer(requests, **md) unless return_op @@ -292,18 +304,24 @@ module GRPC # @param req [Object] the request sent to the server # @param marshal [Function] f(obj)->string that marshals requests # @param unmarshal [Function] f(string)->obj that unmarshals responses - # @param timeout [Numeric] the max completion time in seconds + # @param timeout [Numeric] (optional) the max completion time in seconds + # @param deadline [Time] (optional) the time the request should complete # @param return_op [true|false]return an Operation if true # @param parent [Core::Call] a prior call whose reserved metadata # will be propagated by this one. # @param blk [Block] when provided, is executed for each response # @return [Enumerator|Operation|nil] as discussed above - def server_streamer(method, req, marshal, unmarshal, timeout = nil, + def server_streamer(method, req, marshal, unmarshal, + deadline: nil, + timeout: nil, return_op: false, parent: nil, **kw, &blk) - c = new_active_call(method, marshal, unmarshal, timeout, parent: parent) + c = new_active_call(method, marshal, unmarshal, + deadline: deadline, + timeout: timeout, + parent: parent) kw_with_jwt_uri = self.class.update_with_jwt_aud_uri(kw, @host, method) md = @update_metadata.nil? ? kw : @update_metadata.call(kw_with_jwt_uri) return c.server_streamer(req, **md, &blk) unless return_op @@ -404,17 +422,23 @@ module GRPC # @param marshal [Function] f(obj)->string that marshals requests # @param unmarshal [Function] f(string)->obj that unmarshals responses # @param timeout [Numeric] (optional) the max completion time in seconds + # @param deadline [Time] (optional) the time the request should complete # @param parent [Core::Call] a prior call whose reserved metadata # will be propagated by this one. # @param return_op [true|false] return an Operation if true # @param blk [Block] when provided, is executed for each response # @return [Enumerator|nil|Operation] as discussed above - def bidi_streamer(method, requests, marshal, unmarshal, timeout = nil, + def bidi_streamer(method, requests, marshal, unmarshal, + deadline: nil, + timeout: nil, return_op: false, parent: nil, **kw, &blk) - c = new_active_call(method, marshal, unmarshal, timeout, parent: parent) + c = new_active_call(method, marshal, unmarshal, + deadline: deadline, + timeout: timeout, + parent: parent) kw_with_jwt_uri = self.class.update_with_jwt_aud_uri(kw, @host, method) md = @update_metadata.nil? ? kw : @update_metadata.call(kw_with_jwt_uri) return c.bidi_streamer(requests, **md, &blk) unless return_op @@ -438,8 +462,13 @@ module GRPC # @param parent [Grpc::Call] a parent call, available when calls are # made from server # @param timeout [TimeConst] - def new_active_call(method, marshal, unmarshal, timeout = nil, parent: nil) - deadline = from_relative_time(timeout.nil? ? @timeout : timeout) + def new_active_call(method, marshal, unmarshal, + deadline: nil, + timeout: nil, + parent: nil) + if deadline.nil? + deadline = from_relative_time(timeout.nil? ? @timeout : timeout) + end call = @ch.create_call(@queue, parent, # parent call @propagate_mask, # propagation options diff --git a/src/ruby/lib/grpc/generic/service.rb b/src/ruby/lib/grpc/generic/service.rb index 3b9743ea66..80ff669cca 100644 --- a/src/ruby/lib/grpc/generic/service.rb +++ b/src/ruby/lib/grpc/generic/service.rb @@ -174,26 +174,24 @@ module GRPC unmarshal = desc.unmarshal_proc(:output) route = "/#{route_prefix}/#{name}" if desc.request_response? - define_method(mth_name) do |req, deadline = nil, **kw| + define_method(mth_name) do |req, **kw| GRPC.logger.debug("calling #{@host}:#{route}") - request_response(route, req, marshal, unmarshal, deadline, **kw) + request_response(route, req, marshal, unmarshal, **kw) end elsif desc.client_streamer? - define_method(mth_name) do |reqs, deadline = nil, **kw| + define_method(mth_name) do |reqs, **kw| GRPC.logger.debug("calling #{@host}:#{route}") - client_streamer(route, reqs, marshal, unmarshal, deadline, **kw) + client_streamer(route, reqs, marshal, unmarshal, **kw) end elsif desc.server_streamer? - define_method(mth_name) do |req, deadline = nil, **kw, &blk| + define_method(mth_name) do |req, **kw, &blk| GRPC.logger.debug("calling #{@host}:#{route}") - server_streamer(route, req, marshal, unmarshal, deadline, **kw, - &blk) + server_streamer(route, req, marshal, unmarshal, **kw, &blk) end else # is a bidi_stream - define_method(mth_name) do |reqs, deadline = nil, **kw, &blk| + define_method(mth_name) do |reqs, **kw, &blk| GRPC.logger.debug("calling #{@host}:#{route}") - bidi_streamer(route, reqs, marshal, unmarshal, deadline, **kw, - &blk) + bidi_streamer(route, reqs, marshal, unmarshal, **kw, &blk) end end end diff --git a/src/ruby/lib/grpc/logconfig.rb b/src/ruby/lib/grpc/logconfig.rb index e9b4aa3c95..6b442febcb 100644 --- a/src/ruby/lib/grpc/logconfig.rb +++ b/src/ruby/lib/grpc/logconfig.rb @@ -27,17 +27,33 @@ # (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 'logging' - # GRPC contains the General RPC module. module GRPC - extend Logging.globally -end + # DefaultLogger is a module included in GRPC if no other logging is set up for + # it. See ../spec/spec_helpers an example of where other logging is added. + module DefaultLogger + def logger + LOGGER + end + + private + + # NoopLogger implements the methods of Ruby's conventional logging interface + # that are actually used internally within gRPC with a noop implementation. + class NoopLogger + def info(_ignored) + end -Logging.logger.root.appenders = Logging.appenders.stdout -Logging.logger.root.level = :info + def debug(_ignored) + end -# TODO: provide command-line configuration for logging -Logging.logger['GRPC'].level = :info -Logging.logger['GRPC::ActiveCall'].level = :info -Logging.logger['GRPC::BidiCall'].level = :info + def warn(_ignored) + end + end + + LOGGER = NoopLogger.new + end + + # Inject the noop #logger if no module-level logger method has been injected. + extend DefaultLogger unless methods.include?(:logger) +end diff --git a/src/ruby/pb/README.md b/src/ruby/pb/README.md new file mode 100644 index 0000000000..84644e1098 --- /dev/null +++ b/src/ruby/pb/README.md @@ -0,0 +1,42 @@ +Protocol Buffers +================ + +This folder contains protocol buffers provided with gRPC ruby, and the generated +code to them. + +PREREQUISITES +------------- + +The code is is generated using the protoc (> 3.0.0.alpha.1) and the +grpc_ruby_plugin. These must be installed to regenerate the IDL defined +classes, but that's not necessary just to use them. + +health_check/v1alpha +-------------------- + +This package defines the surface of a simple health check service that gRPC +servers may choose to implement, and provides an implementation for it. To +re-generate the surface. + +```bash +$ # (from this directory) +$ protoc -I . grpc/health/v1alpha/health.proto \ + --grpc_out=. \ + --ruby_out=. \ + --plugin=protoc-gen-grpc=`which grpc_ruby_plugin` +``` + +test +---- + +This package defines the surface of the gRPC interop test service and client +To re-generate the surface, it's necessary to have checked-out versions of +the grpc interop test proto, e.g, by having the full gRPC repository. E.g, + +```bash +$ # (from this directory within the grpc repo) +$ protoc -I../../.. ../../../test/proto/{messages,test,empty}.proto \ + --grpc_out=. \ + --ruby_out=. \ + --plugin=protoc-gen-grpc=`which grpc_ruby_plugin` +``` diff --git a/src/ruby/bin/interop/test/cpp/interop/test_services.rb b/src/ruby/pb/grpc/health/checker.rb index 5a3146c581..8c692e74f9 100644 --- a/src/ruby/bin/interop/test/cpp/interop/test_services.rb +++ b/src/ruby/pb/grpc/health/checker.rb @@ -27,34 +27,49 @@ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -# Generated by the protocol buffer compiler. DO NOT EDIT! -# Source: test/cpp/interop/test.proto for package 'grpc.testing' - require 'grpc' -require 'test/cpp/interop/test' +require 'grpc/health/v1alpha/health_services' +require 'thread' module Grpc - module Testing - module TestService + # Health contains classes and modules that support providing a health check + # service. + module Health + # Checker is implementation of the schema-specified health checking service. + class Checker < V1alpha::Health::Service + StatusCodes = GRPC::Core::StatusCodes + HealthCheckResponse = V1alpha::HealthCheckResponse - # TODO: add proto service documentation here - class Service + # Initializes the statuses of participating services + def initialize + @statuses = {} + @status_mutex = Mutex.new # guards access to @statuses + end - include GRPC::GenericService + # Implements the rpc IDL API method + def check(req, _call) + status = nil + @status_mutex.synchronize do + status = @statuses["#{req.host}/#{req.service}"] + end + fail GRPC::BadStatus, StatusCodes::NOT_FOUND if status.nil? + HealthCheckResponse.new(status: status) + end - self.marshal_class_method = :encode - self.unmarshal_class_method = :decode - self.service_name = 'grpc.testing.TestService' + # Adds the health status for a given host and service. + def add_status(host, service, status) + @status_mutex.synchronize { @statuses["#{host}/#{service}"] = status } + end - rpc :EmptyCall, Empty, Empty - rpc :UnaryCall, SimpleRequest, SimpleResponse - rpc :StreamingOutputCall, StreamingOutputCallRequest, stream(StreamingOutputCallResponse) - rpc :StreamingInputCall, stream(StreamingInputCallRequest), StreamingInputCallResponse - rpc :FullDuplexCall, stream(StreamingOutputCallRequest), stream(StreamingOutputCallResponse) - rpc :HalfDuplexCall, stream(StreamingOutputCallRequest), stream(StreamingOutputCallResponse) + # Clears the status for the given host or service. + def clear_status(host, service) + @status_mutex.synchronize { @statuses.delete("#{host}/#{service}") } end - Stub = Service.rpc_stub_class + # Clears alls the statuses. + def clear_all + @status_mutex.synchronize { @statuses = {} } + end end end end diff --git a/src/ruby/pb/grpc/health/v1alpha/health.proto b/src/ruby/pb/grpc/health/v1alpha/health.proto new file mode 100644 index 0000000000..d31df1e0a7 --- /dev/null +++ b/src/ruby/pb/grpc/health/v1alpha/health.proto @@ -0,0 +1,50 @@ +// 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. + +syntax = "proto3"; + +package grpc.health.v1alpha; + +message HealthCheckRequest { + string host = 1; + string service = 2; +} + +message HealthCheckResponse { + enum ServingStatus { + UNKNOWN = 0; + SERVING = 1; + NOT_SERVING = 2; + } + ServingStatus status = 1; +} + +service Health { + rpc Check(HealthCheckRequest) returns (HealthCheckResponse); +}
\ No newline at end of file diff --git a/src/ruby/pb/grpc/health/v1alpha/health.rb b/src/ruby/pb/grpc/health/v1alpha/health.rb new file mode 100644 index 0000000000..9c04298ea5 --- /dev/null +++ b/src/ruby/pb/grpc/health/v1alpha/health.rb @@ -0,0 +1,29 @@ +# Generated by the protocol buffer compiler. DO NOT EDIT! +# source: grpc/health/v1alpha/health.proto + +require 'google/protobuf' + +Google::Protobuf::DescriptorPool.generated_pool.build do + add_message "grpc.health.v1alpha.HealthCheckRequest" do + optional :host, :string, 1 + optional :service, :string, 2 + end + add_message "grpc.health.v1alpha.HealthCheckResponse" do + optional :status, :enum, 1, "grpc.health.v1alpha.HealthCheckResponse.ServingStatus" + end + add_enum "grpc.health.v1alpha.HealthCheckResponse.ServingStatus" do + value :UNKNOWN, 0 + value :SERVING, 1 + value :NOT_SERVING, 2 + end +end + +module Grpc + module Health + module V1alpha + HealthCheckRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("grpc.health.v1alpha.HealthCheckRequest").msgclass + HealthCheckResponse = Google::Protobuf::DescriptorPool.generated_pool.lookup("grpc.health.v1alpha.HealthCheckResponse").msgclass + HealthCheckResponse::ServingStatus = Google::Protobuf::DescriptorPool.generated_pool.lookup("grpc.health.v1alpha.HealthCheckResponse.ServingStatus").enummodule + end + end +end diff --git a/src/ruby/pb/grpc/health/v1alpha/health_services.rb b/src/ruby/pb/grpc/health/v1alpha/health_services.rb new file mode 100644 index 0000000000..d5cba2e9ec --- /dev/null +++ b/src/ruby/pb/grpc/health/v1alpha/health_services.rb @@ -0,0 +1,28 @@ +# Generated by the protocol buffer compiler. DO NOT EDIT! +# Source: grpc/health/v1alpha/health.proto for package 'grpc.health.v1alpha' + +require 'grpc' +require 'grpc/health/v1alpha/health' + +module Grpc + module Health + module V1alpha + module Health + + # TODO: add proto service documentation here + class Service + + include GRPC::GenericService + + self.marshal_class_method = :encode + self.unmarshal_class_method = :decode + self.service_name = 'grpc.health.v1alpha.Health' + + rpc :Check, HealthCheckRequest, HealthCheckResponse + end + + Stub = Service.rpc_stub_class + end + end + end +end diff --git a/src/ruby/pb/test/client.rb b/src/ruby/pb/test/client.rb new file mode 100755 index 0000000000..164e304b4d --- /dev/null +++ b/src/ruby/pb/test/client.rb @@ -0,0 +1,453 @@ +#!/usr/bin/env ruby + +# 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. + +# client is a testing tool that accesses a gRPC interop testing server and runs +# a test on it. +# +# Helps validate interoperation b/w different gRPC implementations. +# +# Usage: $ path/to/client.rb --server_host=<hostname> \ +# --server_port=<port> \ +# --test_case=<testcase_name> + +this_dir = File.expand_path(File.dirname(__FILE__)) +lib_dir = File.join(File.dirname(File.dirname(this_dir)), 'lib') +pb_dir = File.dirname(File.dirname(this_dir)) +$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir) +$LOAD_PATH.unshift(pb_dir) unless $LOAD_PATH.include?(pb_dir) +$LOAD_PATH.unshift(this_dir) unless $LOAD_PATH.include?(this_dir) + +require 'optparse' + +require 'grpc' +require 'googleauth' +require 'google/protobuf' + +require 'test/proto/empty' +require 'test/proto/messages' +require 'test/proto/test_services' + +require 'signet/ssl_config' + +AUTH_ENV = Google::Auth::CredentialsLoader::ENV_VAR + +# AssertionError is use to indicate interop test failures. +class AssertionError < RuntimeError; end + +# Fails with AssertionError if the block does evaluate to true +def assert(msg = 'unknown cause') + fail 'No assertion block provided' unless block_given? + fail AssertionError, msg unless yield +end + +# loads the certificates used to access the test server securely. +def load_test_certs + this_dir = File.expand_path(File.dirname(__FILE__)) + data_dir = File.join(File.dirname(File.dirname(this_dir)), 'spec/testdata') + files = ['ca.pem', 'server1.key', 'server1.pem'] + files.map { |f| File.open(File.join(data_dir, f)).read } +end + +# loads the certificates used to access the test server securely. +def load_prod_cert + fail 'could not find a production cert' if ENV['SSL_CERT_FILE'].nil? + GRPC.logger.info("loading prod certs from #{ENV['SSL_CERT_FILE']}") + File.open(ENV['SSL_CERT_FILE']).read +end + +# creates SSL Credentials from the test certificates. +def test_creds + certs = load_test_certs + GRPC::Core::Credentials.new(certs[0]) +end + +# creates SSL Credentials from the production certificates. +def prod_creds + cert_text = load_prod_cert + GRPC::Core::Credentials.new(cert_text) +end + +# creates the SSL Credentials. +def ssl_creds(use_test_ca) + return test_creds if use_test_ca + prod_creds +end + +# creates a test stub that accesses host:port securely. +def create_stub(opts) + address = "#{opts.host}:#{opts.port}" + if opts.secure + stub_opts = { + :creds => ssl_creds(opts.use_test_ca), + GRPC::Core::Channel::SSL_TARGET => opts.host_override + } + + # Add service account creds if specified + wants_creds = %w(all compute_engine_creds service_account_creds) + if wants_creds.include?(opts.test_case) + unless opts.oauth_scope.nil? + auth_creds = Google::Auth.get_application_default(opts.oauth_scope) + stub_opts[:update_metadata] = auth_creds.updater_proc + end + end + + if opts.test_case == 'oauth2_auth_token' + auth_creds = Google::Auth.get_application_default(opts.oauth_scope) + kw = auth_creds.updater_proc.call({}) # gives as an auth token + + # use a metadata update proc that just adds the auth token. + stub_opts[:update_metadata] = proc { |md| md.merge(kw) } + end + + if opts.test_case == 'jwt_token_creds' # don't use a scope + auth_creds = Google::Auth.get_application_default + stub_opts[:update_metadata] = auth_creds.updater_proc + end + + GRPC.logger.info("... connecting securely to #{address}") + Grpc::Testing::TestService::Stub.new(address, **stub_opts) + else + GRPC.logger.info("... connecting insecurely to #{address}") + Grpc::Testing::TestService::Stub.new(address) + end +end + +# produces a string of null chars (\0) of length l. +def nulls(l) + fail 'requires #{l} to be +ve' if l < 0 + [].pack('x' * l).force_encoding('utf-8') +end + +# a PingPongPlayer implements the ping pong bidi test. +class PingPongPlayer + include Grpc::Testing + include Grpc::Testing::PayloadType + attr_accessor :queue + attr_accessor :canceller_op + + # reqs is the enumerator over the requests + def initialize(msg_sizes) + @queue = Queue.new + @msg_sizes = msg_sizes + @canceller_op = nil # used to cancel after the first response + end + + def each_item + return enum_for(:each_item) unless block_given? + req_cls, p_cls = StreamingOutputCallRequest, ResponseParameters # short + count = 0 + @msg_sizes.each do |m| + req_size, resp_size = m + req = req_cls.new(payload: Payload.new(body: nulls(req_size)), + response_type: :COMPRESSABLE, + response_parameters: [p_cls.new(size: resp_size)]) + yield req + resp = @queue.pop + assert('payload type is wrong') { :COMPRESSABLE == resp.payload.type } + assert("payload body #{count} has the wrong length") do + resp_size == resp.payload.body.length + end + p "OK: ping_pong #{count}" + count += 1 + unless @canceller_op.nil? + canceller_op.cancel + break + end + end + end +end + +# defines methods corresponding to each interop test case. +class NamedTests + include Grpc::Testing + include Grpc::Testing::PayloadType + + def initialize(stub, args) + @stub = stub + @args = args + end + + def empty_unary + resp = @stub.empty_call(Empty.new) + assert('empty_unary: invalid response') { resp.is_a?(Empty) } + p 'OK: empty_unary' + end + + def large_unary + perform_large_unary + p 'OK: large_unary' + end + + def service_account_creds + # ignore this test if the oauth options are not set + if @args.oauth_scope.nil? + p 'NOT RUN: service_account_creds; no service_account settings' + return + end + json_key = File.read(ENV[AUTH_ENV]) + wanted_email = MultiJson.load(json_key)['client_email'] + resp = perform_large_unary(fill_username: true, + fill_oauth_scope: true) + assert("#{__callee__}: bad username") { wanted_email == resp.username } + assert("#{__callee__}: bad oauth scope") do + @args.oauth_scope.include?(resp.oauth_scope) + end + p "OK: #{__callee__}" + end + + def jwt_token_creds + json_key = File.read(ENV[AUTH_ENV]) + wanted_email = MultiJson.load(json_key)['client_email'] + resp = perform_large_unary(fill_username: true) + assert("#{__callee__}: bad username") { wanted_email == resp.username } + p "OK: #{__callee__}" + end + + def compute_engine_creds + resp = perform_large_unary(fill_username: true, + fill_oauth_scope: true) + assert("#{__callee__}: bad username") do + @args.default_service_account == resp.username + end + p "OK: #{__callee__}" + end + + def oauth2_auth_token + resp = perform_large_unary(fill_username: true, + fill_oauth_scope: true) + json_key = File.read(ENV[AUTH_ENV]) + wanted_email = MultiJson.load(json_key)['client_email'] + assert("#{__callee__}: bad username") { wanted_email == resp.username } + assert("#{__callee__}: bad oauth scope") do + @args.oauth_scope.include?(resp.oauth_scope) + end + p "OK: #{__callee__}" + end + + def per_rpc_creds + auth_creds = Google::Auth.get_application_default(@args.oauth_scope) + kw = auth_creds.updater_proc.call({}) + resp = perform_large_unary(fill_username: true, + fill_oauth_scope: true, + **kw) + json_key = File.read(ENV[AUTH_ENV]) + wanted_email = MultiJson.load(json_key)['client_email'] + assert("#{__callee__}: bad username") { wanted_email == resp.username } + assert("#{__callee__}: bad oauth scope") do + @args.oauth_scope.include?(resp.oauth_scope) + end + p "OK: #{__callee__}" + end + + def client_streaming + msg_sizes = [27_182, 8, 1828, 45_904] + wanted_aggregate_size = 74_922 + reqs = msg_sizes.map do |x| + req = Payload.new(body: nulls(x)) + StreamingInputCallRequest.new(payload: req) + end + resp = @stub.streaming_input_call(reqs) + assert("#{__callee__}: aggregate payload size is incorrect") do + wanted_aggregate_size == resp.aggregated_payload_size + end + p "OK: #{__callee__}" + end + + def server_streaming + msg_sizes = [31_415, 9, 2653, 58_979] + response_spec = msg_sizes.map { |s| ResponseParameters.new(size: s) } + req = StreamingOutputCallRequest.new(response_type: :COMPRESSABLE, + response_parameters: response_spec) + resps = @stub.streaming_output_call(req) + resps.each_with_index do |r, i| + assert("#{__callee__}: too many responses") { i < msg_sizes.length } + assert("#{__callee__}: payload body #{i} has the wrong length") do + msg_sizes[i] == r.payload.body.length + end + assert("#{__callee__}: payload type is wrong") do + :COMPRESSABLE == r.payload.type + end + end + p "OK: #{__callee__}" + end + + def ping_pong + msg_sizes = [[27_182, 31_415], [8, 9], [1828, 2653], [45_904, 58_979]] + ppp = PingPongPlayer.new(msg_sizes) + resps = @stub.full_duplex_call(ppp.each_item) + resps.each { |r| ppp.queue.push(r) } + p "OK: #{__callee__}" + end + + def timeout_on_sleeping_server + msg_sizes = [[27_182, 31_415]] + ppp = PingPongPlayer.new(msg_sizes) + resps = @stub.full_duplex_call(ppp.each_item, timeout: 0.001) + resps.each { |r| ppp.queue.push(r) } + fail 'Should have raised GRPC::BadStatus(DEADLINE_EXCEEDED)' + rescue GRPC::BadStatus => e + assert("#{__callee__}: status was wrong") do + e.code == GRPC::Core::StatusCodes::DEADLINE_EXCEEDED + end + p "OK: #{__callee__}" + end + + def empty_stream + ppp = PingPongPlayer.new([]) + resps = @stub.full_duplex_call(ppp.each_item) + count = 0 + resps.each do |r| + ppp.queue.push(r) + count += 1 + end + assert("#{__callee__}: too many responses expected 0") do + count == 0 + end + p "OK: #{__callee__}" + end + + def cancel_after_begin + msg_sizes = [27_182, 8, 1828, 45_904] + reqs = msg_sizes.map do |x| + req = Payload.new(body: nulls(x)) + StreamingInputCallRequest.new(payload: req) + end + op = @stub.streaming_input_call(reqs, return_op: true) + op.cancel + op.execute + fail 'Should have raised GRPC:Cancelled' + rescue GRPC::Cancelled + assert("#{__callee__}: call operation should be CANCELLED") { op.cancelled } + p "OK: #{__callee__}" + end + + def cancel_after_first_response + msg_sizes = [[27_182, 31_415], [8, 9], [1828, 2653], [45_904, 58_979]] + ppp = PingPongPlayer.new(msg_sizes) + op = @stub.full_duplex_call(ppp.each_item, return_op: true) + ppp.canceller_op = op # causes ppp to cancel after the 1st message + op.execute.each { |r| ppp.queue.push(r) } + fail 'Should have raised GRPC:Cancelled' + rescue GRPC::Cancelled + assert("#{__callee__}: call operation should be CANCELLED") { op.cancelled } + op.wait + p "OK: #{__callee__}" + end + + def all + all_methods = NamedTests.instance_methods(false).map(&:to_s) + all_methods.each do |m| + next if m == 'all' || m.start_with?('assert') + p "TESTCASE: #{m}" + method(m).call + end + end + + private + + def perform_large_unary(fill_username: false, fill_oauth_scope: false, **kw) + req_size, wanted_response_size = 271_828, 314_159 + payload = Payload.new(type: :COMPRESSABLE, body: nulls(req_size)) + req = SimpleRequest.new(response_type: :COMPRESSABLE, + response_size: wanted_response_size, + payload: payload) + req.fill_username = fill_username + req.fill_oauth_scope = fill_oauth_scope + resp = @stub.unary_call(req, **kw) + assert('payload type is wrong') do + :COMPRESSABLE == resp.payload.type + end + assert('payload body has the wrong length') do + wanted_response_size == resp.payload.body.length + end + assert('payload body is invalid') do + nulls(wanted_response_size) == resp.payload.body + end + resp + end +end + +# Args is used to hold the command line info. +Args = Struct.new(:default_service_account, :host, :host_override, + :oauth_scope, :port, :secure, :test_case, + :use_test_ca) + +# validates the the command line options, returning them as a Hash. +def parse_args + args = Args.new + args.host_override = 'foo.test.google.fr' + OptionParser.new do |opts| + opts.on('--oauth_scope scope', + 'Scope for OAuth tokens') { |v| args['oauth_scope'] = v } + opts.on('--server_host SERVER_HOST', 'server hostname') do |v| + args['host'] = v + end + opts.on('--default_service_account email_address', + 'email address of the default service account') do |v| + args['default_service_account'] = v + end + opts.on('--server_host_override HOST_OVERRIDE', + 'override host via a HTTP header') do |v| + args['host_override'] = v + end + opts.on('--server_port SERVER_PORT', 'server port') { |v| args['port'] = v } + # instance_methods(false) gives only the methods defined in that class + test_cases = NamedTests.instance_methods(false).map(&:to_s) + test_case_list = test_cases.join(',') + opts.on('--test_case CODE', test_cases, {}, 'select a test_case', + " (#{test_case_list})") { |v| args['test_case'] = v } + opts.on('-s', '--use_tls', 'require a secure connection?') do |v| + args['secure'] = v + end + opts.on('-t', '--use_test_ca', + 'if secure, use the test certificate?') do |v| + args['use_test_ca'] = v + end + end.parse! + _check_args(args) +end + +def _check_args(args) + %w(host port test_case).each do |a| + if args[a].nil? + fail(OptionParser::MissingArgument, "please specify --#{a}") + end + end + args +end + +def main + opts = parse_args + stub = create_stub(opts) + NamedTests.new(stub, opts).method(opts['test_case']).call +end + +main diff --git a/src/ruby/pb/test/proto/empty.rb b/src/ruby/pb/test/proto/empty.rb new file mode 100644 index 0000000000..559adcc85e --- /dev/null +++ b/src/ruby/pb/test/proto/empty.rb @@ -0,0 +1,15 @@ +# Generated by the protocol buffer compiler. DO NOT EDIT! +# source: test/proto/empty.proto + +require 'google/protobuf' + +Google::Protobuf::DescriptorPool.generated_pool.build do + add_message "grpc.testing.Empty" do + end +end + +module Grpc + module Testing + Empty = Google::Protobuf::DescriptorPool.generated_pool.lookup("grpc.testing.Empty").msgclass + end +end diff --git a/src/ruby/bin/interop/test/cpp/interop/messages.rb b/src/ruby/pb/test/proto/messages.rb index 89c349b406..9b7f977285 100644 --- a/src/ruby/bin/interop/test/cpp/interop/messages.rb +++ b/src/ruby/pb/test/proto/messages.rb @@ -1,34 +1,5 @@ -# 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. - # Generated by the protocol buffer compiler. DO NOT EDIT! -# source: test/cpp/interop/messages.proto +# source: test/proto/messages.proto require 'google/protobuf' @@ -37,12 +8,18 @@ Google::Protobuf::DescriptorPool.generated_pool.build do optional :type, :enum, 1, "grpc.testing.PayloadType" optional :body, :string, 2 end + add_message "grpc.testing.EchoStatus" do + optional :code, :int32, 1 + optional :message, :string, 2 + end add_message "grpc.testing.SimpleRequest" do optional :response_type, :enum, 1, "grpc.testing.PayloadType" optional :response_size, :int32, 2 optional :payload, :message, 3, "grpc.testing.Payload" optional :fill_username, :bool, 4 optional :fill_oauth_scope, :bool, 5 + optional :response_compression, :enum, 6, "grpc.testing.CompressionType" + optional :response_status, :message, 7, "grpc.testing.EchoStatus" end add_message "grpc.testing.SimpleResponse" do optional :payload, :message, 1, "grpc.testing.Payload" @@ -63,20 +40,32 @@ Google::Protobuf::DescriptorPool.generated_pool.build do optional :response_type, :enum, 1, "grpc.testing.PayloadType" repeated :response_parameters, :message, 2, "grpc.testing.ResponseParameters" optional :payload, :message, 3, "grpc.testing.Payload" + optional :response_compression, :enum, 6, "grpc.testing.CompressionType" + optional :response_status, :message, 7, "grpc.testing.EchoStatus" end add_message "grpc.testing.StreamingOutputCallResponse" do optional :payload, :message, 1, "grpc.testing.Payload" end + add_message "grpc.testing.ReconnectInfo" do + optional :passed, :bool, 1 + repeated :backoff_ms, :int32, 2 + end add_enum "grpc.testing.PayloadType" do value :COMPRESSABLE, 0 value :UNCOMPRESSABLE, 1 value :RANDOM, 2 end + add_enum "grpc.testing.CompressionType" do + value :NONE, 0 + value :GZIP, 1 + value :DEFLATE, 2 + end end module Grpc module Testing Payload = Google::Protobuf::DescriptorPool.generated_pool.lookup("grpc.testing.Payload").msgclass + EchoStatus = Google::Protobuf::DescriptorPool.generated_pool.lookup("grpc.testing.EchoStatus").msgclass SimpleRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("grpc.testing.SimpleRequest").msgclass SimpleResponse = Google::Protobuf::DescriptorPool.generated_pool.lookup("grpc.testing.SimpleResponse").msgclass StreamingInputCallRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("grpc.testing.StreamingInputCallRequest").msgclass @@ -84,6 +73,8 @@ module Grpc ResponseParameters = Google::Protobuf::DescriptorPool.generated_pool.lookup("grpc.testing.ResponseParameters").msgclass StreamingOutputCallRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("grpc.testing.StreamingOutputCallRequest").msgclass StreamingOutputCallResponse = Google::Protobuf::DescriptorPool.generated_pool.lookup("grpc.testing.StreamingOutputCallResponse").msgclass + ReconnectInfo = Google::Protobuf::DescriptorPool.generated_pool.lookup("grpc.testing.ReconnectInfo").msgclass PayloadType = Google::Protobuf::DescriptorPool.generated_pool.lookup("grpc.testing.PayloadType").enummodule + CompressionType = Google::Protobuf::DescriptorPool.generated_pool.lookup("grpc.testing.CompressionType").enummodule end end diff --git a/src/ruby/pb/test/proto/test.rb b/src/ruby/pb/test/proto/test.rb new file mode 100644 index 0000000000..100eb6505c --- /dev/null +++ b/src/ruby/pb/test/proto/test.rb @@ -0,0 +1,14 @@ +# Generated by the protocol buffer compiler. DO NOT EDIT! +# source: test/proto/test.proto + +require 'google/protobuf' + +require 'test/proto/empty' +require 'test/proto/messages' +Google::Protobuf::DescriptorPool.generated_pool.build do +end + +module Grpc + module Testing + end +end diff --git a/src/ruby/pb/test/proto/test_services.rb b/src/ruby/pb/test/proto/test_services.rb new file mode 100644 index 0000000000..9df9cc5860 --- /dev/null +++ b/src/ruby/pb/test/proto/test_services.rb @@ -0,0 +1,64 @@ +# Generated by the protocol buffer compiler. DO NOT EDIT! +# Source: test/proto/test.proto for package 'grpc.testing' + +require 'grpc' +require 'test/proto/test' + +module Grpc + module Testing + module TestService + + # TODO: add proto service documentation here + class Service + + include GRPC::GenericService + + self.marshal_class_method = :encode + self.unmarshal_class_method = :decode + self.service_name = 'grpc.testing.TestService' + + rpc :EmptyCall, Empty, Empty + rpc :UnaryCall, SimpleRequest, SimpleResponse + rpc :StreamingOutputCall, StreamingOutputCallRequest, stream(StreamingOutputCallResponse) + rpc :StreamingInputCall, stream(StreamingInputCallRequest), StreamingInputCallResponse + rpc :FullDuplexCall, stream(StreamingOutputCallRequest), stream(StreamingOutputCallResponse) + rpc :HalfDuplexCall, stream(StreamingOutputCallRequest), stream(StreamingOutputCallResponse) + end + + Stub = Service.rpc_stub_class + end + module UnimplementedService + + # TODO: add proto service documentation here + class Service + + include GRPC::GenericService + + self.marshal_class_method = :encode + self.unmarshal_class_method = :decode + self.service_name = 'grpc.testing.UnimplementedService' + + rpc :UnimplementedCall, Empty, Empty + end + + Stub = Service.rpc_stub_class + end + module ReconnectService + + # TODO: add proto service documentation here + class Service + + include GRPC::GenericService + + self.marshal_class_method = :encode + self.unmarshal_class_method = :decode + self.service_name = 'grpc.testing.ReconnectService' + + rpc :Start, Empty, Empty + rpc :Stop, Empty, ReconnectInfo + end + + Stub = Service.rpc_stub_class + end + end +end diff --git a/src/ruby/pb/test/server.rb b/src/ruby/pb/test/server.rb new file mode 100755 index 0000000000..e2e1ecbd62 --- /dev/null +++ b/src/ruby/pb/test/server.rb @@ -0,0 +1,196 @@ +#!/usr/bin/env ruby + +# 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. + +# interop_server is a Testing app that runs a gRPC interop testing server. +# +# It helps validate interoperation b/w gRPC in different environments +# +# Helps validate interoperation b/w different gRPC implementations. +# +# Usage: $ path/to/interop_server.rb --port + +this_dir = File.expand_path(File.dirname(__FILE__)) +lib_dir = File.join(File.dirname(File.dirname(this_dir)), 'lib') +pb_dir = File.dirname(File.dirname(this_dir)) +$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir) +$LOAD_PATH.unshift(pb_dir) unless $LOAD_PATH.include?(pb_dir) +$LOAD_PATH.unshift(this_dir) unless $LOAD_PATH.include?(this_dir) + +require 'forwardable' +require 'optparse' + +require 'grpc' + +require 'test/proto/empty' +require 'test/proto/messages' +require 'test/proto/test_services' + +# loads the certificates by the test server. +def load_test_certs + this_dir = File.expand_path(File.dirname(__FILE__)) + data_dir = File.join(File.dirname(File.dirname(this_dir)), 'spec/testdata') + files = ['ca.pem', 'server1.key', 'server1.pem'] + files.map { |f| File.open(File.join(data_dir, f)).read } +end + +# creates a ServerCredentials from the test certificates. +def test_server_creds + certs = load_test_certs + GRPC::Core::ServerCredentials.new(nil, certs[1], certs[2]) +end + +# produces a string of null chars (\0) of length l. +def nulls(l) + fail 'requires #{l} to be +ve' if l < 0 + [].pack('x' * l).force_encoding('utf-8') +end + +# A EnumeratorQueue wraps a Queue yielding the items added to it via each_item. +class EnumeratorQueue + extend Forwardable + def_delegators :@q, :push + + def initialize(sentinel) + @q = Queue.new + @sentinel = sentinel + end + + def each_item + return enum_for(:each_item) unless block_given? + loop do + r = @q.pop + break if r.equal?(@sentinel) + fail r if r.is_a? Exception + yield r + end + end +end + +# A runnable implementation of the schema-specified testing service, with each +# service method implemented as required by the interop testing spec. +class TestTarget < Grpc::Testing::TestService::Service + include Grpc::Testing + include Grpc::Testing::PayloadType + + def empty_call(_empty, _call) + Empty.new + end + + def unary_call(simple_req, _call) + req_size = simple_req.response_size + SimpleResponse.new(payload: Payload.new(type: :COMPRESSABLE, + body: nulls(req_size))) + end + + def streaming_input_call(call) + sizes = call.each_remote_read.map { |x| x.payload.body.length } + sum = sizes.inject { |s, x| s + x } + StreamingInputCallResponse.new(aggregated_payload_size: sum) + end + + def streaming_output_call(req, _call) + cls = StreamingOutputCallResponse + req.response_parameters.map do |p| + cls.new(payload: Payload.new(type: req.response_type, + body: nulls(p.size))) + end + end + + def full_duplex_call(reqs) + # reqs is a lazy Enumerator of the requests sent by the client. + q = EnumeratorQueue.new(self) + cls = StreamingOutputCallResponse + Thread.new do + begin + GRPC.logger.info('interop-server: started receiving') + reqs.each do |req| + resp_size = req.response_parameters[0].size + GRPC.logger.info("read a req, response size is #{resp_size}") + resp = cls.new(payload: Payload.new(type: req.response_type, + body: nulls(resp_size))) + q.push(resp) + end + GRPC.logger.info('interop-server: finished receiving') + q.push(self) + rescue StandardError => e + GRPC.logger.info('interop-server: failed') + GRPC.logger.warn(e) + q.push(e) # share the exception with the enumerator + end + end + q.each_item + end + + def half_duplex_call(reqs) + # TODO: update with unique behaviour of the half_duplex_call if that's + # ever required by any of the tests. + full_duplex_call(reqs) + end +end + +# validates the the command line options, returning them as a Hash. +def parse_options + options = { + 'port' => nil, + 'secure' => false + } + OptionParser.new do |opts| + opts.banner = 'Usage: --port port' + opts.on('--port PORT', 'server port') do |v| + options['port'] = v + end + opts.on('-s', '--use_tls', 'require a secure connection?') do |v| + options['secure'] = v + end + end.parse! + + if options['port'].nil? + fail(OptionParser::MissingArgument, 'please specify --port') + end + options +end + +def main + opts = parse_options + host = "0.0.0.0:#{opts['port']}" + s = GRPC::RpcServer.new + if opts['secure'] + s.add_http2_port(host, test_server_creds) + GRPC.logger.info("... running securely on #{host}") + else + s.add_http2_port(host) + GRPC.logger.info("... running insecurely on #{host}") + end + s.handle(TestTarget) + s.run_till_terminated +end + +main 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 |