class Object

Constants

AUTH_ENV
ActiveCall

The ActiveCall class provides simple methods for sending marshallable data to a call

Args

Args is used to hold the command line info.

Call
CallCredentials
CallError
CallOps
ChannelCredentials
CheckerStub
Creds
Dsl
EchoStub
FailingStub
GenericService

Provides behaviour used to implement schema-derived service classes.

Is intended to be used to support both client and server IDL-schema-derived servers.

HCReq
HCResp
HEADER_DIRS
INCLUDEDIR
INTERNAL
LIBDIR
LIB_DIRS
NoProtoStub
OK
Pool

Pool is a simple thread pool.

RpcDesc

RpcDesc is a Descriptor of an RPC method.

RpcServer

RpcServer hosts a number of services and makes them available on the network.

Server
ServingStatus
SlowStub
StatusCodes
Stream
TEST_WRITE_FLAGS
TimeConsts

TimeConsts is a module from the C extension.

Here it's re-opened to add a utility func.

UNKNOWN
WriteFlags

Public Instance Methods

_check_args(args) click to toggle source
# File src/ruby/bin/apis/pubsub_demo.rb, line 241
def _check_args(args)
  %w(host port action).each do |a|
    if args[a].nil?
      raise OptionParser::MissingArgument.new("please specify --#{a}")
    end
  end
  args
end
a_bidi_streamer(_call) click to toggle source
# File src/ruby/spec/generic/service_spec.rb, line 313
def a_bidi_streamer(_call)
end
a_client_streamer(_call) click to toggle source
# File src/ruby/spec/generic/service_spec.rb, line 310
def a_client_streamer(_call)
end
a_server_streamer(_req, _call) click to toggle source
# File src/ruby/spec/generic/service_spec.rb, line 307
def a_server_streamer(_req, _call)
end
an_rpc() click to toggle source
# File src/ruby/spec/generic/service_spec.rb, line 290
def an_rpc
end
assert(msg = 'unknown cause') { || ... } click to toggle source

Fails with AssertionError if the block does evaluate to true

# File src/ruby/pb/test/client.rb, line 81
def assert(msg = 'unknown cause')
  fail 'No assertion block provided' unless block_given?
  fail AssertionError, msg unless yield
end
auth_proc(opts) click to toggle source

Builds the metadata authentication update proc.

# File src/ruby/bin/apis/pubsub_demo.rb, line 63
def auth_proc(opts)
  auth_creds = Google::Auth.get_application_default
  return auth_creds.updater_proc
end
bad_status(_req, _call) click to toggle source
# File src/ruby/spec/generic/rpc_desc_spec.rb, line 316
def bad_status(_req, _call)
  fail GRPC::BadStatus.new(@bs_code, 'NOK')
end
bad_status_alt(_call) click to toggle source
# File src/ruby/spec/generic/rpc_desc_spec.rb, line 324
def bad_status_alt(_call)
  fail GRPC::BadStatus.new(@bs_code, 'NOK')
end
can_run_codegen_check() click to toggle source
# File src/ruby/spec/pb/health/checker_spec.rb, line 36
def can_run_codegen_check
  system('which grpc_ruby_plugin') && system('which protoc')
end
check_md(wanted_md, received_md) click to toggle source
# File src/ruby/spec/generic/rpc_server_spec.rb, line 38
def check_md(wanted_md, received_md)
  wanted_md.zip(received_md).each do |w, r|
    w.each do |key, value|
      expect(r[key]).to eq(value)
    end
  end
end
construct_with_args(a) click to toggle source
# File src/ruby/spec/channel_spec.rb, line 101
def construct_with_args(a)
  proc { GRPC::Core::Channel.new('dummy_host', a, create_test_cert) }
end
create_stub(opts) click to toggle source

creates a test stub that accesses host:port securely.

