mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
add tests.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22005 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
1643d1c28a
commit
4aec56ba71
1 changed files with 74 additions and 0 deletions
|
@ -1,5 +1,6 @@
|
|||
begin
|
||||
require "socket"
|
||||
require "tmpdir"
|
||||
require "test/unit"
|
||||
rescue LoadError
|
||||
end
|
||||
|
@ -71,4 +72,77 @@ class TestSocket < Test::Unit::TestCase
|
|||
assert(ai.ip?)
|
||||
}
|
||||
end
|
||||
|
||||
def test_tcp
|
||||
TCPServer.open(0) {|serv|
|
||||
port, addr = Socket.unpack_sockaddr_in(serv.getsockname)
|
||||
Socket.tcp(addr, port) {|s1|
|
||||
s2 = serv.accept
|
||||
begin
|
||||
assert_equal(s2.remote_address.ip_unpack, s1.local_address.ip_unpack)
|
||||
ensure
|
||||
s2.close
|
||||
end
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
def random_port
|
||||
# IANA suggests dynamic port for 49152 to 65535
|
||||
# http://www.iana.org/assignments/port-numbers
|
||||
49152 + rand(65535-49152+1)
|
||||
end
|
||||
|
||||
def test_tcp_server_sockets
|
||||
port = random_port
|
||||
begin
|
||||
sockets = Socket.tcp_server_sockets(port)
|
||||
rescue Errno::EADDRINUSE
|
||||
return # not test failure
|
||||
end
|
||||
begin
|
||||
sockets.each {|s|
|
||||
assert_equal(port, s.local_address.ip_port)
|
||||
}
|
||||
ensure
|
||||
sockets.each {|s|
|
||||
s.close
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
if defined? UNIXSocket
|
||||
def test_unix
|
||||
Dir.mktmpdir {|tmpdir|
|
||||
path = "#{tmpdir}/sock"
|
||||
UNIXServer.open(path) {|serv|
|
||||
Socket.unix(path) {|s1|
|
||||
s2 = serv.accept
|
||||
begin
|
||||
assert_equal(s2.remote_address.unix_path, s1.local_address.unix_path)
|
||||
ensure
|
||||
s2.close
|
||||
end
|
||||
}
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
def test_unix_server_socket
|
||||
Dir.mktmpdir {|tmpdir|
|
||||
path = "#{tmpdir}/sock"
|
||||
2.times {
|
||||
serv = Socket.unix_server_socket(path)
|
||||
begin
|
||||
assert_kind_of(Socket, serv)
|
||||
assert(File.socket?(path))
|
||||
assert_equal(path, serv.local_address.unix_path)
|
||||
ensure
|
||||
serv.close
|
||||
end
|
||||
}
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
end if defined?(Socket)
|
||||
|
|
Loading…
Add table
Reference in a new issue