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  construct_many(test_proc)
40
41  GC.enable
42  construct_many(test_proc)
43
44  GC.start
45  construct_many(test_proc)
46end
47
48def 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'
58end
59
60# default (no gc_stress and no concurrency_stress)
61def run_default_test(test_proc)
62  thd = Thread.new do
63    test_proc.call
64  end
65  test_proc.call
66  thd.join
67end
68
69def get_test_proc(grpc_class)
70  case grpc_class
71  when 'channel'
72    return proc do
73      GRPC::Core::Channel.new('dummy_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 'call_credentials'
84    return proc do
85      GRPC::Core::CallCredentials.new(proc { |noop| noop })
86    end
87  when 'compression_options'
88    return proc do
89      GRPC::Core::CompressionOptions.new
90    end
91  else
92    fail "bad --grpc_class=#{grpc_class} param"
93  end
94end
95
96def main
97  grpc_class = ''
98  stress_test = ''
99  OptionParser.new do |opts|
100    opts.on('--grpc_class=P', String) do |p|
101      grpc_class = p
102    end
103    opts.on('--stress_test=P') do |p|
104      stress_test = p
105    end
106  end.parse!
107
108  test_proc = get_test_proc(grpc_class)
109
110  # the different test configs need to be ran
111  # in separate processes, since each one tests
112  # clean shutdown in a different way
113  case stress_test
114  when 'gc'
115    p 'run gc stress'
116    run_gc_stress_test(test_proc)
117  when 'concurrency'
118    p 'run concurrency stress'
119    run_concurrency_stress_test(test_proc)
120  when ''
121    p 'run default'
122    run_default_test(test_proc)
123  else
124    fail "bad --stress_test=#{stress_test} param"
125  end
126end
127
128main
129