Top Level Namespace

Defined Under Namespace

Modules: GRPC, Grpc, Math, RubyLogger, StdoutLogger Classes: BidiService, Calculator, CheckCallAfterFinishedService, DebugMessageTestService, EchoMsg, EchoService, EmptyService, EncodeDecodeMsg, EnumeratorQueue, FailingService, Fibber, GoodMsg, GoogleRpcStatusTestService, NoProto, NoProtoMsg, NoProtoService, NoRpcImplementation, NoStatusDetailsBinTestService, SlowService, SslTestService, Struct, SynchronizedCancellationService, TestClientInterceptor, TestServerInterceptor

Constant Summary collapse

Server =
GRPC::Core::Server
NoProtoStub =
NoProtoService.rpc_stub_class
SslTestServiceStub =
SslTestService.rpc_stub_class
EchoStub =
EchoService.rpc_stub_class
TimeConsts =
GRPC::Core::TimeConsts
StatusCodes =
GRPC::Core::StatusCodes
TEST_DEBUG_MESSAGE =
'raised by test server'.freeze
DebugMessageTestServiceStub =
DebugMessageTestService.rpc_stub_class
GenericService =
GRPC::GenericService
Dsl =
GenericService::Dsl
FailingStub =
FailingService.rpc_stub_class
SlowStub =
SlowService.rpc_stub_class
SynchronizedCancellationStub =
SynchronizedCancellationService.rpc_stub_class
CheckCallAfterFinishedServiceStub =
CheckCallAfterFinishedService.rpc_stub_class
BidiStub =
BidiService.rpc_stub_class
GoogleRpcStatusTestStub =
GoogleRpcStatusTestService.rpc_stub_class
NoStatusDetailsBinTestServiceStub =
NoStatusDetailsBinTestService.rpc_stub_class

Constants included from GRPC::Spec::Helpers

GRPC::Spec::Helpers::RpcServer

Constants included from GRPC::Core::StatusCodes

GRPC::Core::StatusCodes::ABORTED, GRPC::Core::StatusCodes::ALREADY_EXISTS, GRPC::Core::StatusCodes::CANCELLED, GRPC::Core::StatusCodes::DATA_LOSS, GRPC::Core::StatusCodes::DEADLINE_EXCEEDED, GRPC::Core::StatusCodes::FAILED_PRECONDITION, GRPC::Core::StatusCodes::INTERNAL, GRPC::Core::StatusCodes::INVALID_ARGUMENT, GRPC::Core::StatusCodes::NOT_FOUND, GRPC::Core::StatusCodes::OK, GRPC::Core::StatusCodes::OUT_OF_RANGE, GRPC::Core::StatusCodes::PERMISSION_DENIED, GRPC::Core::StatusCodes::RESOURCE_EXHAUSTED, GRPC::Core::StatusCodes::UNAUTHENTICATED, GRPC::Core::StatusCodes::UNAVAILABLE, GRPC::Core::StatusCodes::UNIMPLEMENTED, GRPC::Core::StatusCodes::UNKNOWN

Instance Method Summary collapse

Methods included from GRPC::Spec::Helpers

#build_insecure_stub, #build_rpc_server, #new_core_server_for_testing, #new_rpc_server_for_testing, #run_services_on_server, #update_server_args_hash

Methods included from GRPC::Core::TimeConsts

from_relative_time

Instance Method Details

#can_run_codegen_checkObject



21
22
23
# File 'src/ruby/spec/pb/health/checker_spec.rb', line 21

def can_run_codegen_check
  system('which grpc_ruby_plugin') && system('which protoc')
end

#check_md(wanted_md, received_md) ⇒ Object



22
23
24
25
26
27
28
# File 'src/ruby/spec/generic/rpc_server_spec.rb', line 22

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

#check_op_view_of_finished_client_call(op_view, expected_metadata, expected_trailing_metadata) ⇒ Object

check that methods on a finished/closed call t crash



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'src/ruby/spec/generic/client_stub_spec.rb', line 40

def check_op_view_of_finished_client_call(op_view,
                                          ,
                                          )
  # use read_response_stream to try to iterate through
  # possible response stream
  fail('need something to attempt reads') unless block_given?
  expect do
    resp = op_view.execute
    yield resp
  end.to raise_error(GRPC::Core::CallError)

  expect { op_view.start_call }.to raise_error(RuntimeError)

  sanity_check_values_of_accessors(op_view,
                                   ,
                                   )

  expect do
    op_view.wait
    op_view.cancel
    op_view.write_flag = 1
  end.to_not raise_error
end

#client_certObject



24
25
26
27
28
29
# File 'src/ruby/spec/client_auth_spec.rb', line 24

def client_cert
  test_root = File.join(File.dirname(__FILE__), 'testdata')
  cert = File.open(File.join(test_root, 'client.pem')).read
  fail unless cert.is_a?(String)
  cert
end

#close_active_server_call(active_server_call) ⇒ Object



86
87
88
89
# File 'src/ruby/spec/generic/client_stub_spec.rb', line 86

def close_active_server_call(active_server_call)
  active_server_call.send(:set_input_stream_done)
  active_server_call.send(:set_output_stream_done)
end

#create_channel_credsObject



17
18
19
20
21
22
# File 'src/ruby/spec/client_auth_spec.rb', line 17

def create_channel_creds
  test_root = File.join(File.dirname(__FILE__), 'testdata')
  files = ['ca.pem', 'client.key', 'client.pem']
  creds = files.map { |f| File.open(File.join(test_root, f)).read }
  GRPC::Core::ChannelCredentials.new(creds[0], creds[1], creds[2])
end

#create_server_credsObject



31
32
33
34
35
36
37
38
39
40
# File 'src/ruby/spec/client_auth_spec.rb', line 31

