summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authornormal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-04-12 01:41:51 +0000
committernormal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-04-12 01:41:51 +0000
commitc18df6d87cb1a26f596218beb8099c709e30aaf4 (patch)
tree080c4c27634bd3a33258b05e2291e5390e3d287d /test
parent525d2102fcc625435598729e8c972c147eefd24f (diff)
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
Diffstat (limited to 'test')
-rw-r--r--test/openssl/test_pair.rb24
-rw-r--r--test/socket/test_nonblock.rb23
2 files changed, 45 insertions, 2 deletions
diff --git a/test/openssl/test_pair.rb b/test/openssl/test_pair.rb
index 2c2776b644..cfcfa7a74d 100644
--- a/test/openssl/test_pair.rb
+++ b/test/openssl/test_pair.rb
@@ -283,7 +283,7 @@ module OpenSSL::TestPairM
serv.close if serv && !serv.closed?
end
- def test_accept_nonblock_no_exception
+ def test_connect_accept_nonblock_no_exception
ctx2 = OpenSSL::SSL::SSLContext.new
ctx2.ciphers = "ADH"
ctx2.tmp_dh_callback = proc { OpenSSL::TestUtils::TEST_KEY_DH1024 }
@@ -297,11 +297,31 @@ module OpenSSL::TestPairM
ctx1 = OpenSSL::SSL::SSLContext.new
ctx1.ciphers = "ADH"
s1 = OpenSSL::SSL::SSLSocket.new(sock1, ctx1)
- th = Thread.new { s1.connect }
+ th = Thread.new do
+ rets = []
+ begin
+ rv = s1.connect_nonblock(exception: false)
+ rets << rv
+ case rv
+ when :wait_writable
+ IO.select(nil, [s1], nil, 5)
+ when :wait_readable
+ IO.select([s1], nil, nil, 5)
+ end
+ end until rv == s1
+ rets
+ end
+
until th.join(0.01)
accepted = s2.accept_nonblock(exception: false)
assert_includes([s2, :wait_readable, :wait_writable ], accepted)
end
+
+ rets = th.value
+ assert_instance_of Array, rets
+ rets.each do |rv|
+ assert_includes([s1, :wait_readable, :wait_writable ], rv)
+ end
ensure
s1.close if s1
s2.close if s2
diff --git a/test/socket/test_nonblock.rb b/test/socket/test_nonblock.rb
index 94ed198616..6912046879 100644
--- a/test/socket/test_nonblock.rb
+++ b/test/socket/test_nonblock.rb
@@ -55,6 +55,29 @@ class TestSocketNonblock < Test::Unit::TestCase
s.close if s
end
+ 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))
+ 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
+
def test_udp_recvfrom_nonblock
u1 = UDPSocket.new
u2 = UDPSocket.new