graceful_sig_handling_client.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. require_relative './end2end_common'
  16. # Test client. Sends RPC's as normal but process also has signal handlers
  17. class SigHandlingClientController < ClientControl::ClientController::Service
  18. def initialize(stub)
  19. @stub = stub
  20. end
  21. def do_echo_rpc(req, _)
  22. response = @stub.echo(Echo::EchoRequest.new(request: req.request))
  23. fail 'bad response' unless response.response == req.request
  24. ClientControl::Void.new
  25. end
  26. end
  27. def main
  28. client_control_port = ''
  29. server_port = ''
  30. OptionParser.new do |opts|
  31. opts.on('--client_control_port=P', String) do |p|
  32. client_control_port = p
  33. end
  34. opts.on('--server_port=P', String) do |p|
  35. server_port = p
  36. end
  37. end.parse!
  38. # Allow a few seconds to be safe.
  39. srv = new_rpc_server_for_testing
  40. srv.add_http2_port("0.0.0.0:#{client_control_port}",
  41. :this_port_is_insecure)
  42. stub = Echo::EchoServer::Stub.new("localhost:#{server_port}",
  43. :this_channel_is_insecure)
  44. control_service = SigHandlingClientController.new(stub)
  45. srv.handle(control_service)
  46. server_thread = Thread.new do
  47. srv.run_till_terminated_or_interrupted(['int'])
  48. end
  49. srv.wait_till_running
  50. # send a first RPC to notify the parent process that we've started
  51. stub.echo(Echo::EchoRequest.new(request: 'client/child started'))
  52. server_thread.join
  53. end
  54. main