class NamedTests

defines methods corresponding to each interop test case.

Public Class Methods

new(stub, args) click to toggle source
# File src/ruby/pb/test/client.rb, line 203
def initialize(stub, args)
  @stub = stub
  @args = args
end

Public Instance Methods

all() click to toggle source
# File src/ruby/pb/test/client.rb, line 380
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
cancel_after_begin() click to toggle source
# File src/ruby/pb/test/client.rb, line 352
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
cancel_after_first_response() click to toggle source
# File src/ruby/pb/test/client.rb, line 367
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
client_streaming() click to toggle source
# File src/ruby/pb/test/client.rb, line 285
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
compute_engine_creds() click to toggle source
# File src/ruby/pb/test/client.rb, line 244
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
empty_stream() click to toggle source
# File src/ruby/pb/test/client.rb, line 338
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
empty_unary() click to toggle source
# File src/ruby/pb/test/client.rb, line 208
def empty_unary
  resp = @stub.empty_call(Empty.new)
  assert('empty_unary: invalid response') { resp.is_a?(Empty) }
  p 'OK: empty_unary'
end
jwt_token_creds() click to toggle source
# File src/ruby/pb/test/client.rb, line 236
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
large_unary() click to toggle source
# File src/ruby/pb/test/client.rb, line 214
def large_unary
  perform_large_unary
  p 'OK: large_unary'
end
oauth2_auth_token() click to toggle source
# File src/ruby/pb/test/client.rb, line 253
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
per_rpc_creds() click to toggle source
# File src/ruby/pb/test/client.rb, line 265
def per_rpc_creds
  auth_creds = Google::Auth.get_application_default(@args.oauth_scope)
  update_metadata = proc do |md|
    kw = auth_creds.updater_proc.call({})
  end

  call_creds = GRPC::Core::CallCredentials.new(update_metadata)

  resp = perform_large_unary(fill_username: true,
                             fill_oauth_scope: true,
                             credentials: call_creds)
  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
ping_pong() click to toggle source
# File src/ruby/pb/test/client.rb, line 317
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
server_streaming() click to toggle source
# File src/ruby/pb/test/client.rb, line 299
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
service_account_creds() click to toggle source
# File src/ruby/pb/test/client.rb, line 219
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
timeout_on_sleeping_server() click to toggle source
# File src/ruby/pb/test/client.rb, line 325
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

Private Instance Methods

perform_large_unary(fill_username: false, fill_oauth_scope: false, **kw) click to toggle source
# File src/ruby/pb/test/client.rb, line 391
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