def create_server_creds
  test_root = File.join(File.dirname(__FILE__), 'testdata')
  GRPC.logger.info("test root: #{test_root}")
  files = ['ca.pem', 'server1.key', 'server1.pem']
  creds = files.map { |f| File.open(File.join(test_root, f)).read }
  GRPC::Core::ServerCredentials.new(
    creds[0],
    [{ private_key: creds[1], cert_chain: creds[2] }],
    true) # force client auth
end

#do_div(stub) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'src/ruby/bin/math_client.rb', line 44

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)
  GRPC.logger.info("Answer: #{resp.inspect}")
  GRPC.logger.info('----------------')
end

#do_div_many(stub) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'src/ruby/bin/math_client.rb', line 77

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)
  resp.each do |r|
    GRPC.logger.info("Answer: #{r.inspect}")
  end
  GRPC.logger.info('----------------')
end

#do_fib(stub) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'src/ruby/bin/math_client.rb', line 65

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)
  resp.each do |r|
    GRPC.logger.info("Answer: #{r.inspect}")
  end
  GRPC.logger.info('----------------')
end

#do_sum(stub) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'src/ruby/bin/math_client.rb', line 54

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

#load_test_certsObject



150
151
152
153
154
155
# File 'src/ruby/bin/math_server.rb', line 150

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

#mainObject



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'src/ruby/bin/math_server.rb', line 163

def main
  options = {
    'host' => 'localhost:7071',
    'secure' => false
  }
  OptionParser.new do |opts|
    opts.banner = 'Usage: [--host <hostname>:<port>] [--secure|-s]'
    opts.on('--host HOST', '<hostname>:<port>') do |v|
      options['host'] = v
    end
    opts.on('-s', '--secure', 'access using test creds') do |v|
      options['secure'] = v
    end
  end.parse!

  s = GRPC::RpcServer.new
  if options['secure']
    s.add_http2_port(options['host'], test_server_creds)
    GRPC.logger.info("... running securely on #{options['host']}")
  else
    s.add_http2_port(options['host'], :this_port_is_insecure)
    GRPC.logger.info("... running insecurely on #{options['host']}")
  end

  s.handle(Calculator)
  s.run_till_terminated
end

#sanity_check_values_of_accessors(op_view, expected_metadata, expected_trailing_metadata) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'src/ruby/spec/generic/client_stub_spec.rb', line 64

def sanity_check_values_of_accessors(op_view,
                                     ,
                                     )
  expected_status = Struct::Status.new
  expected_status.code = 0
  expected_status.details = 'OK'
  expected_status. = 

  expect(op_view.status).to eq(expected_status)
  expect(op_view.).to eq()
  expect(op_view.).to eq()

  expect(op_view.cancelled?).to be(false)
  expect(op_view.write_flag).to be(nil)

  # The deadline attribute of a call can be either
  # a GRPC::Core::TimeSpec or a Time, which are mutually exclusive.
  # TODO: fix so that the accessor always returns the same type.
  expect(op_view.deadline.is_a?(GRPC::Core::TimeSpec) ||
         op_view.deadline.is_a?(Time)).to be(true)
end

#start_server(port = 0) ⇒ Object



21
22
23
24
25
26
27
28
# File 'src/ruby/spec/channel_connection_spec.rb', line 21

def start_server(port = 0)
  @srv = new_rpc_server_for_testing(pool_size: 1)
  server_port = @srv.add_http2_port("localhost:#{port}", :this_port_is_insecure)
  @srv.handle(EchoService)
  @server_thd = Thread.new { @srv.run }
  @srv.wait_till_running
  server_port
end

#stop_serverObject



30
31
32
33
34
35
# File 'src/ruby/spec/channel_connection_spec.rb', line 30

def stop_server
  expect(@srv.stopped?).to be(false)
  @srv.stop
  @server_thd.join
  expect(@srv.stopped?).to be(true)
end

#test_credsObject



54
55
56
57
# File 'src/ruby/bin/noproto_client.rb', line 54

def test_creds
  certs = load_test_certs
  GRPC::Core::ChannelCredentials.new(certs[0])
end

#test_server_credsObject



157
158
159
160
161
# File 'src/ruby/bin/math_server.rb', line 157

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) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'src/ruby/spec/generic/client_stub_spec.rb', line 19

def wakey_thread(&blk)
  n = GRPC::Notifier.new
  t = Thread.new do
    blk.call(n)
  end
  t.abort_on_exception = true
  n.wait
  t
end

#with_protos(file_paths) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'src/ruby/spec/pb/codegen/package_option_spec.rb', line 49

def with_protos(file_paths)
  fail 'CONFIG env variable unexpectedly unset' unless ENV['CONFIG']
  bins_sub_dir = ENV['CONFIG']

  pb_dir = File.dirname(__FILE__)
  bins_dir = File.join('..', '..', '..', '..', '..', 'bins', bins_sub_dir)

  plugin = File.join(bins_dir, 'grpc_ruby_plugin')
  protoc = File.join(bins_dir, 'protobuf', 'protoc')

  # Generate the service from the proto
  Dir.mktmpdir(nil, File.dirname(__FILE__)) do |tmp_dir|
    gen_file = system(protoc,
                      '-I.',
                      *file_paths,
                      "--grpc_out=#{tmp_dir}", # generate the service
                      "--ruby_out=#{tmp_dir}", # generate the definitions
                      "--plugin=protoc-gen-grpc=#{plugin}",
                      chdir: pb_dir,
                      out: File::NULL)

    expect(gen_file).to be_truthy
    begin
      $LOAD_PATH.push(tmp_dir)
      yield
    ensure
      $LOAD_PATH.delete(tmp_dir)
    end
  end
end