2015-11-14 16:15:33 +03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2006-06-01 19:33:09 +04:00
|
|
|
begin
|
|
|
|
require "socket"
|
2015-05-17 08:56:07 +03:00
|
|
|
require "io/nonblock"
|
2015-06-15 22:38:49 +03:00
|
|
|
require "io/wait"
|
2006-06-01 19:33:09 +04:00
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
|
|
|
|
require "test/unit"
|
|
|
|
require "tempfile"
|
2006-07-01 10:23:21 +04:00
|
|
|
require "timeout"
|
2006-06-01 19:33:09 +04:00
|
|
|
|
2009-02-11 07:17:57 +03:00
|
|
|
class TestSocketNonblock < Test::Unit::TestCase
|
2006-06-01 19:33:09 +04:00
|
|
|
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)
|
2009-03-19 14:40:38 +03:00
|
|
|
assert_raise(IO::WaitReadable) { serv.accept_nonblock }
|
2015-03-12 06:03:04 +03:00
|
|
|
assert_equal :wait_readable, serv.accept_nonblock(exception: false)
|
|
|
|
assert_raise(IO::WaitReadable) { serv.accept_nonblock(exception: true) }
|
2006-06-01 19:33:09 +04:00
|
|
|
c = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
|
|
|
c.connect(serv.getsockname)
|
2009-02-17 14:05:35 +03:00
|
|
|
begin
|
|
|
|
s, sockaddr = serv.accept_nonblock
|
2009-03-19 14:40:38 +03:00
|
|
|
rescue IO::WaitReadable
|
|
|
|
IO.select [serv]
|
2009-02-17 14:05:35 +03:00
|
|
|
s, sockaddr = serv.accept_nonblock
|
|
|
|
end
|
2006-07-24 11:21:45 +04:00
|
|
|
assert_equal(Socket.unpack_sockaddr_in(c.getsockname), Socket.unpack_sockaddr_in(sockaddr))
|
2015-05-17 08:56:07 +03:00
|
|
|
if s.respond_to?(:nonblock?)
|
2015-05-18 12:41:37 +03:00
|
|
|
assert_predicate(s, :nonblock?, 'accepted socket is non-blocking')
|
2015-05-17 08:56:07 +03:00
|
|
|
end
|
2006-06-01 19:33:09 +04:00
|
|
|
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)
|
2009-03-19 15:45:40 +03:00
|
|
|
rescue IO::WaitWritable
|
2006-06-01 19:33:09 +04:00
|
|
|
IO.select nil, [c]
|
|
|
|
assert_nothing_raised {
|
|
|
|
begin
|
|
|
|
c.connect_nonblock(servaddr)
|
|
|
|
rescue Errno::EISCONN
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
s, sockaddr = serv.accept
|
2006-07-24 11:21:45 +04:00
|
|
|
assert_equal(Socket.unpack_sockaddr_in(c.getsockname), Socket.unpack_sockaddr_in(sockaddr))
|
2006-06-01 19:33:09 +04:00
|
|
|
ensure
|
|
|
|
serv.close if serv
|
|
|
|
c.close if c
|
|
|
|
s.close if s
|
|
|
|
end
|
|
|
|
|
connect_nonblock supports "exception: false"
This is for consistency with accept_nonblock arguments and gives a
minor speedup from avoiding exceptions.
[ruby-core:68838] [Feature #11024]
* ext/openssl/ossl_ssl.c (ossl_ssl_connect_nonblock):
support `exception: false'
* (get_no_exception): move function location
* ext/socket/socket.c (sock_connect_nonblock):
support `exception: false'
* test/openssl/test_pair.rb (test_connect_accept_nonblock_no_exception):
test `exception: false' on connect,
rename from `test_accept_nonblock_no_exception'
* test/socket/test_nonblock.rb (test_connect_nonblock_no_exception):
new test
Benchmark results:
default 0.050000 0.100000 0.150000 ( 0.151307)
exception: false 0.030000 0.080000 0.110000 ( 0.108840)
----------------------------8<-----------------------
require 'socket'
require 'benchmark'
require 'io/wait'
require 'tmpdir'
host = '127.0.0.1'
serv = TCPServer.new(host, 0) # UNIX sockets may not hit EINPROGRESS
nr = 5000 # few iterations to avoid running out of ports
addr = serv.getsockname
pid = fork do
begin
serv.accept.close
rescue => e
warn "#$$: #{e.message} (#{e.class})"
end while true
end
at_exit { Process.kill(:TERM, pid) }
serv.close
Benchmark.bmbm do |x|
x.report("default") do
nr.times do
s = Socket.new(:INET, :STREAM)
s.setsockopt(:SOL_SOCKET, :SO_REUSEADDR, 1)
begin
s.connect_nonblock(addr)
rescue IO::WaitWritable
s.wait_writable
end
s.close
end
end
x.report("exception: false") do
nr.times do
s = Socket.new(:INET, :STREAM)
s.setsockopt(:SOL_SOCKET, :SO_REUSEADDR, 1)
case s.connect_nonblock(addr, exception: false)
when :wait_writable
s.wait_writable
end
s.close
end
end
end
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50254 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-04-12 04:41:51 +03:00
|
|
|
def test_connect_nonblock_no_exception
|
|
|
|
serv = Socket.new(:INET, :STREAM)
|
|
|
|
serv.bind(Socket.sockaddr_in(0, "127.0.0.1"))
|
|
|
|
serv.listen(5)
|
|
|
|
c = Socket.new(:INET, :STREAM)
|
|
|
|
servaddr = serv.getsockname
|
|
|
|
rv = c.connect_nonblock(servaddr, exception: false)
|
|
|
|
case rv
|
|
|
|
when 0
|
|
|
|
# some OSes return immediately on non-blocking local connect()
|
|
|
|
else
|
|
|
|
assert_equal :wait_writable, rv
|
|
|
|
end
|
|
|
|
assert_equal([ [], [c], [] ], IO.select(nil, [c], nil, 60))
|
2015-04-20 05:11:10 +03:00
|
|
|
assert_equal(0, c.connect_nonblock(servaddr, exception: false),
|
|
|
|
'there should be no EISCONN error')
|
connect_nonblock supports "exception: false"
This is for consistency with accept_nonblock arguments and gives a
minor speedup from avoiding exceptions.
[ruby-core:68838] [Feature #11024]
* ext/openssl/ossl_ssl.c (ossl_ssl_connect_nonblock):
support `exception: false'
* (get_no_exception): move function location
* ext/socket/socket.c (sock_connect_nonblock):
support `exception: false'
* test/openssl/test_pair.rb (test_connect_accept_nonblock_no_exception):
test `exception: false' on connect,
rename from `test_accept_nonblock_no_exception'
* test/socket/test_nonblock.rb (test_connect_nonblock_no_exception):
new test
Benchmark results:
default 0.050000 0.100000 0.150000 ( 0.151307)
exception: false 0.030000 0.080000 0.110000 ( 0.108840)
----------------------------8<-----------------------
require 'socket'
require 'benchmark'
require 'io/wait'
require 'tmpdir'
host = '127.0.0.1'
serv = TCPServer.new(host, 0) # UNIX sockets may not hit EINPROGRESS
nr = 5000 # few iterations to avoid running out of ports
addr = serv.getsockname
pid = fork do
begin
serv.accept.close
rescue => e
warn "#$$: #{e.message} (#{e.class})"
end while true
end
at_exit { Process.kill(:TERM, pid) }
serv.close
Benchmark.bmbm do |x|
x.report("default") do
nr.times do
s = Socket.new(:INET, :STREAM)
s.setsockopt(:SOL_SOCKET, :SO_REUSEADDR, 1)
begin
s.connect_nonblock(addr)
rescue IO::WaitWritable
s.wait_writable
end
s.close
end
end
x.report("exception: false") do
nr.times do
s = Socket.new(:INET, :STREAM)
s.setsockopt(:SOL_SOCKET, :SO_REUSEADDR, 1)
case s.connect_nonblock(addr, exception: false)
when :wait_writable
s.wait_writable
end
s.close
end
end
end
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50254 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-04-12 04:41:51 +03:00
|
|
|
s, sockaddr = serv.accept
|
|
|
|
assert_equal(Socket.unpack_sockaddr_in(c.getsockname),
|
|
|
|
Socket.unpack_sockaddr_in(sockaddr))
|
|
|
|
ensure
|
|
|
|
serv.close if serv
|
|
|
|
c.close if c
|
|
|
|
s.close if s
|
|
|
|
end
|
|
|
|
|
2006-06-01 19:33:09 +04:00
|
|
|
def test_udp_recvfrom_nonblock
|
|
|
|
u1 = UDPSocket.new
|
|
|
|
u2 = UDPSocket.new
|
|
|
|
u1.bind("127.0.0.1", 0)
|
2009-03-19 14:40:38 +03:00
|
|
|
assert_raise(IO::WaitReadable) { u1.recvfrom_nonblock(100) }
|
|
|
|
assert_raise(IO::WaitReadable, Errno::EINVAL) { u2.recvfrom_nonblock(100) }
|
2006-06-01 19: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)
|
2014-02-18 16:05:09 +04:00
|
|
|
_, port, _, _ = inet_addr
|
|
|
|
u2_port, _ = Socket.unpack_sockaddr_in(u2.getsockname)
|
2006-06-01 19:33:09 +04:00
|
|
|
assert_equal(u2_port, port)
|
2009-03-19 14:40:38 +03:00
|
|
|
assert_raise(IO::WaitReadable) { u1.recvfrom_nonblock(100) }
|
2006-06-01 19:33:09 +04:00
|
|
|
u2.send("", 0, u1.getsockname)
|
2006-07-01 11:27:55 +04:00
|
|
|
assert_nothing_raised("cygwin 1.5.19 has a problem to send an empty UDP packet. [ruby-dev:28915]") {
|
2015-07-13 13:07:01 +03:00
|
|
|
Timeout.timeout(1) { IO.select [u1] }
|
2006-07-01 11:27:55 +04:00
|
|
|
}
|
2006-06-01 19:33:09 +04:00
|
|
|
mesg, inet_addr = u1.recvfrom_nonblock(100)
|
|
|
|
assert_equal("", mesg)
|
|
|
|
ensure
|
|
|
|
u1.close if u1
|
|
|
|
u2.close if u2
|
|
|
|
end
|
|
|
|
|
2006-06-26 20:39:59 +04:00
|
|
|
def test_udp_recv_nonblock
|
|
|
|
u1 = UDPSocket.new
|
|
|
|
u2 = UDPSocket.new
|
|
|
|
u1.bind("127.0.0.1", 0)
|
2009-03-19 14:40:38 +03:00
|
|
|
assert_raise(IO::WaitReadable) { u1.recv_nonblock(100) }
|
|
|
|
assert_raise(IO::WaitReadable, Errno::EINVAL) { u2.recv_nonblock(100) }
|
2006-06-26 20:39:59 +04:00
|
|
|
u2.send("aaa", 0, u1.getsockname)
|
|
|
|
IO.select [u1]
|
|
|
|
mesg = u1.recv_nonblock(100)
|
2006-06-01 19:33:09 +04:00
|
|
|
assert_equal("aaa", mesg)
|
2009-03-19 14:40:38 +03:00
|
|
|
assert_raise(IO::WaitReadable) { u1.recv_nonblock(100) }
|
2006-06-26 20:39:59 +04:00
|
|
|
u2.send("", 0, u1.getsockname)
|
2006-07-01 11:27:55 +04:00
|
|
|
assert_nothing_raised("cygwin 1.5.19 has a problem to send an empty UDP packet. [ruby-dev:28915]") {
|
2015-07-13 13:07:01 +03:00
|
|
|
Timeout.timeout(1) { IO.select [u1] }
|
2006-07-01 11:27:55 +04:00
|
|
|
}
|
2006-06-26 20:39:59 +04:00
|
|
|
mesg = u1.recv_nonblock(100)
|
2006-06-01 19:33:09 +04:00
|
|
|
assert_equal("", mesg)
|
2015-06-15 23:02:43 +03:00
|
|
|
|
2015-11-14 16:15:33 +03:00
|
|
|
buf = "short".dup
|
2015-06-15 23:02:43 +03:00
|
|
|
out = "hello world" * 4
|
|
|
|
out.freeze
|
|
|
|
u2.send(out, 0, u1.getsockname)
|
|
|
|
IO.select [u1]
|
|
|
|
rv = u1.recv_nonblock(100, 0, buf)
|
|
|
|
assert_equal rv.object_id, buf.object_id
|
|
|
|
assert_equal out, rv
|
|
|
|
assert_equal out, buf
|
2006-06-01 19:33:09 +04:00
|
|
|
ensure
|
2006-06-26 20:39:59 +04:00
|
|
|
u1.close if u1
|
|
|
|
u2.close if u2
|
|
|
|
end
|
2006-06-01 19: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)
|
2009-03-19 14:40:38 +03:00
|
|
|
assert_raise(IO::WaitReadable) { s1.recvfrom_nonblock(100) }
|
|
|
|
assert_raise(IO::WaitReadable, Errno::EINVAL) { s2.recvfrom_nonblock(100) }
|
2006-06-01 19:33:09 +04:00
|
|
|
s2.send("aaa", 0, s1.getsockname)
|
|
|
|
IO.select [s1]
|
2009-03-06 06:56:38 +03:00
|
|
|
mesg, sockaddr = s1.recvfrom_nonblock(100)
|
2006-06-01 19:33:09 +04:00
|
|
|
assert_equal("aaa", mesg)
|
2014-02-18 16:05:09 +04:00
|
|
|
port, _ = Socket.unpack_sockaddr_in(sockaddr)
|
|
|
|
s2_port, _ = Socket.unpack_sockaddr_in(s2.getsockname)
|
2006-06-01 19:33:09 +04:00
|
|
|
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)
|
2014-02-18 16:05:09 +04:00
|
|
|
_, port, _, addr = serv.addr
|
2006-06-01 19:33:09 +04:00
|
|
|
c = TCPSocket.new(addr, port)
|
|
|
|
s = serv.accept
|
2009-02-21 18:57:52 +03:00
|
|
|
if block_given?
|
|
|
|
begin
|
|
|
|
yield c, s
|
|
|
|
ensure
|
|
|
|
c.close if !c.closed?
|
|
|
|
s.close if !s.closed?
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return c, s
|
|
|
|
end
|
2006-06-01 19:33:09 +04:00
|
|
|
ensure
|
2009-02-21 18:57:52 +03:00
|
|
|
serv.close if serv && !serv.closed?
|
2006-06-01 19:33:09 +04:00
|
|
|
end
|
|
|
|
|
2009-07-19 12:53:11 +04:00
|
|
|
def udp_pair
|
|
|
|
s1 = UDPSocket.new
|
|
|
|
s1.bind('127.0.0.1', 0)
|
|
|
|
af, port1, host, addr1 = s1.addr
|
|
|
|
|
|
|
|
s2 = UDPSocket.new
|
|
|
|
s2.bind('127.0.0.1', 0)
|
|
|
|
af, port2, host, addr2 = s2.addr
|
|
|
|
|
|
|
|
s1.connect(addr2, port2)
|
|
|
|
s2.connect(addr1, port1)
|
|
|
|
|
|
|
|
if block_given?
|
|
|
|
begin
|
|
|
|
yield s1, s2
|
|
|
|
ensure
|
|
|
|
s1.close if !s1.closed?
|
|
|
|
s2.close if !s2.closed?
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return s1, s2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-06-26 20:39:59 +04:00
|
|
|
def test_tcp_recv_nonblock
|
|
|
|
c, s = tcp_pair
|
2009-03-19 14:40:38 +03:00
|
|
|
assert_raise(IO::WaitReadable) { c.recv_nonblock(100) }
|
|
|
|
assert_raise(IO::WaitReadable) { s.recv_nonblock(100) }
|
2006-06-26 20:39:59 +04:00
|
|
|
c.write("abc")
|
|
|
|
IO.select [s]
|
|
|
|
assert_equal("a", s.recv_nonblock(1))
|
|
|
|
assert_equal("bc", s.recv_nonblock(100))
|
2009-03-19 14:40:38 +03:00
|
|
|
assert_raise(IO::WaitReadable) { s.recv_nonblock(100) }
|
2006-06-26 20:39:59 +04:00
|
|
|
ensure
|
|
|
|
c.close if c
|
|
|
|
s.close if s
|
|
|
|
end
|
|
|
|
|
2006-06-01 19:33:09 +04:00
|
|
|
def test_read_nonblock
|
|
|
|
c, s = tcp_pair
|
2009-03-19 14:40:38 +03:00
|
|
|
assert_raise(IO::WaitReadable) { c.read_nonblock(100) }
|
|
|
|
assert_raise(IO::WaitReadable) { s.read_nonblock(100) }
|
2006-06-01 19:33:09 +04:00
|
|
|
c.write("abc")
|
|
|
|
IO.select [s]
|
|
|
|
assert_equal("a", s.read_nonblock(1))
|
|
|
|
assert_equal("bc", s.read_nonblock(100))
|
2009-03-19 14:40:38 +03:00
|
|
|
assert_raise(IO::WaitReadable) { s.read_nonblock(100) }
|
2006-06-01 19:33:09 +04:00
|
|
|
ensure
|
|
|
|
c.close if c
|
|
|
|
s.close if s
|
|
|
|
end
|
|
|
|
|
2013-08-27 02:41:44 +04:00
|
|
|
def test_read_nonblock_no_exception
|
|
|
|
c, s = tcp_pair
|
|
|
|
assert_equal :wait_readable, c.read_nonblock(100, exception: false)
|
|
|
|
assert_equal :wait_readable, s.read_nonblock(100, exception: false)
|
|
|
|
c.write("abc")
|
|
|
|
IO.select [s]
|
|
|
|
assert_equal("a", s.read_nonblock(1, exception: false))
|
|
|
|
assert_equal("bc", s.read_nonblock(100, exception: false))
|
|
|
|
assert_equal :wait_readable, s.read_nonblock(100, exception: false)
|
|
|
|
ensure
|
|
|
|
c.close if c
|
|
|
|
s.close if s
|
|
|
|
end
|
|
|
|
|
2006-07-01 10:23:21 +04:00
|
|
|
=begin
|
2006-06-01 19:33:09 +04:00
|
|
|
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 {
|
2009-03-19 14:40:38 +03:00
|
|
|
assert_raise(IO::WaitWritable) {
|
2006-06-01 19: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
|
2006-07-01 10:23:21 +04:00
|
|
|
=end
|
2006-06-01 19:33:09 +04:00
|
|
|
|
2009-02-21 18:57:52 +03:00
|
|
|
def test_sendmsg_nonblock_error
|
2009-07-19 12:53:11 +04:00
|
|
|
udp_pair {|s1, s2|
|
2009-02-21 18:57:52 +03:00
|
|
|
begin
|
|
|
|
loop {
|
2009-07-19 12:53:11 +04:00
|
|
|
s1.sendmsg_nonblock("a" * 100000)
|
2009-02-21 18:57:52 +03:00
|
|
|
}
|
2010-06-14 17:46:06 +04:00
|
|
|
rescue NotImplementedError, Errno::ENOSYS
|
2013-05-09 15:48:29 +04:00
|
|
|
skip "sendmsg not implemented on this platform: #{$!}"
|
2009-07-19 12:53:11 +04:00
|
|
|
rescue Errno::EMSGSIZE
|
|
|
|
# UDP has 64K limit (if no Jumbograms). No problem.
|
2009-02-21 18:57:52 +03:00
|
|
|
rescue Errno::EWOULDBLOCK
|
2009-03-19 14:40:38 +03:00
|
|
|
assert_kind_of(IO::WaitWritable, $!)
|
2009-02-21 18:57:52 +03:00
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-06-15 22:38:49 +03:00
|
|
|
def test_recvfrom_nonblock_no_exception
|
|
|
|
udp_pair do |s1, s2|
|
|
|
|
assert_equal :wait_readable, s1.recvfrom_nonblock(100, exception: false)
|
2015-06-16 12:26:26 +03:00
|
|
|
s2.send("aaa", 0)
|
2015-06-16 12:29:17 +03:00
|
|
|
assert_predicate s1, :wait_readable
|
2015-06-15 22:38:49 +03:00
|
|
|
mesg, inet_addr = s1.recvfrom_nonblock(100, exception: false)
|
|
|
|
assert_equal(4, inet_addr.length)
|
|
|
|
assert_equal("aaa", mesg)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-02 05:02:00 +03:00
|
|
|
if defined?(UNIXSocket) && defined?(Socket::SOCK_SEQPACKET)
|
|
|
|
def test_sendmsg_nonblock_seqpacket
|
2015-07-21 11:17:48 +03:00
|
|
|
buf = '*' * 4096
|
2015-06-02 04:47:41 +03:00
|
|
|
UNIXSocket.pair(:SEQPACKET) do |s1, s2|
|
2015-06-06 02:55:23 +03:00
|
|
|
assert_raise(IO::WaitWritable) do
|
2015-06-02 04:47:41 +03:00
|
|
|
loop { s1.sendmsg_nonblock(buf) }
|
|
|
|
end
|
|
|
|
end
|
2015-06-02 05:02:00 +03:00
|
|
|
rescue NotImplementedError, Errno::ENOSYS, Errno::EPROTONOSUPPORT
|
|
|
|
skip "UNIXSocket.pair(:SEQPACKET) not implemented on this platform: #{$!}"
|
2015-06-02 04:47:41 +03:00
|
|
|
end
|
2015-06-15 22:38:49 +03:00
|
|
|
|
|
|
|
def test_sendmsg_nonblock_no_exception
|
2015-07-21 11:17:48 +03:00
|
|
|
buf = '*' * 4096
|
2015-06-15 22:38:49 +03:00
|
|
|
UNIXSocket.pair(:SEQPACKET) do |s1, s2|
|
|
|
|
n = 0
|
|
|
|
Timeout.timeout(60) do
|
|
|
|
case rv = s1.sendmsg_nonblock(buf, exception: false)
|
|
|
|
when Integer
|
|
|
|
n += rv
|
|
|
|
when :wait_writable
|
|
|
|
break
|
|
|
|
else
|
|
|
|
flunk "unexpected return value: #{rv.inspect}"
|
|
|
|
end while true
|
|
|
|
assert_equal :wait_writable, rv
|
|
|
|
assert_operator n, :>, 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue NotImplementedError, Errno::ENOSYS, Errno::EPROTONOSUPPORT
|
|
|
|
skip "UNIXSocket.pair(:SEQPACKET) not implemented on this platform: #{$!}"
|
|
|
|
end
|
2015-06-02 04:47:41 +03:00
|
|
|
end
|
|
|
|
|
2009-02-21 18:57:52 +03:00
|
|
|
def test_recvmsg_nonblock_error
|
2009-07-19 12:53:11 +04:00
|
|
|
udp_pair {|s1, s2|
|
2009-02-21 18:57:52 +03:00
|
|
|
begin
|
2009-07-19 12:53:11 +04:00
|
|
|
s1.recvmsg_nonblock(4096)
|
2009-07-17 17:40:59 +04:00
|
|
|
rescue NotImplementedError
|
2009-07-19 12:53:11 +04:00
|
|
|
skip "recvmsg not implemented on this platform."
|
2009-02-21 18:57:52 +03:00
|
|
|
rescue Errno::EWOULDBLOCK
|
2009-03-19 14:40:38 +03:00
|
|
|
assert_kind_of(IO::WaitReadable, $!)
|
2009-02-21 18:57:52 +03:00
|
|
|
end
|
2015-06-15 22:38:49 +03:00
|
|
|
assert_equal :wait_readable, s1.recvmsg_nonblock(11, exception: false)
|
2009-02-21 18:57:52 +03:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_recv_nonblock_error
|
|
|
|
tcp_pair {|c, s|
|
|
|
|
begin
|
|
|
|
c.recv_nonblock(4096)
|
|
|
|
rescue Errno::EWOULDBLOCK
|
2009-03-19 14:40:38 +03:00
|
|
|
assert_kind_of(IO::WaitReadable, $!)
|
2009-02-21 18:57:52 +03:00
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-06-15 22:38:49 +03:00
|
|
|
def test_recv_nonblock_no_exception
|
|
|
|
tcp_pair {|c, s|
|
|
|
|
assert_equal :wait_readable, c.recv_nonblock(11, exception: false)
|
|
|
|
s.write('HI')
|
2015-06-16 12:29:17 +03:00
|
|
|
assert_predicate c, :wait_readable
|
2015-06-15 22:38:49 +03:00
|
|
|
assert_equal 'HI', c.recv_nonblock(11, exception: false)
|
|
|
|
assert_equal :wait_readable, c.recv_nonblock(11, exception: false)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2009-02-21 18:57:52 +03:00
|
|
|
def test_connect_nonblock_error
|
|
|
|
serv = TCPServer.new("127.0.0.1", 0)
|
2014-02-18 16:05:09 +04:00
|
|
|
_, port, _, _ = serv.addr
|
2009-02-21 18:57:52 +03:00
|
|
|
c = Socket.new(:INET, :STREAM)
|
|
|
|
begin
|
|
|
|
c.connect_nonblock(Socket.sockaddr_in(port, "127.0.0.1"))
|
|
|
|
rescue Errno::EINPROGRESS
|
2009-03-19 14:40:38 +03:00
|
|
|
assert_kind_of(IO::WaitWritable, $!)
|
2009-02-21 18:57:52 +03:00
|
|
|
end
|
|
|
|
ensure
|
|
|
|
serv.close if serv && !serv.closed?
|
|
|
|
c.close if c && !c.closed?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_accept_nonblock_error
|
|
|
|
serv = Socket.new(:INET, :STREAM)
|
|
|
|
serv.bind(Socket.sockaddr_in(0, "127.0.0.1"))
|
|
|
|
serv.listen(5)
|
|
|
|
begin
|
|
|
|
s, _ = serv.accept_nonblock
|
2009-03-19 14:40:38 +03:00
|
|
|
rescue Errno::EWOULDBLOCK
|
|
|
|
assert_kind_of(IO::WaitReadable, $!)
|
2009-02-21 18:57:52 +03:00
|
|
|
end
|
|
|
|
ensure
|
|
|
|
serv.close if serv && !serv.closed?
|
|
|
|
s.close if s && !s.closed?
|
|
|
|
end
|
|
|
|
|
2006-06-01 19:33:09 +04:00
|
|
|
end if defined?(Socket)
|