end2end_common.rb 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env ruby
  2. # Copyright 2015 gRPC authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. this_dir = File.expand_path(File.dirname(__FILE__))
  16. protos_lib_dir = File.join(this_dir, 'lib')
  17. grpc_lib_dir = File.join(File.dirname(this_dir), 'lib')
  18. $LOAD_PATH.unshift(grpc_lib_dir) unless $LOAD_PATH.include?(grpc_lib_dir)
  19. $LOAD_PATH.unshift(protos_lib_dir) unless $LOAD_PATH.include?(protos_lib_dir)
  20. $LOAD_PATH.unshift(this_dir) unless $LOAD_PATH.include?(this_dir)
  21. require 'grpc'
  22. require 'echo_services_pb'
  23. require 'client_control_services_pb'
  24. require 'socket'
  25. require 'optparse'
  26. require 'thread'
  27. require 'timeout'
  28. require 'English' # see https://github.com/bbatsov/rubocop/issues/1747
  29. require_relative '../spec/support/helpers'
  30. include GRPC::Spec::Helpers
  31. # GreeterServer is simple server that implements the Helloworld Greeter server.
  32. class EchoServerImpl < Echo::EchoServer::Service
  33. # say_hello implements the SayHello rpc method.
  34. def echo(echo_req, _)
  35. Echo::EchoReply.new(response: echo_req.request)
  36. end
  37. end
  38. # ServerRunner starts an "echo server" that test clients can make calls to
  39. class ServerRunner
  40. def initialize(service_impl, rpc_server_args: {})
  41. @service_impl = service_impl
  42. @rpc_server_args = rpc_server_args
  43. end
  44. def run
  45. @srv = new_rpc_server_for_testing(@rpc_server_args)
  46. port = @srv.add_http2_port('0.0.0.0:0', :this_port_is_insecure)
  47. @srv.handle(@service_impl)
  48. @thd = Thread.new do
  49. @srv.run
  50. end
  51. @srv.wait_till_running
  52. port
  53. end
  54. def stop
  55. @srv.stop
  56. @thd.join
  57. fail 'server not stopped' unless @srv.stopped?
  58. end
  59. end
  60. def start_client(client_main, server_port)
  61. this_dir = File.expand_path(File.dirname(__FILE__))
  62. tmp_server = TCPServer.new(0)
  63. client_control_port = tmp_server.local_address.ip_port
  64. tmp_server.close
  65. client_path = File.join(this_dir, client_main)
  66. client_pid = Process.spawn(RbConfig.ruby,
  67. client_path,
  68. "--client_control_port=#{client_control_port}",
  69. "--server_port=#{server_port}")
  70. control_stub = ClientControl::ClientController::Stub.new(
  71. "localhost:#{client_control_port}", :this_channel_is_insecure)
  72. [control_stub, client_pid]
  73. end
  74. def cleanup(control_stub, client_pid, server_runner)
  75. control_stub.shutdown(ClientControl::Void.new)
  76. Process.wait(client_pid)
  77. client_exit_code = $CHILD_STATUS
  78. if client_exit_code != 0
  79. fail "term sig test failure: client exit code: #{client_exit_code}"
  80. end
  81. server_runner.stop
  82. end