Skip to content

Instantly share code, notes, and snippets.

@copiousfreetime
Created July 27, 2021 15:34
Show Gist options
  • Select an option

  • Save copiousfreetime/e733dd0d193ff9d25afc25f599986cba to your computer and use it in GitHub Desktop.

Select an option

Save copiousfreetime/e733dd0d193ff9d25afc25f599986cba to your computer and use it in GitHub Desktop.

Revisions

  1. copiousfreetime created this gist Jul 27, 2021.
    51 changes: 51 additions & 0 deletions client-failure-testing.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    #!/usr/bin/env ruby

    require 'optimist'
    require 'thread'
    require 'socket'

    opts = Optimist::options do
    opt :host, "Hostname to connect to", default: "127.0.0.1"
    opt :port, "Port to connect to", default: 4332
    opt :timeout, "Connection timeout", type: :integer
    end
    sock_opts = {}

    puts "Connecting to #{opts[:host]}:#{opts[:port]}"
    if opts[:timeout] then
    puts " with timeout #{opts[:timeout]}"
    sock_opts[:connect_timeout] = opts[:timeout]
    end

    client_thread = Thread.new do
    begin
    Thread.current.name = "client"
    puts "Starting to test"
    puts "Opening socket"
    socket = ::Socket.tcp(opts[:host], opts[:port], sock_opts)
    puts "Socket: #{socket}"
    rescue => e
    puts "Socket exception: #{e.class} #{e}"
    ensure
    puts "ensure Thread is in #{Thread.current.status}"
    end
    end

    puts "Client thread joining..."
    join_result = client_thread.join(5)
    puts "Client thread joined : #{join_result}"
    if join_result.nil? then
    puts "#{client_thread.name} before kill: #{client_thread.status}"
    client_thread.kill
    sleep 0.1
    puts "#{client_thread.name} after kill: #{client_thread.status}"
    end

    loop do
    puts "exit loop : alive 1 -> #{client_thread.alive?}"
    break unless client_thread.alive?
    puts "exit loop : alive 2 -> #{client_thread.alive?} #{client_thread.status}"
    sleep 0.5
    end

    puts "done"