Skip to content

Commit

Permalink
Use SIGINT to kill netcat
Browse files Browse the repository at this point in the history
Some tests failed with EBADF because their socket was closed on GC
before reading from/writing to it. This was likely caused by the
port-knocking technique to kill nc.
  • Loading branch information
pietern committed Sep 2, 2011
1 parent f7ffb60 commit 0efd8a5
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions test/connection_test.rb
Expand Up @@ -12,28 +12,23 @@ def sockopt(sock, opt, unpack = "i")
sock.getsockopt(Socket::SOL_SOCKET, opt).unpack("i").first
end

def knock(port)
sock = TCPSocket.new("localhost", port)
sock.close
rescue
end

def listen(port = DEFAULT_PORT)
IO.popen("nc -l #{port}", "r+") do |io|
sleep 0.1 # Give nc a little time to start listening

begin
Thread.new do
timeout = Thread.new do
sleep 10 # Tests should complete in 10s
knock port
Process.kill("SIGINT", io.pid) rescue Errno::ESRCH
end

yield io
ensure
hiredis.disconnect if hiredis.connected?
# Abort timeout
timeout.kill

# Connect to make sure netcat exits
knock port
# Netcat waits forever if no-one connected, so kill it.
Process.kill("SIGINT", io.pid) rescue Errno::ESRCH
end
end
end
Expand Down

0 comments on commit 0efd8a5

Please sign in to comment.