# File src/ruby/pb/test/client.rb, line 112
def create_stub(opts)
  address = "#{opts.host}:#{opts.port}"
  if opts.secure
    creds = ssl_creds(opts.use_test_ca)
    stub_opts = {
      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)
        call_creds = GRPC::Core::CallCredentials.new(auth_creds.updater_proc)
        creds = creds.compose call_creds
      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.
      call_creds = GRPC::Core::CallCredentials.new(proc { |md| md.merge(kw) })
      creds = creds.compose call_creds
    end

    if opts.test_case == 'jwt_token_creds'  # don't use a scope
      auth_creds = Google::Auth.get_application_default
      call_creds = GRPC::Core::CallCredentials.new(auth_creds.updater_proc)
      creds = creds.compose call_creds
    end

    GRPC.logger.info("... connecting securely to #{address}")
    Grpc::Testing::TestService::Stub.new(address, creds, **stub_opts)
  else
    GRPC.logger.info("... connecting insecurely to #{address}")
    Grpc::Testing::TestService::Stub.new(address, :this_channel_is_insecure)
  end
end
create_test_cert() click to toggle source
# File src/ruby/spec/channel_spec.rb, line 42
def create_test_cert
  GRPC::Core::ChannelCredentials.new(load_test_certs[0])
end
create_test_server() click to toggle source
# File src/ruby/spec/generic/client_stub_spec.rb, line 458
def create_test_server
  @server_queue = GRPC::Core::CompletionQueue.new
  @server = GRPC::Core::Server.new(@server_queue, nil)
  @server.add_http2_port('0.0.0.0:0', :this_port_is_insecure)
end
deadline() click to toggle source
# File src/ruby/spec/call_spec.rb, line 160
def deadline
  Time.now + 2  # in 2 seconds; arbitrary
end
do_div(stub) click to toggle source
# File src/ruby/bin/math_client.rb, line 48
def do_div(stub)
  GRPC.logger.info('request_response')
  GRPC.logger.info('----------------')
  req = Math::DivArgs.new(dividend: 7, divisor: 3)
  GRPC.logger.info("div(7/3): req=#{req.inspect}")
  resp = stub.div(req, timeout: INFINITE_FUTURE)
  GRPC.logger.info("Answer: #{resp.inspect}")
  GRPC.logger.info('----------------')
end
do_div_many(stub) click to toggle source
# File src/ruby/bin/math_client.rb, line 81
def do_div_many(stub)
  GRPC.logger.info('bidi_streamer')
  GRPC.logger.info('-------------')
  reqs = []
  reqs << Math::DivArgs.new(dividend: 7, divisor: 3)
  reqs << Math::DivArgs.new(dividend: 5, divisor: 2)
  reqs << Math::DivArgs.new(dividend: 7, divisor: 2)
  GRPC.logger.info("div(7/3), div(5/2), div(7/2): reqs=#{reqs.inspect}")
  resp = stub.div_many(reqs, timeout: INFINITE_FUTURE)
  resp.each do |r|
    GRPC.logger.info("Answer: #{r.inspect}")
  end
  GRPC.logger.info('----------------')
end
do_fib(stub) click to toggle source
# File src/ruby/bin/math_client.rb, line 69
def do_fib(stub)
  GRPC.logger.info('server_streamer')
  GRPC.logger.info('----------------')
  req = Math::FibArgs.new(limit: 11)
  GRPC.logger.info("fib(11): req=#{req.inspect}")
  resp = stub.fib(req, timeout: INFINITE_FUTURE)
  resp.each do |r|
    GRPC.logger.info("Answer: #{r.inspect}")
  end
  GRPC.logger.info('----------------')
end
do_sum(stub) click to toggle source
# File src/ruby/bin/math_client.rb, line 58
def do_sum(stub)
  # to make client streaming requests, pass an enumerable of the inputs
  GRPC.logger.info('client_streamer')
  GRPC.logger.info('---------------')
  reqs = [1, 2, 3, 4, 5].map { |x| Math::Num.new(num: x) }
  GRPC.logger.info("sum(1, 2, 3, 4, 5): reqs=#{reqs.inspect}")
  resp = stub.sum(reqs)  # reqs.is_a?(Enumerable)
  GRPC.logger.info("Answer: #{resp.inspect}")
  GRPC.logger.info('---------------')
