Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions spec/tcp_server_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require File.expand_path('../spec_helper', __FILE__)
require 'timeout'

TIMEOUT = 0.010
HOST = '127.0.0.1'
Expand Down Expand Up @@ -39,13 +40,31 @@ def on_message(data)
@data = data
end

# The reactor can be an order of magnitude slower under valgrind, so sleeping a
# fixed interval and assuming the event was processed by then makes these specs
# fail intermittently. Wait for the result instead. The timeout is generous
# enough that a slow machine still passes, and a genuine hang raises
# Timeout::Error here instead of blocking the suite forever.
WAIT_TIMEOUT = 5.0

def wait_until(timeout = WAIT_TIMEOUT)
Timeout.timeout(timeout) do
sleep 0.001 until yield
end
end

def test_run(data = nil)
@data = ""
reactor = Coolio::Loop.new
server = Cool.io::TCPServer.new(HOST, PORT, MyConnection, method(:on_message))
reactor.attach(server)
thread = Thread.new { reactor.run }
send_data(data) if data
sleep TIMEOUT
if data
send_data(data)
wait_until { @data == data }
else
sleep TIMEOUT
end
reactor.stop
server.detach
send_data('') # to leave from blocking loop
Expand Down Expand Up @@ -86,6 +105,7 @@ def test_run_once_timeout(timeout = TIMEOUT)
end

def test_run_timeout(data = nil, timeout = TIMEOUT)
@data = ""
reactor = Coolio::Loop.new
server = Cool.io::TCPServer.new(HOST, PORT, MyConnection, method(:on_message))
reactor.attach(server)
Expand All @@ -95,8 +115,12 @@ def test_run_timeout(data = nil, timeout = TIMEOUT)
reactor.run_once(timeout)
end
end
send_data(data) if data
sleep timeout
if data
send_data(data)
wait_until { @data == data }
else
sleep timeout
end
server.detach
running = false # another send is not required
thread.join
Expand Down