• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env ruby
2
3# Copyright 2016 gRPC authors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# smoke test for a grpc-using app that receives and
18# handles process-ending signals
19
20require_relative './end2end_common'
21
22# A service that calls back it's received_rpc_callback
23# upon receiving an RPC. Used for synchronization/waiting
24# for child process to start.
25class ClientStartedService < Echo::EchoServer::Service
26  def initialize(received_rpc_callback)
27    @received_rpc_callback = received_rpc_callback
28  end
29
30  def echo(echo_req, _)
31    @received_rpc_callback.call unless @received_rpc_callback.nil?
32    @received_rpc_callback = nil
33    Echo::EchoReply.new(response: echo_req.request)
34  end
35end
36
37def main
38  STDERR.puts 'start server'
39  client_started = false
40  client_started_mu = Mutex.new
41  client_started_cv = ConditionVariable.new
42  received_rpc_callback = proc do
43    client_started_mu.synchronize do
44      client_started = true
45      client_started_cv.signal
46    end
47  end
48
49  client_started_service = ClientStartedService.new(received_rpc_callback)
50  server_runner = ServerRunner.new(client_started_service)
51  server_port = server_runner.run
52  STDERR.puts 'start client'
53  control_stub, client_pid = start_client('graceful_sig_handling_client.rb', server_port)
54
55  client_started_mu.synchronize do
56    client_started_cv.wait(client_started_mu) until client_started
57  end
58
59  control_stub.do_echo_rpc(
60    ClientControl::DoEchoRpcRequest.new(request: 'hello'))
61
62  STDERR.puts 'killing client'
63  Process.kill('SIGINT', client_pid)
64  Process.wait(client_pid)
65  client_exit_status = $CHILD_STATUS
66
67  if client_exit_status.exited?
68    if client_exit_status.exitstatus != 0
69      STDERR.puts 'Client did not close gracefully'
70      exit(1)
71    end
72  else
73    STDERR.puts 'Client did not close gracefully'
74    exit(1)
75  end
76
77  STDERR.puts 'Client ended gracefully'
78
79  # no need to call cleanup, client should already be dead
80  server_runner.stop
81end
82
83main
84