end
expect_server_to_be_invoked(**kw) click to toggle source
# File src/ruby/spec/generic/active_call_spec.rb, line 356
def expect_server_to_be_invoked(**kw)
  recvd_rpc =  @server.request_call(@server_queue, @server_tag, deadline)
  expect(recvd_rpc).to_not eq nil
  recvd_call = recvd_rpc.call
  recvd_call.run_batch(@server_queue, @server_tag, deadline,
                       CallOps::SEND_INITIAL_METADATA => kw)
  ActiveCall.new(recvd_call, @server_queue, @pass_through,
                 @pass_through, deadline)
end
expect_server_to_receive(sent_text, **kw) click to toggle source
# File src/ruby/spec/generic/active_call_spec.rb, line 350
def expect_server_to_receive(sent_text, **kw)
  c = expect_server_to_be_invoked(**kw)
  expect(c.remote_read).to eq(sent_text)
  c
end
fake_bidistream(an_array) click to toggle source
# File src/ruby/spec/generic/rpc_desc_spec.rb, line 312
def fake_bidistream(an_array)
  an_array
end
fake_clstream(_arg) click to toggle source
# File src/ruby/spec/generic/rpc_desc_spec.rb, line 193
def fake_clstream(_arg)
end
fake_reqresp(_req, _call) click to toggle source
# File src/ruby/spec/generic/rpc_desc_spec.rb, line 300
def fake_reqresp(_req, _call)
  @ok_response
end
fake_svstream(_arg1, _arg2) click to toggle source
# File src/ruby/spec/generic/rpc_desc_spec.rb, line 196
def fake_svstream(_arg1, _arg2)
end
get_response(stub) click to toggle source
# File src/ruby/spec/generic/client_stub_spec.rb, line 173
def get_response(stub)
  stub.request_response(@method, @sent_msg, noop, noop,
                        k1: 'v1', k2: 'v2')
end
get_responses(stub) click to toggle source
# File src/ruby/spec/generic/client_stub_spec.rb, line 290
def get_responses(stub)
  e = stub.server_streamer(@method, @sent_msg, noop, noop,
                           k1: 'v1', k2: 'v2')
  expect(e).to be_a(Enumerator)
  e
