Debian Codegraph @

src/ruby/end2end/end2end_common.rb

raw
1 #!/usr/bin/env ruby
2
3 # Copyright 2015 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 this_dir = File.expand_path(File.dirname(__FILE__))
18 protos_lib_dir = File.join(this_dir, 'lib')
19 grpc_lib_dir = File.join(File.dirname(this_dir), 'lib')
20 $LOAD_PATH.unshift(grpc_lib_dir) unless $LOAD_PATH.include?(grpc_lib_dir)
21 $LOAD_PATH.unshift(protos_lib_dir) unless $LOAD_PATH.include?(protos_lib_dir)
22 $LOAD_PATH.unshift(this_dir) unless $LOAD_PATH.include?(this_dir)
23
24 require 'grpc'
25 require 'echo_services_pb'
26 require 'client_control_services_pb'
27 require 'socket'
28 require 'optparse'
29 require 'thread'
30 require 'timeout'
31 require 'English' # see https://github.com/bbatsov/rubocop/issues/1747
32 require_relative '../spec/support/helpers'
33
34 include GRPC::Spec::Helpers
35
36 # Useful to update a value within a do block
37 class MutableValue
38 attr_accessor :value
39
40 def initialize(value)
41 @value = value
42 end
43 end
44
45 # GreeterServer is simple server that implements the Helloworld Greeter server.
46 class EchoServerImpl < Echo::EchoServer::Service
47 # say_hello implements the SayHello rpc method.
48 def echo(echo_req, _)
49 Echo::EchoReply.new(response: echo_req.request)
50 end
51 end
52
53 # ServerRunner starts an "echo server" that test clients can make calls to
54 class ServerRunner
55 attr_accessor :server_creds
56
57 def initialize(service_impl, rpc_server_args: {})
58 @service_impl = service_impl
59 @rpc_server_args = rpc_server_args
60 @server_creds = :this_port_is_insecure
61 end
62
63 def run
64 @srv = new_rpc_server_for_testing(@rpc_server_args)
65 port = @srv.add_http2_port('0.0.0.0:0', @server_creds)
66 @srv.handle(@service_impl)
67
68 @thd = Thread.new do
69 @srv.run
70 end
71 @srv.wait_till_running
72 port
73 end
74
75 def stop
76 @srv.stop
77 @thd.join
78 fail 'server not stopped' unless @srv.stopped?
79 end
80 end
81
82 # ClientController is used to start a child process and communicate
83 # with it for test orchestration purposes via RPCs.
84 class ClientController < ClientControl::ParentController::Service
85 attr_reader :stub, :client_pid
86
87 def initialize(client_main, server_port)
88 this_dir = File.expand_path(File.dirname(__FILE__))
89 client_path = File.join(this_dir, client_main)
90 @server = new_rpc_server_for_testing(poll_period: 3)
91 port = @server.add_http2_port('localhost:0', :this_port_is_insecure)
92 server_thread = Thread.new do
93 @server.handle(self)
94 @server.run
95 end
96 @server.wait_till_running
97 @client_controller_port_mu = Mutex.new
98 @client_controller_port_cv = ConditionVariable.new
99 @client_controller_port = nil
100 @client_pid = Process.spawn(RbConfig.ruby,
101 client_path,
102 "--parent_controller_port=#{port}",
103 "--server_port=#{server_port}")
104 begin
105 Timeout.timeout(60) do
106 @client_controller_port_mu.synchronize do
107 while @client_controller_port.nil?
108 @client_controller_port_cv.wait(@client_controller_port_mu)
109 end
110 end
111 end
112 rescue => e
113 fail "timeout waiting for child process to report port. error: #{e}"
114 end
115 @server.stop
116 server_thread.join
117 @stub = ClientControl::ClientController::Stub.new(
118 "localhost:#{@client_controller_port}", :this_channel_is_insecure)
119 end
120
121 def set_client_controller_port(req, _)
122 @client_controller_port_mu.synchronize do
123 unless @client_controller_port.nil?
124 fail 'client controller port already set'
125 end
126 @client_controller_port = req.port
127 @client_controller_port_cv.broadcast
128 end
129 ClientControl::Void.new
130 end
131 end
132
133 def report_controller_port_to_parent(parent_controller_port, client_controller_port)
134 unless parent_controller_port.to_i > 0
135 fail "bad parent control port: |#{parent_controller_port}|"
136 end
137 stub = ClientControl::ParentController::Stub.new(
138 "localhost:#{parent_controller_port.to_i}", :this_channel_is_insecure)
139 m = ClientControl::Port.new
140 m.port = client_controller_port.to_i
141 stub.set_client_controller_port(m, deadline: Time.now + 10)
142 end