2006-05-18 09:42:52 -04:00
|
|
|
require "test/unit"
|
|
|
|
require "webrick/utils"
|
|
|
|
|
|
|
|
class TestWEBrickUtils < Test::Unit::TestCase
|
|
|
|
def assert_expired(flag, m)
|
|
|
|
if m == WEBrick::Utils
|
|
|
|
handler = WEBrick::Utils::TimeoutHandler.instance
|
|
|
|
assert_equal(flag, handler.instance_eval{ @timeout_info.empty? })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def do_test_timeout(m)
|
|
|
|
ex = Class.new(StandardError)
|
|
|
|
|
|
|
|
assert_equal(:foo, m.timeout(10){ :foo })
|
|
|
|
assert_expired(true, m)
|
|
|
|
|
|
|
|
i = 0
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(Timeout::Error){
|
2006-05-18 09:42:52 -04:00
|
|
|
m.timeout(2){
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(Timeout::Error){ m.timeout(1){ i += 1; sleep } }
|
2006-05-18 09:42:52 -04:00
|
|
|
assert_expired(false, m)
|
|
|
|
i += 1
|
|
|
|
sleep
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_equal(2, i)
|
|
|
|
assert_expired(true, m)
|
|
|
|
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(Timeout::Error){ m.timeout(0.1){ sleep } }
|
2006-05-18 09:42:52 -04:00
|
|
|
assert_expired(true, m)
|
|
|
|
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(ex){ m.timeout(0.1, ex){ sleep } }
|
2006-05-18 09:42:52 -04:00
|
|
|
assert_expired(true, m)
|
|
|
|
|
|
|
|
i = 0
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(ex){
|
2006-05-18 09:42:52 -04:00
|
|
|
m.timeout(10){
|
|
|
|
m.timeout(1, ex){ i += 1; sleep }
|
|
|
|
}
|
|
|
|
sleep
|
|
|
|
}
|
|
|
|
assert_equal(1, i)
|
|
|
|
assert_expired(true, m)
|
|
|
|
|
|
|
|
i = 0
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(Timeout::Error){
|
2006-05-18 09:42:52 -04:00
|
|
|
m.timeout(1){
|
|
|
|
m.timeout(10, ex){ i += 1; sleep }
|
|
|
|
}
|
|
|
|
sleep
|
|
|
|
}
|
|
|
|
assert_equal(1, i)
|
|
|
|
assert_expired(true, m)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_webrick_timeout
|
|
|
|
do_test_timeout(WEBrick::Utils)
|
|
|
|
end
|
|
|
|
|
2015-01-02 01:53:12 -05:00
|
|
|
def test_create_listeners
|
|
|
|
listeners = WEBrick::Utils.create_listeners("127.0.0.1", "9999")
|
|
|
|
srv = listeners.first
|
|
|
|
assert_equal true, srv.is_a?(TCPServer)
|
|
|
|
assert_equal ["AF_INET", 9999, "127.0.0.1", "127.0.0.1"], srv.addr
|
|
|
|
end
|
|
|
|
|
2006-05-18 09:42:52 -04:00
|
|
|
end
|