end
load_test_certs() click to toggle source
# File src/ruby/bin/math_client.rb, line 96
def load_test_certs
  this_dir = File.expand_path(File.dirname(__FILE__))
  data_dir = File.join(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
main() click to toggle source
# File src/ruby/bin/apis/pubsub_demo.rb, line 250
def main
  args = parse_args
  pub, sub = publisher_stub(args), subscriber_stub(args)
  NamedActions.new(pub, sub, args).method(args.action).call
end
make_test_call() click to toggle source
# File src/ruby/spec/call_spec.rb, line 156
def make_test_call
  @ch.create_call(client_queue, nil, nil, 'dummy_method', nil, deadline)
end
new_client_call() click to toggle source
# File src/ruby/spec/client_server_spec.rb, line 56
def new_client_call
  @ch.create_call(@client_queue, nil, nil, '/method', nil, deadline)
end
no_arg() click to toggle source
# File src/ruby/spec/generic/rpc_desc_spec.rb, line 190
def no_arg
end
nulls(l) click to toggle source

produces a string of null chars (0) of length l.

# File src/ruby/pb/test/client.rb, line 154
def nulls(l)
  fail 'requires #{l} to be +ve' if l < 0
  [].pack('x' * l).force_encoding('ascii-8bit')
end
other_error(_req, _call) click to toggle source
# File src/ruby/spec/generic/rpc_desc_spec.rb, line 320
def other_error(_req, _call)
  fail(ArgumentError, 'other error')
end
other_error_alt(_call) click to toggle source
# File src/ruby/spec/generic/rpc_desc_spec.rb, line 328
def other_error_alt(_call)
  fail(ArgumentError, 'other error')
end
parse_args() click to toggle source

validates the the command line options, returning them as an Arg.

# File src/ruby/bin/apis/pubsub_demo.rb, line 212
def parse_args
  args = Args.new('pubsub-staging.googleapis.com',
                   443, 'list_some_topics', 'stoked-keyword-656')
  OptionParser.new do |opts|
    opts.on('--server_host SERVER_HOST', 'server hostname') do |v|
      args.host = v
    end
    opts.on('--server_port SERVER_PORT', 'server port') do |v|
      args.port = v
    end

    # instance_methods(false) gives only the methods defined in that class.
    scenes = NamedActions.instance_methods(false).map { |t| t.to_s }
    scene_list = scenes.join(',')
    opts.on("--action CODE", scenes, {}, 'pick a demo action',
            "  (#{scene_list})") do |v|
      args.action = v
    end

    # Set the remaining values.
    %w(project_id topic_name sub_name).each do |o|
      opts.on("--#{o} VALUE", "#{o}") do |v|
        args[o] = v
      end
    end
  end.parse!
  _check_args(args)
end
parse_options() click to toggle source

validates the the command line options, returning them as a Hash.

# File src/ruby/pb/test/server.rb, line 216
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('--use_tls USE_TLS', ['false', 'true'],
            'require a secure connection?') do |v|
      options['secure'] = v == 'true'
    end
  end.parse!

  if options['port'].nil?
    fail(OptionParser::MissingArgument, 'please specify --port')
  end
  options
end
prod_creds() click to toggle source

creates SSL Credentials from the production certificates.

# File src/ruby/pb/test/client.rb, line 101
def prod_creds
  GRPC::Core::ChannelCredentials.new()
end
publisher_stub(opts) click to toggle source

Creates a stub for accessing the publisher service.

# File src/ruby/bin/apis/pubsub_demo.rb, line 69
def publisher_stub(opts)
  address = "#{opts.host}:#{opts.port}"
  stub_clz = Tech::Pubsub::PublisherService::Stub # shorter
  GRPC.logger.info("... access PublisherService at #{address}")
  call_creds = GRPC::Core::CallCredentials.new(auth_proc(opts))
  combined_creds = ssl_creds.compose(call_creds)
  stub_clz.new(address, creds: combined_creds,
               GRPC::Core::Channel::SSL_TARGET => opts.host)
end
run_bidi_streamer_echo_ping_pong(expected_inputs, status, client_starts) click to toggle source
# File src/ruby/spec/generic/client_stub_spec.rb, line 416
def run_bidi_streamer_echo_ping_pong(expected_inputs, status, client_starts)
  wakey_thread do |notifier|
    c = expect_server_to_be_invoked(notifier)
    expected_inputs.each do |i|
      if client_starts
        expect(c.remote_read).to eq(i)
        c.remote_send(i)
      else
        c.remote_send(i)
        expect(c.remote_read).to eq(i)
      end
    end
    c.send_status(status, status == @pass ? 'OK' : 'NOK', true)
  end
end
run_bidi_streamer_handle_inputs_first(expected_inputs, replys, status) click to toggle source
# File src/ruby/spec/generic/client_stub_spec.rb, line 406
def run_bidi_streamer_handle_inputs_first(expected_inputs, replys,
                                          status)
  wakey_thread do |notifier|
    c = expect_server_to_be_invoked(notifier)
    expected_inputs.each { |i| expect(c.remote_read).to eq(i) }
    replys.each { |r| c.remote_send(r) }
    c.send_status(status, status == @pass ? 'OK' : 'NOK', true)
  end
end
run_client_streamer(expected_inputs, resp, status, **kw) click to toggle source
# File src/ruby/spec/generic/client_stub_spec.rb, line 432
def run_client_streamer(expected_inputs, resp, status, **kw)
  wanted_metadata = kw.clone
  wakey_thread do |notifier|
    c = expect_server_to_be_invoked(notifier)
    expected_inputs.each { |i| expect(c.remote_read).to eq(i) }
    wanted_metadata.each do |k, v|
      expect(c.metadata[k.to_s]).to eq(v)
    end
    c.remote_send(resp)
    c.send_status(status, status == @pass ? 'OK' : 'NOK', true)
  end
end
run_request_response(expected_input, resp, status, **kw) click to toggle source
# File src/ruby/spec/generic/client_stub_spec.rb, line 445
def run_request_response(expected_input, resp, status, **kw)
  wanted_metadata = kw.clone
  wakey_thread do |notifier|
    c = expect_server_to_be_invoked(notifier)
    expect(c.remote_read).to eq(expected_input)
    wanted_metadata.each do |k, v|
      expect(c.metadata[k.to_s]).to eq(v)
    end
    c.remote_send(resp)
    c.send_status(status, status == @pass ? 'OK' : 'NOK', true)
  end
end
run_server_streamer(expected_input, replys, status, **kw) click to toggle source
# File src/ruby/spec/generic/client_stub_spec.rb, line 393
def run_server_streamer(expected_input, replys, status, **kw)
  wanted_metadata = kw.clone
  wakey_thread do |notifier|
    c = expect_server_to_be_invoked(notifier)
    wanted_metadata.each do |k, v|
      expect(c.metadata[k.to_s]).to eq(v)
    end
    expect(c.remote_read).to eq(expected_input)
    replys.each { |r| c.remote_send(r) }
    c.send_status(status, status == @pass ? 'OK' : 'NOK', true)
  end
end
server_allows_client_to_proceed() click to toggle source
# File src/ruby/spec/client_server_spec.rb, line 46
def server_allows_client_to_proceed
  recvd_rpc = @server.request_call(@server_queue, @server_tag, deadline)
  expect(recvd_rpc).to_not eq nil
  server_call = recvd_rpc.call
  ops = { CallOps::SEND_INITIAL_METADATA => {} }
  svr_batch = server_call.run_batch(@server_queue, @server_tag, deadline, ops)
  expect(svr_batch.send_metadata).to be true
  server_call
end
ssl_creds() click to toggle source

creates a SSL Credentials from the production certificates.

# File src/ruby/bin/apis/pubsub_demo.rb, line 58
def ssl_creds
  GRPC::Core::ChannelCredentials.new()
end
start_a_server() click to toggle source
# File src/ruby/spec/server_spec.rb, line 203
def start_a_server
  s = Server.new(@cq, nil)
  s.add_http2_port('0.0.0.0:0', :this_port_is_insecure)
  s.start
  s
end
subscriber_stub(opts) click to toggle source

Creates a stub for accessing the subscriber service.

# File src/ruby/bin/apis/pubsub_demo.rb, line 80
def subscriber_stub(opts)
  address = "#{opts.host}:#{opts.port}"
  stub_clz = Tech::Pubsub::SubscriberService::Stub # shorter
  GRPC.logger.info("... access SubscriberService at #{address}")
  call_creds = GRPC::Core::CallCredentials.new(auth_proc(opts))
  combined_creds = ssl_creds.compose(call_creds)
  stub_clz.new(address, creds: combined_creds,
               GRPC::Core::Channel::SSL_TARGET => opts.host)
end
test_creds() click to toggle source
# File src/ruby/bin/math_client.rb, line 103
def test_creds
  certs = load_test_certs
  GRPC::Core::ChannelCredentials.new(certs[0])
end
test_server_creds() click to toggle source
# File src/ruby/bin/math_server.rb, line 172
def test_server_creds
  certs = load_test_certs
  GRPC::Core::ServerCredentials.new(
    nil, [{ private_key: certs[1], cert_chain: certs[2] }], false)
end
wakey_thread(&blk) click to toggle source
# File src/ruby/spec/generic/client_stub_spec.rb, line 32
def wakey_thread(&blk)
  n = GRPC::Notifier.new
  t = Thread.new do
    blk.call(n)
  end
  n.wait
  t
end