1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* ext/socket/socket.c (bsock_recv_nonblock): new method

BasicSocket#recv_nonblock.
  (udp_recvfrom_nonblock): renamed from ip_recvfrom_nonblock.
  IPSocket#recvfrom_nonblock is moved to UDPSocket#recvfrom_nonblock.
  (unix_recvfrom_nonblock): removed.
  UNIXSocket#recvfrom_nonblock is removed.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10403 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2006-06-26 16:39:59 +00:00
parent 018c600103
commit b73e6801e3
4 changed files with 141 additions and 135 deletions

View file

@ -71,38 +71,25 @@ class TestNonblockSocket < Test::Unit::TestCase
u2.close if u2
end
def bound_unix_socket(klass)
tmpfile = Tempfile.new("testrubysock")
path = tmpfile.path
tmpfile.close(true)
return klass.new(path), path
end
def test_unix_recvfrom_nonblock
serv, serv_path = bound_unix_socket(UNIXServer)
c = UNIXSocket.new(serv_path)
s = serv.accept
assert_raise(Errno::EAGAIN, Errno::EWOULDBLOCK) { s.recvfrom_nonblock(100) }
assert_raise(Errno::EAGAIN, Errno::EWOULDBLOCK) { c.recvfrom_nonblock(100) }
s.write "aaa"
IO.select [c]
mesg, unix_addr = c.recvfrom_nonblock(100)
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)
assert_equal("aaa", mesg)
assert_equal(2, unix_addr.length)
af, path = unix_addr
if path != "" # connection-oriented socket may not return the peer address.
assert_equal(serv_path, path)
end
s.close
IO.select [c]
mesg, unix_addr = c.recvfrom_nonblock(100)
assert_raise(Errno::EAGAIN, Errno::EWOULDBLOCK) { u1.recv_nonblock(100) }
u2.send("", 0, u1.getsockname)
IO.select [u1]
mesg = u1.recv_nonblock(100)
assert_equal("", mesg)
ensure
File.unlink serv_path if serv_path && File.socket?(serv_path)
serv.close if serv
c.close if c
s.close if s && !s.closed?
end if defined?(UNIXSocket)
u1.close if u1
u2.close if u2
end
def test_socket_recvfrom_nonblock
s1 = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0)
@ -132,6 +119,20 @@ class TestNonblockSocket < Test::Unit::TestCase
serv.close if serv
end
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
def test_read_nonblock
c, s = tcp_pair
assert_raise(Errno::EAGAIN, Errno::EWOULDBLOCK) { c.read_nonblock(100) }

View file

@ -101,11 +101,11 @@ class TestUNIXSocket < Test::Unit::TestCase
s2.close
end
def test_noname_recvfrom_nonblock
def test_noname_recv_nonblock
s1, s2 = UNIXSocket.pair
s2.write("a")
IO.select [s1]
assert_equal(["a", ["AF_UNIX", ""]], s1.recvfrom_nonblock(10))
assert_equal("a", s1.recv_nonblock(10))
ensure
s1.close
s2.close
@ -123,7 +123,7 @@ class TestUNIXSocket < Test::Unit::TestCase
def test_dgram_pair
s1, s2 = UNIXSocket.pair(Socket::SOCK_DGRAM)
assert_raise(Errno::EAGAIN) { s1.recvfrom_nonblock(10) }
assert_raise(Errno::EAGAIN) { s1.recv_nonblock(10) }
s2.send("", 0)
s2.send("haha", 0)
s2.send("", 0)
@ -132,7 +132,7 @@ class TestUNIXSocket < Test::Unit::TestCase
assert_equal("haha", s1.recv(10))
assert_equal("", s1.recv(10))
assert_equal("", s1.recv(10))
assert_raise(Errno::EAGAIN) { s1.recvfrom_nonblock(10) }
assert_raise(Errno::EAGAIN) { s1.recv_nonblock(10) }
ensure
s1.close
s2.close
@ -140,7 +140,7 @@ class TestUNIXSocket < Test::Unit::TestCase
def test_seqpacket_pair
s1, s2 = UNIXSocket.pair(Socket::SOCK_SEQPACKET)
assert_raise(Errno::EAGAIN) { s1.recvfrom_nonblock(10) }
assert_raise(Errno::EAGAIN) { s1.recv_nonblock(10) }
s2.send("", 0)
s2.send("haha", 0)
s2.send("", 0)
@ -149,7 +149,7 @@ class TestUNIXSocket < Test::Unit::TestCase
assert_equal("haha", s1.recv(10))
assert_equal("", s1.recv(10))
assert_equal("", s1.recv(10))
assert_raise(Errno::EAGAIN) { s1.recvfrom_nonblock(10) }
assert_raise(Errno::EAGAIN) { s1.recv_nonblock(10) }
rescue Errno::EPROTONOSUPPORT
ensure
s1.close if s1