1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
require 'eventmachine'
require 'test/unit'
require 'rbconfig'
require 'socket'
class Test::Unit::TestCase
class EMTestTimeout < StandardError ; end
def setup_timeout(timeout = TIMEOUT_INTERVAL)
EM.schedule {
EM.add_timer(timeout) {
raise EMTestTimeout, "Test was cancelled after #{timeout} seconds."
}
}
end
def port_in_use?(port, host="127.0.0.1")
s = TCPSocket.new(host, port)
s.close
s
rescue Errno::ECONNREFUSED
false
end
def next_port
@@port ||= 9000
begin
@@port += 1
end while port_in_use?(@@port)
@@port
end
def exception_class
jruby? ? NativeException : RuntimeError
end
module PlatformHelper
# http://blog.emptyway.com/2009/11/03/proper-way-to-detect-windows-platform-in-ruby/
def windows?
RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
end
# http://stackoverflow.com/questions/1342535/how-can-i-tell-if-im-running-from-jruby-vs-ruby/1685970#1685970
def jruby?
defined? JRUBY_VERSION
end
end
include PlatformHelper
extend PlatformHelper
# Tests run significantly slower on windows. YMMV
TIMEOUT_INTERVAL = windows? ? 1 : 0.25
def silent
backup, $VERBOSE = $VERBOSE, nil
begin
yield
ensure
$VERBOSE = backup
end
end
end
|