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