From 988ca60565a5ba6661f7215026f008afebcf7aee Mon Sep 17 00:00:00 2001 From: tenderlove Date: Mon, 26 Aug 2013 22:41:44 +0000 Subject: * io.c (io_read_nonblock): support non-blocking reads without raising exceptions. As in: `io.read_nonblock(size, exception: false)` [ruby-core:38666] [Feature #5138] * ext/openssl/ossl_ssl.c (ossl_ssl_read_internal): ditto * ext/stringio/stringio.c (strio_sysread): ditto * io.c (rb_io_write_nonblock): support non-blocking writes without raising an exception. * ext/openssl/ossl_ssl.c (ossl_ssl_write_internal): ditto * test/openssl/test_pair.rb (class OpenSSL): tests * test/ruby/test_io.rb (class TestIO): ditto * test/socket/test_nonblock.rb (class TestSocketNonblock): ditto * test/stringio/test_stringio.rb (class TestStringIO): ditto git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42695 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/stringio/test_stringio.rb | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'test/stringio') diff --git a/test/stringio/test_stringio.rb b/test/stringio/test_stringio.rb index 0eceeba894..f29322b393 100644 --- a/test/stringio/test_stringio.rb +++ b/test/stringio/test_stringio.rb @@ -89,6 +89,14 @@ class TestStringIO < Test::Unit::TestCase f.close unless f.closed? end + def test_write_nonblock_no_exceptions + s = "" + f = StringIO.new(s, "w") + f.write_nonblock("foo", exception: false) + f.close + assert_equal("foo", s) + end + def test_write_nonblock s = "" f = StringIO.new(s, "w") @@ -437,7 +445,7 @@ class TestStringIO < Test::Unit::TestCase f = StringIO.new("\u3042\u3044") assert_raise(ArgumentError) { f.readpartial(-1) } assert_raise(ArgumentError) { f.readpartial(1, 2, 3) } - assert_equal("\u3042\u3044", f.readpartial) + assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.readpartial(100)) f.rewind assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.readpartial(f.size)) f.rewind @@ -450,7 +458,19 @@ class TestStringIO < Test::Unit::TestCase f = StringIO.new("\u3042\u3044") assert_raise(ArgumentError) { f.read_nonblock(-1) } assert_raise(ArgumentError) { f.read_nonblock(1, 2, 3) } - assert_equal("\u3042\u3044", f.read_nonblock) + assert_equal("\u3042\u3044".force_encoding("BINARY"), f.read_nonblock(100)) + assert_raise(EOFError) { f.read_nonblock(10) } + f.rewind + assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.read_nonblock(f.size)) + end + + def test_read_nonblock_no_exceptions + f = StringIO.new("\u3042\u3044") + assert_raise(ArgumentError) { f.read_nonblock(-1, exception: false) } + assert_raise(ArgumentError) { f.read_nonblock(1, 2, 3, exception: false) } + assert_raise(ArgumentError) { f.read_nonblock } + assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.read_nonblock(100, exception: false)) + assert_equal(nil, f.read_nonblock(10, exception: false)) f.rewind assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.read_nonblock(f.size)) f.rewind -- cgit v1.2.3