Home | History | Annotate | Download | only in end2end
      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 require_relative './end2end_common'
     18 
     19 Thread.abort_on_exception = true
     20 
     21 include GRPC::Core::ConnectivityStates
     22 
     23 def watch_state(ch, sleep_time)
     24   thd = Thread.new do
     25     state = ch.connectivity_state(false)
     26     fail "non-idle state: #{state}" unless state == IDLE
     27     ch.watch_connectivity_state(IDLE, Time.now + 360)
     28   end
     29   # sleep to get the thread into the middle of a
     30   # "watch connectivity state" call
     31   sleep sleep_time
     32   thd.kill
     33 end
     34 
     35 def run_multiple_killed_watches(num_threads, sleep_time)
     36   channels = []
     37   num_threads.times do
     38     ch = GRPC::Core::Channel.new('dummy_host',
     39                                  nil, :this_channel_is_insecure)
     40     watch_state(ch, sleep_time)
     41     channels << ch
     42   end
     43 
     44   # checking state should still be safe to call
     45   channels.each do |c|
     46     connectivity_state = c.connectivity_state(false)
     47     # The state should be FATAL_FAILURE in the case that it was interrupted
     48     # while watching connectivity state, and IDLE if it we never started
     49     # watching the channel's connectivity state
     50     unless [FATAL_FAILURE, IDLE].include?(connectivity_state)
     51       fail "unexpected connectivity state: #{connectivity_state}"
     52     end
     53   end
     54 end
     55 
     56 def main
     57   STDERR.puts '10 iterations, sleep 0.1 before killing thread'
     58   run_multiple_killed_watches(10, 0.1)
     59   STDERR.puts '1000 iterations, sleep 0.001 before killing thread'
     60   run_multiple_killed_watches(1000, 0.001)
     61 end
     62 
     63 main
     64