Debian Codegraph @

src/ruby/end2end/grpc_class_init_client.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 # For GRPC::Core classes, which use the grpc c-core, object init
18 # is interesting because it's related to overall library init.
19
20 require_relative './end2end_common'
21
22 def construct_many(test_proc)
23 thds = []
24 4.times do
25 thds << Thread.new do
26 20.times do
27 test_proc.call
28 end
29 end
30 end
31 20.times do
32 test_proc.call
33 end
34 thds.each(&:join)
35 end
36
37 def run_gc_stress_test(test_proc)
38 GC.disable
39 construct_many(test_proc)
40
41 GC.enable
42 construct_many(test_proc)
43
44 GC.start
45 construct_many(test_proc)
46 end
47
48 def run_concurrency_stress_test(test_proc)
49 100.times do
50 Thread.new do
51 test_proc.call
52 end
53 end
54
55 test_proc.call
56
57 fail '(expected) exception thrown while child thread initing class'
58 end
59
60 # default (no gc_stress and no concurrency_stress)
61 def run_default_test(test_proc)
62 thd = Thread.new do
63 test_proc.call
64 end
65 test_proc.call
66 thd.join
67 end
68
69 def get_test_proc(grpc_class)
70 case grpc_class
71 when 'channel'
72 return proc do
73 GRPC::Core::Channel.new('phony_host', nil, :this_channel_is_insecure)
74 end
75 when 'server'
76 return proc do
77 GRPC::Core::Server.new({})
78 end
79 when 'channel_credentials'
80 return proc do
81 GRPC::Core::ChannelCredentials.new
82 end
83 when 'xds_channel_credentials'
84 return proc do
85 GRPC::Core::XdsChannelCredentials.new(GRPC::Core::ChannelCredentials.new)
86 end
87 when 'server_credentials'
88 return proc do
89 test_root = File.join(File.dirname(__FILE__), '..', 'spec', 'testdata')
90 files = ['ca.pem', 'server1.key', 'server1.pem']
91 creds = files.map { |f| File.open(File.join(test_root, f)).read }
92 GRPC::Core::ServerCredentials.new(
93 creds[0],
94 [{ private_key: creds[1], cert_chain: creds[2] }],
95 true)
96 end
97 when 'xds_server_credentials'
98 return proc do
99 test_root = File.join(File.dirname(__FILE__), '..', 'spec', 'testdata')
100 files = ['ca.pem', 'server1.key', 'server1.pem']
101 creds = files.map { |f| File.open(File.join(test_root, f)).read }
102 GRPC::Core::XdsServerCredentials.new(
103 GRPC::Core::ServerCredentials.new(
104 creds[0],
105 [{ private_key: creds[1], cert_chain: creds[2] }],
106 true))
107 end
108 when 'call_credentials'
109 return proc do
110 GRPC::Core::CallCredentials.new(proc { |noop| noop })
111 end
112 when 'compression_options'
113 return proc do
114 GRPC::Core::CompressionOptions.new
115 end
116 else
117 fail "bad --grpc_class=#{grpc_class} param"
118 end
119 end
120
121 def main
122 grpc_class = ''
123 stress_test = ''
124 OptionParser.new do |opts|
125 opts.on('--grpc_class=P', String) do |p|
126 grpc_class = p
127 end
128 opts.on('--stress_test=P') do |p|
129 stress_test = p
130 end
131 end.parse!
132
133 test_proc = get_test_proc(grpc_class)
134
135 # the different test configs need to be ran
136 # in separate processes, since each one tests
137 # clean shutdown in a different way
138 case stress_test
139 when 'gc'
140 p 'run gc stress'
141 run_gc_stress_test(test_proc)
142 when 'concurrency'
143 p 'run concurrency stress'
144 run_concurrency_stress_test(test_proc)
145 when ''
146 p 'run default'
147 run_default_test(test_proc)
148 else
149 fail "bad --stress_test=#{stress_test} param"
150 end
151 end
152
153 main