2006-05-18 09:42:52 -04:00
|
|
|
require "test/unit"
|
|
|
|
require "webrick/utils"
|
|
|
|
|
|
|
|
class TestWEBrickUtils < Test::Unit::TestCase
|
2015-07-14 00:55:03 -04:00
|
|
|
def assert_expired(m)
|
|
|
|
Thread.handle_interrupt(Timeout::Error => :never, EX => :never) do
|
|
|
|
assert_empty(m::TimeoutHandler.instance.instance_variable_get(:@timeout_info))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_not_expired(m)
|
|
|
|
Thread.handle_interrupt(Timeout::Error => :never, EX => :never) do
|
|
|
|
assert_not_empty(m::TimeoutHandler.instance.instance_variable_get(:@timeout_info))
|
2006-05-18 09:42:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-13 22:20:34 -04:00
|
|
|
EX = Class.new(StandardError)
|
2006-05-18 09:42:52 -04:00
|
|
|
|
2015-07-13 22:20:34 -04:00
|
|
|
def test_no_timeout
|
|
|
|
m = WEBrick::Utils
|
2006-05-18 09:42:52 -04:00
|
|
|
assert_equal(:foo, m.timeout(10){ :foo })
|
2015-07-14 00:55:03 -04:00
|
|
|
assert_expired(m)
|
2015-07-13 22:20:34 -04:00
|
|
|
end
|
2006-05-18 09:42:52 -04:00
|
|
|
|
2015-07-13 22:20:34 -04:00
|
|
|
def test_nested_timeout_outer
|
|
|
|
m = WEBrick::Utils
|
2006-05-18 09:42:52 -04:00
|
|
|
i = 0
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(Timeout::Error){
|
2015-07-22 05:04:57 -04:00
|
|
|
m.timeout(0.2){
|
|
|
|
assert_raise(Timeout::Error){ m.timeout(0.1){ i += 1; sleep } }
|
2015-07-14 00:55:03 -04:00
|
|
|
assert_not_expired(m)
|
2006-05-18 09:42:52 -04:00
|
|
|
i += 1
|
|
|
|
sleep
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_equal(2, i)
|
2015-07-14 00:55:03 -04:00
|
|
|
assert_expired(m)
|
2015-07-13 22:20:34 -04:00
|
|
|
end
|
2006-05-18 09:42:52 -04:00
|
|
|
|
2015-07-13 22:20:34 -04:00
|
|
|
def test_timeout_default_execption
|
|
|
|
m = WEBrick::Utils
|
2015-07-13 22:22:51 -04:00
|
|
|
assert_raise(Timeout::Error){ m.timeout(0.01){ sleep } }
|
2015-07-14 00:55:03 -04:00
|
|
|
assert_expired(m)
|
2015-07-13 22:20:34 -04:00
|
|
|
end
|
2006-05-18 09:42:52 -04:00
|
|
|
|
2015-07-13 22:20:34 -04:00
|
|
|
def test_timeout_custom_exception
|
|
|
|
m = WEBrick::Utils
|
|
|
|
ex = EX
|
2015-07-13 22:22:51 -04:00
|
|
|
assert_raise(ex){ m.timeout(0.01, ex){ sleep } }
|
2015-07-14 00:55:03 -04:00
|
|
|
assert_expired(m)
|
2015-07-13 22:20:34 -04:00
|
|
|
end
|
2006-05-18 09:42:52 -04:00
|
|
|
|
2015-07-13 22:20:34 -04:00
|
|
|
def test_nested_timeout_inner_custom_exception
|
|
|
|
m = WEBrick::Utils
|
|
|
|
ex = EX
|
2006-05-18 09:42:52 -04:00
|
|
|
i = 0
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(ex){
|
2006-05-18 09:42:52 -04:00
|
|
|
m.timeout(10){
|
2015-07-13 22:22:51 -04:00
|
|
|
m.timeout(0.01, ex){ i += 1; sleep }
|
2006-05-18 09:42:52 -04:00
|
|
|
}
|
|
|
|
sleep
|
|
|
|
}
|
|
|
|
assert_equal(1, i)
|
2015-07-14 00:55:03 -04:00
|
|
|
assert_expired(m)
|
2015-07-13 22:20:34 -04:00
|
|
|
end
|
2006-05-18 09:42:52 -04:00
|
|
|
|
2015-07-13 22:20:34 -04:00
|
|
|
def test_nested_timeout_outer_custom_exception
|
|
|
|
m = WEBrick::Utils
|
|
|
|
ex = EX
|
2006-05-18 09:42:52 -04:00
|
|
|
i = 0
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(Timeout::Error){
|
2015-07-13 22:22:51 -04:00
|
|
|
m.timeout(0.01){
|
|
|
|
m.timeout(1.0, ex){ i += 1; sleep }
|
2006-05-18 09:42:52 -04:00
|
|
|
}
|
|
|
|
sleep
|
|
|
|
}
|
|
|
|
assert_equal(1, i)
|
2015-07-14 00:55:03 -04:00
|
|
|
assert_expired(m)
|
2006-05-18 09:42:52 -04:00
|
|
|
end
|
|
|
|
|
2015-01-02 01:53:12 -05:00
|
|
|
def test_create_listeners
|
2015-01-11 01:11:40 -05:00
|
|
|
addr = listener_address(0)
|
|
|
|
port = addr.slice!(1)
|
|
|
|
assert_kind_of(Integer, port, "dynamically chosen port number")
|
|
|
|
assert_equal(["AF_INET", "127.0.0.1", "127.0.0.1"], addr)
|
|
|
|
|
|
|
|
assert_equal(["AF_INET", port, "127.0.0.1", "127.0.0.1"],
|
|
|
|
listener_address(port),
|
|
|
|
"specific port number")
|
|
|
|
|
|
|
|
assert_equal(["AF_INET", port, "127.0.0.1", "127.0.0.1"],
|
|
|
|
listener_address(port.to_s),
|
|
|
|
"specific port number string")
|
2015-01-02 01:53:12 -05:00
|
|
|
end
|
|
|
|
|
2015-01-11 01:11:40 -05:00
|
|
|
def listener_address(port)
|
|
|
|
listeners = WEBrick::Utils.create_listeners("127.0.0.1", port)
|
|
|
|
srv = listeners.first
|
|
|
|
assert_kind_of TCPServer, srv
|
|
|
|
srv.addr
|
|
|
|
ensure
|
|
|
|
listeners.each(&:close) if listeners
|
|
|
|
end
|
2006-05-18 09:42:52 -04:00
|
|
|
end
|