1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/test/webrick/test_utils.rb
nobu 7c4d749d18 test_utils.rb: dynamically chosen port number
* test/webrick/test_utils.rb (test_create_listeners): use
  dynamically chosen port number, not hardcoded port number.
  [ruby-core:67508]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49211 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-01-11 06:11:40 +00:00

84 lines
2 KiB
Ruby

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
assert_raise(Timeout::Error){
m.timeout(2){
assert_raise(Timeout::Error){ m.timeout(1){ i += 1; sleep } }
assert_expired(false, m)
i += 1
sleep
}
}
assert_equal(2, i)
assert_expired(true, m)
assert_raise(Timeout::Error){ m.timeout(0.1){ sleep } }
assert_expired(true, m)
assert_raise(ex){ m.timeout(0.1, ex){ sleep } }
assert_expired(true, m)
i = 0
assert_raise(ex){
m.timeout(10){
m.timeout(1, ex){ i += 1; sleep }
}
sleep
}
assert_equal(1, i)
assert_expired(true, m)
i = 0
assert_raise(Timeout::Error){
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
def test_create_listeners
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")
end
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
end