2006-06-01 11:33:09 -04:00
|
|
|
begin
|
|
|
|
require "socket"
|
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
|
|
|
|
require "test/unit"
|
|
|
|
require "tempfile"
|
|
|
|
|
|
|
|
class TestNonblockSocket < Test::Unit::TestCase
|
|
|
|
def test_accept_nonblock
|
|
|
|
serv = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
|
|
|
serv.bind(Socket.sockaddr_in(0, "127.0.0.1"))
|
|
|
|
serv.listen(5)
|
2006-06-07 02:44:24 -04:00
|
|
|
assert_raise(Errno::EAGAIN, Errno::EWOULDBLOCK) { serv.accept_nonblock }
|
2006-06-01 11:33:09 -04:00
|
|
|
c = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
|
|
|
c.connect(serv.getsockname)
|
|
|
|
s, sockaddr = serv.accept_nonblock
|
|
|
|
assert_equal(c.getsockname, sockaddr)
|
|
|
|
ensure
|
|
|
|
serv.close if serv
|
|
|
|
c.close if c
|
|
|
|
s.close if s
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_connect_nonblock
|
|
|
|
serv = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
|
|
|
serv.bind(Socket.sockaddr_in(0, "127.0.0.1"))
|
|
|
|
serv.listen(5)
|
|
|
|
c = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
|
|
|
servaddr = serv.getsockname
|
|
|
|
begin
|
|
|
|
c.connect_nonblock(servaddr)
|
|
|
|
rescue Errno::EINPROGRESS
|
|
|
|
IO.select nil, [c]
|
|
|
|
assert_nothing_raised {
|
|
|
|
begin
|
|
|
|
c.connect_nonblock(servaddr)
|
|
|
|
rescue Errno::EISCONN
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
s, sockaddr = serv.accept
|
|
|
|
assert_equal(c.getsockname, sockaddr)
|
|
|
|
ensure
|
|
|
|
serv.close if serv
|
|
|
|
c.close if c
|
|
|
|
s.close if s
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_udp_recvfrom_nonblock
|
|
|
|
u1 = UDPSocket.new
|
|
|
|
u2 = UDPSocket.new
|
|
|
|
u1.bind("127.0.0.1", 0)
|
2006-06-07 02:44:24 -04:00
|
|
|
assert_raise(Errno::EAGAIN, Errno::EWOULDBLOCK) { u1.recvfrom_nonblock(100) }
|
|
|
|
assert_raise(Errno::EAGAIN, Errno::EWOULDBLOCK, Errno::EINVAL) { u2.recvfrom_nonblock(100) }
|
2006-06-01 11:33:09 -04:00
|
|
|
u2.send("aaa", 0, u1.getsockname)
|
|
|
|
IO.select [u1]
|
|
|
|
mesg, inet_addr = u1.recvfrom_nonblock(100)
|
|
|
|
assert_equal(4, inet_addr.length)
|
|
|
|
assert_equal("aaa", mesg)
|
|
|
|
af, port, host, addr = inet_addr
|
|
|
|
u2_port, u2_addr = Socket.unpack_sockaddr_in(u2.getsockname)
|
|
|
|
assert_equal(u2_port, port)
|
2006-06-07 02:44:24 -04:00
|
|
|
assert_raise(Errno::EAGAIN, Errno::EWOULDBLOCK) { u1.recvfrom_nonblock(100) }
|
2006-06-01 11:33:09 -04:00
|
|
|
u2.send("", 0, u1.getsockname)
|
|
|
|
IO.select [u1]
|
|
|
|
mesg, inet_addr = u1.recvfrom_nonblock(100)
|
|
|
|
assert_equal("", mesg)
|
|
|
|
ensure
|
|
|
|
u1.close if u1
|
|
|
|
u2.close if u2
|
|
|
|
end
|
|
|
|
|
2006-06-26 12:39:59 -04:00
|
|
|
def test_udp_recv_nonblock
|
|
|
|
u1 = UDPSocket.new
|
|
|
|
u2 = UDPSocket.new
|
|
|
|
u1.bind("127.0.0.1", 0)
|
|
|
|
assert_raise(Errno::EAGAIN, Errno::EWOULDBLOCK) { u1.recv_nonblock(100) }
|
|
|
|
assert_raise(Errno::EAGAIN, Errno::EWOULDBLOCK, Errno::EINVAL) { u2.recv_nonblock(100) }
|
|
|
|
u2.send("aaa", 0, u1.getsockname)
|
|
|
|
IO.select [u1]
|
|
|
|
mesg = u1.recv_nonblock(100)
|
2006-06-01 11:33:09 -04:00
|
|
|
assert_equal("aaa", mesg)
|
2006-06-26 12:39:59 -04:00
|
|
|
assert_raise(Errno::EAGAIN, Errno::EWOULDBLOCK) { u1.recv_nonblock(100) }
|
|
|
|
u2.send("", 0, u1.getsockname)
|
|
|
|
IO.select [u1]
|
|
|
|
mesg = u1.recv_nonblock(100)
|
2006-06-01 11:33:09 -04:00
|
|
|
assert_equal("", mesg)
|
|
|
|
ensure
|
2006-06-26 12:39:59 -04:00
|
|
|
u1.close if u1
|
|
|
|
u2.close if u2
|
|
|
|
end
|
2006-06-01 11:33:09 -04:00
|
|
|
|
|
|
|
def test_socket_recvfrom_nonblock
|
|
|
|
s1 = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0)
|
|
|
|
s1.bind(Socket.sockaddr_in(0, "127.0.0.1"))
|
|
|
|
s2 = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0)
|
2006-06-07 02:44:24 -04:00
|
|
|
assert_raise(Errno::EAGAIN, Errno::EWOULDBLOCK) { s1.recvfrom_nonblock(100) }
|
|
|
|
assert_raise(Errno::EAGAIN, Errno::EWOULDBLOCK, Errno::EINVAL) { s2.recvfrom_nonblock(100) }
|
2006-06-01 11:33:09 -04:00
|
|
|
s2.send("aaa", 0, s1.getsockname)
|
|
|
|
IO.select [s1]
|
|
|
|
mesg, sockaddr = s1.recvfrom_nonblock(100)
|
|
|
|
assert_equal("aaa", mesg)
|
|
|
|
port, addr = Socket.unpack_sockaddr_in(sockaddr)
|
|
|
|
s2_port, s2_addr = Socket.unpack_sockaddr_in(s2.getsockname)
|
|
|
|
assert_equal(s2_port, port)
|
|
|
|
ensure
|
|
|
|
s1.close if s1
|
|
|
|
s2.close if s2
|
|
|
|
end
|
|
|
|
|
|
|
|
def tcp_pair
|
|
|
|
serv = TCPServer.new("127.0.0.1", 0)
|
|
|
|
af, port, host, addr = serv.addr
|
|
|
|
c = TCPSocket.new(addr, port)
|
|
|
|
s = serv.accept
|
|
|
|
return c, s
|
|
|
|
ensure
|
|
|
|
serv.close if serv
|
|
|
|
end
|
|
|
|
|
2006-06-26 12:39:59 -04:00
|
|
|
def test_tcp_recv_nonblock
|
|
|
|
c, s = tcp_pair
|
|
|
|
assert_raise(Errno::EAGAIN, Errno::EWOULDBLOCK) { c.recv_nonblock(100) }
|
|
|
|
assert_raise(Errno::EAGAIN, Errno::EWOULDBLOCK) { s.recv_nonblock(100) }
|
|
|
|
c.write("abc")
|
|
|
|
IO.select [s]
|
|
|
|
assert_equal("a", s.recv_nonblock(1))
|
|
|
|
assert_equal("bc", s.recv_nonblock(100))
|
|
|
|
assert_raise(Errno::EAGAIN, Errno::EWOULDBLOCK) { s.recv_nonblock(100) }
|
|
|
|
ensure
|
|
|
|
c.close if c
|
|
|
|
s.close if s
|
|
|
|
end
|
|
|
|
|
2006-06-01 11:33:09 -04:00
|
|
|
def test_read_nonblock
|
|
|
|
c, s = tcp_pair
|
2006-06-07 02:44:24 -04:00
|
|
|
assert_raise(Errno::EAGAIN, Errno::EWOULDBLOCK) { c.read_nonblock(100) }
|
|
|
|
assert_raise(Errno::EAGAIN, Errno::EWOULDBLOCK) { s.read_nonblock(100) }
|
2006-06-01 11:33:09 -04:00
|
|
|
c.write("abc")
|
|
|
|
IO.select [s]
|
|
|
|
assert_equal("a", s.read_nonblock(1))
|
|
|
|
assert_equal("bc", s.read_nonblock(100))
|
2006-06-07 02:44:24 -04:00
|
|
|
assert_raise(Errno::EAGAIN, Errno::EWOULDBLOCK) { s.read_nonblock(100) }
|
2006-06-01 11:33:09 -04:00
|
|
|
ensure
|
|
|
|
c.close if c
|
|
|
|
s.close if s
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_write_nonblock
|
|
|
|
c, s = tcp_pair
|
|
|
|
str = "a" * 10000
|
|
|
|
_, ws, _ = IO.select(nil, [c], nil)
|
|
|
|
assert_equal([c], ws)
|
|
|
|
ret = c.write_nonblock(str)
|
|
|
|
assert_operator(ret, :>, 0)
|
|
|
|
loop {
|
2006-06-07 02:44:24 -04:00
|
|
|
assert_raise(Errno::EAGAIN, Errno::EWOULDBLOCK) {
|
2006-06-01 11:33:09 -04:00
|
|
|
loop {
|
|
|
|
ret = c.write_nonblock(str)
|
|
|
|
assert_operator(ret, :>, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, ws, _ = IO.select(nil, [c], nil, 0)
|
|
|
|
break if !ws
|
|
|
|
}
|
|
|
|
ensure
|
|
|
|
c.close if c
|
|
|
|
s.close if s
|
|
|
|
end
|
|
|
|
|
|
|
|
end if defined?(Socket)
|