Created
July 27, 2021 15:34
-
-
Save copiousfreetime/e733dd0d193ff9d25afc25f599986cba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment