try_nonblock.diff

Yehuda Katz, 08/02/2011 01:01 PM

Download (20.2 KB)

View differences:

ext/openssl/lib/openssl/buffering.rb
182 182
    ret
183 183
  end
184 184

  
185
  def try_read_nonblock(maxlen, buf=nil)
186
    if maxlen == 0
187
      if buf
188
        buf.clear
189
        return buf
190
      else
191
        return ""
192
      end
193
    end
194
    if @rbuffer.empty?
195
      return try_sysread_nonblock(maxlen, buf)
196
    end
197
    ret = consume_rbuff(maxlen)
198
    if buf
199
      buf.replace(ret)
200
      ret = buf
201
    end
202
    raise EOFError if ret.empty?
203
    ret
204
  end
205

  
185 206
  ##
186 207
  # Reads the next "line+ from the stream.  Lines are separated by +eol+.  If
187 208
  # +limit+ is provided the result will not be longer than the given number of
......
374 395
    syswrite_nonblock(s)
375 396
  end
376 397

  
398
  def try_write_nonblock(s)
399
    flush
400
    try_syswrite_nonblock(s)
401
  end
402

  
377 403
  ##
378 404
  # Writes +s+ to the stream.  +s+ will be converted to a String using
379 405
  # String#to_s.
ext/openssl/ossl_ssl.c
1232 1232
}
1233 1233

  
1234 1234
static VALUE
1235
ossl_ssl_read_internal(int argc, VALUE *argv, VALUE self, int nonblock)
1235
ossl_ssl_read_internal(int argc, VALUE *argv, VALUE self, int nonblock, int no_exception)
1236 1236
{
1237 1237
    SSL *ssl;
1238 1238
    int ilen, nread = 0;
......
1260 1260
	    case SSL_ERROR_NONE:
1261 1261
		goto end;
1262 1262
	    case SSL_ERROR_ZERO_RETURN:
1263
		if (no_exception) { return Qnil; }
1263 1264
		rb_eof_error();
1264 1265
	    case SSL_ERROR_WANT_WRITE:
1266
		if (no_exception) { return ID2SYM(rb_intern("wait_writable")); }
1265 1267
                write_would_block(nonblock);
1266 1268
                rb_io_wait_writable(FPTR_TO_FD(fptr));
1267 1269
                continue;
1268 1270
	    case SSL_ERROR_WANT_READ:
1271
		if (no_exception) { return ID2SYM(rb_intern("wait_readable")); }
1269 1272
                read_would_block(nonblock);
1270 1273
                rb_io_wait_readable(FPTR_TO_FD(fptr));
1271 1274
		continue;
1272 1275
	    case SSL_ERROR_SYSCALL:
1273
		if(ERR_peek_error() == 0 && nread == 0) rb_eof_error();
1276
		if(ERR_peek_error() == 0 && nread == 0) {
1277
		    if (no_exception) { return Qnil; }
1278
		    rb_eof_error();
1279
		}
1274 1280
		rb_sys_fail(0);
1275 1281
	    default:
1276 1282
		ossl_raise(eSSLError, "SSL_read:");
......
1302 1308
static VALUE
1303 1309
ossl_ssl_read(int argc, VALUE *argv, VALUE self)
1304 1310
{
1305
    return ossl_ssl_read_internal(argc, argv, self, 0);
1311
    return ossl_ssl_read_internal(argc, argv, self, 0, 0);
1306 1312
}
1307 1313

  
1308 1314
/*
......
1319 1325
static VALUE
1320 1326
ossl_ssl_read_nonblock(int argc, VALUE *argv, VALUE self)
1321 1327
{
1322
    return ossl_ssl_read_internal(argc, argv, self, 1);
1328
    return ossl_ssl_read_internal(argc, argv, self, 1, 0);
1329
}
1330

  
1331
/*
1332
 * call-seq:
1333
 *    ssl.try_sysread_nonblock(length) => string, :wait_writable,
1334
 *      :wait_readable, or nil (for EOF)
1335
 *    ssl.try_sysread_nonblock(length, buffer) => buffer, :wait_writable,
1336
 *      :wait_readable, or nil (for EOF)
1337
 *
1338
 * Exactly the same as +sysread_nonblock+, except that instead of raising
1339
 * exceptions for EOF or when the read would block, it returns nil,
1340
 * :wait_readable or :wait_writable.
1341
 */
1342
static VALUE
1343
ossl_ssl_try_read_nonblock(int argc, VALUE *argv, VALUE self)
1344
{
1345
    return ossl_ssl_read_internal(argc, argv, self, 1, 1);
1323 1346
}
1324 1347

  
1325 1348
static VALUE
1326
ossl_ssl_write_internal(VALUE self, VALUE str, int nonblock)
1349
ossl_ssl_write_internal(VALUE self, VALUE str, int nonblock, int no_exception)
1327 1350
{
1328 1351
    SSL *ssl;
1329 1352
    int nwrite = 0;
......
1340 1363
	    case SSL_ERROR_NONE:
1341 1364
		goto end;
1342 1365
	    case SSL_ERROR_WANT_WRITE:
1366
		if (no_exception) { return ID2SYM(rb_intern("wait_writable")); }
1343 1367
                write_would_block(nonblock);
1344 1368
                rb_io_wait_writable(FPTR_TO_FD(fptr));
1345 1369
                continue;
1346 1370
	    case SSL_ERROR_WANT_READ:
1371
		if (no_exception) { return ID2SYM(rb_intern("wait_readable")); }
1347 1372
                read_would_block(nonblock);
1348 1373
                rb_io_wait_readable(FPTR_TO_FD(fptr));
1349 1374
                continue;
......
1373 1398
static VALUE
1374 1399
ossl_ssl_write(VALUE self, VALUE str)
1375 1400
{
1376
    return ossl_ssl_write_internal(self, str, 0);
1401
    return ossl_ssl_write_internal(self, str, 0, 0);
1377 1402
}
1378 1403

  
1379 1404
/*
......
1386 1411
static VALUE
1387 1412
ossl_ssl_write_nonblock(VALUE self, VALUE str)
1388 1413
{
1389
    return ossl_ssl_write_internal(self, str, 1);
1414
    return ossl_ssl_write_internal(self, str, 1, 0);
1415
}
1416

  
1417
/*
1418
 * call-seq:
1419
 *    ssl.syswrite_nonblock(string) => Integer, :wait_readable or
1420
 *      :wait_writable
1421
 *
1422
 * Exactly the same as +syswrite_nonblock+, except that instead of
1423
 * raising an exception if the write would block, returns
1424
 * :wait_readable or :wait_writable.
1425
 */
1426
static VALUE
1427
ossl_ssl_try_write_nonblock(VALUE self, VALUE str)
1428
{
1429
    return ossl_ssl_write_internal(self, str, 1, 1);
1390 1430
}
1391 1431

  
1392 1432
/*
......
1946 1986
    rb_define_method(cSSLSocket, "accept_nonblock",     ossl_ssl_accept_nonblock, 0);
1947 1987
    rb_define_method(cSSLSocket, "sysread",    ossl_ssl_read, -1);
1948 1988
    rb_define_private_method(cSSLSocket, "sysread_nonblock",    ossl_ssl_read_nonblock, -1);
1989
    rb_define_private_method(cSSLSocket, "try_sysread_nonblock",    ossl_ssl_try_read_nonblock, -1);
1949 1990
    rb_define_method(cSSLSocket, "syswrite",   ossl_ssl_write, 1);
1950 1991
    rb_define_private_method(cSSLSocket, "syswrite_nonblock",    ossl_ssl_write_nonblock, 1);
1992
    rb_define_private_method(cSSLSocket, "try_syswrite_nonblock",    ossl_ssl_try_write_nonblock, 1);
1951 1993
    rb_define_method(cSSLSocket, "sysclose",   ossl_ssl_close, 0);
1952 1994
    rb_define_method(cSSLSocket, "cert",       ossl_ssl_get_cert, 0);
1953 1995
    rb_define_method(cSSLSocket, "peer_cert",  ossl_ssl_get_peer_cert, 0);
ext/stringio/stringio.c
1286 1286
static VALUE
1287 1287
strio_sysread(int argc, VALUE *argv, VALUE self)
1288 1288
{
1289
    if (argc == 0) { rb_raise(rb_eArgError, "wrong number of arguments (0 for 1)", argc); }
1290

  
1289 1291
    VALUE val = strio_read(argc, argv, self);
1290 1292
    if (NIL_P(val)) {
1291 1293
	rb_eof_error();
......
1293 1295
    return val;
1294 1296
}
1295 1297

  
1298
/*
1299
 * call-seq:
1300
 *   strio.sysread(integer[, outbuf])    -> string or nil
1301
 *
1302
 * Exactly the same as +sysread+, except that instead of raising an
1303
 * EOFError at EOF, returns nil. This matches the +read_nonblock+
1304
 * protocol from the IO class.
1305
 */
1306
static VALUE
1307
strio_try_sysread(int argc, VALUE *argv, VALUE self)
1308
{
1309
    if (argc == 0) { rb_raise(rb_eArgError, "wrong number of arguments (0 for 1)", argc); }
1310

  
1311
    VALUE val = strio_read(argc, argv, self);
1312
    if (NIL_P(val)) { return Qnil; }
1313
    return val;
1314
}
1315

  
1296 1316
#define strio_syswrite strio_write
1297 1317

  
1298 1318
/*
......
1467 1487
    rb_define_method(StringIO, "sysread", strio_sysread, -1);
1468 1488
    rb_define_method(StringIO, "readpartial", strio_sysread, -1);
1469 1489
    rb_define_method(StringIO, "read_nonblock", strio_sysread, -1);
1490
    rb_define_method(StringIO, "try_read_nonblock", strio_try_sysread, -1);
1470 1491

  
1471 1492
    rb_define_method(StringIO, "write", strio_write, 1);
1472 1493
    rb_define_method(StringIO, "<<", strio_addstr, 1);
io.c
1883 1883
}
1884 1884

  
1885 1885
static VALUE
1886
io_getpartial(int argc, VALUE *argv, VALUE io, int nonblock)
1886
io_getpartial(int argc, VALUE *argv, VALUE io, int nonblock, int no_exception)
1887 1887
{
1888 1888
    rb_io_t *fptr;
1889 1889
    VALUE length, str;
......
1918 1918
        if (n < 0) {
1919 1919
            if (!nonblock && rb_io_wait_readable(fptr->fd))
1920 1920
                goto again;
1921
            if (nonblock && (errno == EWOULDBLOCK || errno == EAGAIN))
1922
                rb_mod_sys_fail(rb_mWaitReadable, "read would block");
1921
            if (nonblock && (errno == EWOULDBLOCK || errno == EAGAIN)) {
1922
                if (no_exception)
1923
                    return ID2SYM(rb_intern("wait_readable"));
1924
                else
1925
                    rb_mod_sys_fail(rb_mWaitReadable, "read would block");
1926
            }
1923 1927
            rb_sys_fail_path(fptr->pathv);
1924 1928
        }
1925 1929
    }
......
1993 1997
{
1994 1998
    VALUE ret;
1995 1999

  
1996
    ret = io_getpartial(argc, argv, io, 0);
2000
    ret = io_getpartial(argc, argv, io, 0, 0);
1997 2001
    if (NIL_P(ret))
1998 2002
        rb_eof_error();
1999 2003
    else
......
2054 2058
{
2055 2059
    VALUE ret;
2056 2060

  
2057
    ret = io_getpartial(argc, argv, io, 1);
2061
    ret = io_getpartial(argc, argv, io, 1, 0);
2058 2062
    if (NIL_P(ret))
2059 2063
        rb_eof_error();
2060 2064
    else
2061 2065
        return ret;
2062 2066
}
2063 2067

  
2068
/**
2069
 *  call-seq:
2070
 *     ios.try_read_nonblock(maxlen)              -> string, nil, or :wait_readable
2071
 *     ios.try_read_nonblock(maxlen, outbuf)      -> outbuf, nil, or :wait_readable
2072
 *
2073
 *  +try_read_nonblock+ is identical to +read_nonblock+,
2074
 *  except that instead of raising exceptions, blocking
2075
 *  calls will return :wait_readable, and EOF will
2076
 *  return nil.
2077
 */
2078
static VALUE
2079
io_try_read_nonblock(int argc, VALUE *argv, VALUE io)
2080
{
2081
    VALUE ret;
2082

  
2083
    ret = io_getpartial(argc, argv, io, 1, 1);
2084
    if (NIL_P(ret))
2085
        return Qnil;
2086
    else
2087
        return ret;
2088
}
2089

  
2090

  
2091
static VALUE
2092
io_write_nonblock(VALUE io, VALUE str, int no_exception)
2093
{
2094
    rb_io_t *fptr;
2095
    long n;
2096

  
2097
    rb_secure(4);
2098
    if (TYPE(str) != T_STRING)
2099
	str = rb_obj_as_string(str);
2100

  
2101
    io = GetWriteIO(io);
2102
    GetOpenFile(io, fptr);
2103
    rb_io_check_writable(fptr);
2104

  
2105
    if (io_fflush(fptr) < 0)
2106
        rb_sys_fail(0);
2107

  
2108
    rb_io_set_nonblock(fptr);
2109
    n = write(fptr->fd, RSTRING_PTR(str), RSTRING_LEN(str));
2110

  
2111
    if (n == -1) {
2112
        if (errno == EWOULDBLOCK || errno == EAGAIN) {
2113
	    if (no_exception) return ID2SYM(rb_intern("wait_writable"));
2114
	    rb_mod_sys_fail(rb_mWaitWritable, "write would block");
2115
	}
2116
        rb_sys_fail_path(fptr->pathv);
2117
    }
2118

  
2119
    return LONG2FIX(n);
2120
}
2121

  
2064 2122
/*
2065 2123
 *  call-seq:
2066 2124
 *     ios.write_nonblock(string)   -> integer
......
2117 2175
static VALUE
2118 2176
rb_io_write_nonblock(VALUE io, VALUE str)
2119 2177
{
2120
    rb_io_t *fptr;
2121
    long n;
2122

  
2123
    rb_secure(4);
2124
    if (TYPE(str) != T_STRING)
2125
	str = rb_obj_as_string(str);
2126

  
2127
    io = GetWriteIO(io);
2128
    GetOpenFile(io, fptr);
2129
    rb_io_check_writable(fptr);
2130

  
2131
    if (io_fflush(fptr) < 0)
2132
        rb_sys_fail(0);
2133

  
2134
    rb_io_set_nonblock(fptr);
2135
    n = write(fptr->fd, RSTRING_PTR(str), RSTRING_LEN(str));
2136

  
2137
    if (n == -1) {
2138
        if (errno == EWOULDBLOCK || errno == EAGAIN)
2139
            rb_mod_sys_fail(rb_mWaitWritable, "write would block");
2140
        rb_sys_fail_path(fptr->pathv);
2141
    }
2178
    return io_write_nonblock(io, str, 0);
2179
}
2142 2180

  
2143
    return LONG2FIX(n);
2181
/*
2182
 *  call-seq:
2183
 *    ios.try_write_nonblock(string)    -> integer or :wait_writable
2184
 *
2185
 *  Works exactly like write_nonblock, with one exception:
2186
 *
2187
 *  * if the write would block, <code>try_write_nonblock</code> returns
2188
 *    :wait_writable rather than raising IO::WaitWritable
2189
 */
2190
static VALUE
2191
rb_io_try_write_nonblock(VALUE io, VALUE str)
2192
{
2193
    return io_write_nonblock(io, str, 1);
2144 2194
}
2145 2195

  
2146 2196
/*
......
9703 9753
			 RUBY_METHOD_FUNC(0), Qnil, rb_eEOFError, (VALUE)0);
9704 9754
    }
9705 9755
    else {
9706
        tmp = io_getpartial(argc, argv, ARGF.current_file, nonblock);
9756
        tmp = io_getpartial(argc, argv, ARGF.current_file, nonblock, 0);
9707 9757
    }
9708 9758
    if (NIL_P(tmp)) {
9709 9759
        if (ARGF.next_p == -1) {
......
10594 10644
    rb_define_method(rb_cIO, "readlines",  rb_io_readlines, -1);
10595 10645

  
10596 10646
    rb_define_method(rb_cIO, "read_nonblock",  io_read_nonblock, -1);
10647
    rb_define_method(rb_cIO, "try_read_nonblock", io_try_read_nonblock, -1);
10597 10648
    rb_define_method(rb_cIO, "write_nonblock", rb_io_write_nonblock, 1);
10649
    rb_define_method(rb_cIO, "try_write_nonblock", rb_io_try_write_nonblock, 1);
10598 10650
    rb_define_method(rb_cIO, "readpartial",  io_readpartial, -1);
10599 10651
    rb_define_method(rb_cIO, "read",  io_read, -1);
10600 10652
    rb_define_method(rb_cIO, "write", io_write_m, 1);
test/openssl/test_pair.rb
157 157
      ret = nil
158 158
      assert_nothing_raised("[ruby-core:20298]") { ret = s2.read_nonblock(10) }
159 159
      assert_equal("def\n", ret)
160
      s1.close
161
      assert_raise(EOFError) { s2.read_nonblock(10) }
162
    }
163
  end
164

  
165
  def test_try_read_nonblock
166
    ssl_pair {|s1, s2|
167
      assert_equal :wait_readable, s2.try_read_nonblock(10)
168
      s1.write "abc\ndef\n"
169
      IO.select([s2])
170
      assert_equal("ab", s2.try_read_nonblock(2))
171
      assert_equal("c\n", s2.gets)
172
      ret = nil
173
      assert_nothing_raised("[ruby-core:20298]") { ret = s2.try_read_nonblock(10) }
174
      assert_equal("def\n", ret)
175
      s1.close
176
      assert_equal(nil, s2.try_read_nonblock(10))
160 177
    }
161 178
  end
162 179

  
180
  def write_nonblock(socket, meth, str)
181
    ret = socket.send(meth, str)
182
    ret.is_a?(Symbol) ? 0 : ret
183
  end
184

  
163 185
  def test_write_nonblock
164 186
    ssl_pair {|s1, s2|
165 187
      n = 0
166 188
      begin
167
        n += s1.write_nonblock("a" * 100000)
168
        n += s1.write_nonblock("b" * 100000)
169
        n += s1.write_nonblock("c" * 100000)
170
        n += s1.write_nonblock("d" * 100000)
171
        n += s1.write_nonblock("e" * 100000)
172
        n += s1.write_nonblock("f" * 100000)
189
        n += write_nonblock s1, :write_nonblock, "a" * 100000
190
        n += write_nonblock s1, :write_nonblock, "b" * 100000
191
        n += write_nonblock s1, :write_nonblock, "c" * 100000
192
        n += write_nonblock s1, :write_nonblock, "d" * 100000
193
        n += write_nonblock s1, :write_nonblock, "e" * 100000
194
        n += write_nonblock s1, :write_nonblock, "f" * 100000
173 195
      rescue IO::WaitWritable
174 196
      end
175 197
      s1.close
......
177 199
    }
178 200
  end
179 201

  
202
  def test_try_write_nonblock
203
    ssl_pair {|s1, s2|
204
      n = 0
205
      n += write_nonblock s1, :try_write_nonblock, "a" * 100000
206
      n += write_nonblock s1, :try_write_nonblock, "b" * 100000
207
      n += write_nonblock s1, :try_write_nonblock, "c" * 100000
208
      n += write_nonblock s1, :try_write_nonblock, "d" * 100000
209
      n += write_nonblock s1, :try_write_nonblock, "e" * 100000
210
      n += write_nonblock s1, :try_write_nonblock, "f" * 100000
211
      s1.close
212
      assert_equal(n, s2.read.length)
213
    }
214
  end
215

  
180 216
  def test_write_nonblock_with_buffered_data
181 217
    ssl_pair {|s1, s2|
182 218
      s1.write "foo"
......
187 223
    }
188 224
  end
189 225

  
226
  def test_try_write_nonblock_with_buffered_data
227
    ssl_pair {|s1, s2|
228
      s1.write "foo"
229
      s1.try_write_nonblock("bar")
230
      s1.write "baz"
231
      s1.close
232
      assert_equal("foobarbaz", s2.read)
233
    }
234
  end
235

  
190 236
  def test_connect_accept_nonblock
191 237
    host = "127.0.0.1"
192 238
    port = 0
test/ruby/test_io.rb
1002 1002
    end)
1003 1003
  end
1004 1004

  
1005
  def test_try_write_nonblock
1006
    skip "IO#write_nonblock is not supported on file/pipe." if /mswin|bccwin|mingw/ =~ RUBY_PLATFORM
1007
    pipe(proc do |w|
1008
      w.try_write_nonblock(1)
1009
      w.close
1010
    end, proc do |r|
1011
      assert_equal("1", r.read)
1012
    end)
1013
  end
1014

  
1005 1015
  def test_read_nonblock_error
1006 1016
    return if !have_nonblock?
1007 1017
    skip "IO#read_nonblock is not supported on file/pipe." if /mswin|bccwin|mingw/ =~ RUBY_PLATFORM
......
1012 1022
        assert_kind_of(IO::WaitReadable, $!)
1013 1023
      end
1014 1024
    }
1025

  
1026
    with_pipe {|r, w|
1027
      begin
1028
        r.read_nonblock 4096, ""
1029
      rescue Errno::EWOULDBLOCK
1030
        assert_kind_of(IO::WaitReadable, $!)
1031
      end
1032
    }
1033
  end
1034

  
1035
  def test_try_read_nonblock
1036
    return if !have_nonblock?
1037
    skip "IO#try_read_nonblock is not supported on file/pipe." if /mswin|bccwin|mingw/ =~ RUBY_PLATFORM
1038
    with_pipe {|r, w|
1039
      assert_equal :wait_readable, r.try_read_nonblock(4096)
1040
      w.puts "HI!"
1041
      assert_equal "HI!\n", r.try_read_nonblock(4096)
1042
      w.close
1043
      assert_equal nil, r.try_read_nonblock(4096)
1044
    }
1045
  end
1046

  
1047
  def test_try_read_nonblock_with_buffer
1048
    return if !have_nonblock?
1049
    skip "IO#try_read_nonblock is not supported on file/pipe." if /mswin|bccwin|mingw/ =~ RUBY_PLATFORM
1050
    with_pipe {|r, w|
1051
      assert_equal :wait_readable, r.try_read_nonblock(4096, "")
1052
      w.puts "HI!"
1053
      buf = "buf"
1054
      value = r.try_read_nonblock(4096, buf)
1055
      assert_equal value, "HI!\n"
1056
      assert buf.equal?(value)
1057
      w.close
1058
      assert_equal nil, r.try_read_nonblock(4096, "")
1059
    }
1015 1060
  end
1016 1061

  
1017 1062
  def test_write_nonblock_error
......
1028 1073
    }
1029 1074
  end
1030 1075

  
1076
  def test_try_write_nonblock
1077
    return if !have_nonblock?
1078
    skip "IO#write_nonblock is not supported on file/pipe." if /mswin|bccwin|mingw/ =~ RUBY_PLATFORM
1079
    with_pipe {|r, w|
1080
      loop {
1081
        ret = w.try_write_nonblock "a"*100000
1082
        if ret.is_a?(Symbol)
1083
          assert ret == :wait_writable
1084
          break
1085
        end
1086
      }
1087
    }
1088
  end
1089

  
1031 1090
  def test_gets
1032 1091
    pipe(proc do |w|
1033 1092
      w.write "foobarbaz"
test/socket/test_nonblock.rb
190 190
    s.close if s
191 191
  end
192 192

  
193
  def test_try_read_nonblock
194
    c, s = tcp_pair
195
    assert_equal :read_would_block, c.try_read_nonblock(100)
196
    assert_equal :read_would_block, s.try_read_nonblock(100)
197
    c.write("abc")
198
    IO.select [s]
199
    assert_equal("a", s.try_read_nonblock(1))
200
    assert_equal("bc", s.try_read_nonblock(100))
201
    assert_equal :read_would_block, s.try_read_nonblock(100)
202
  ensure
203
    c.close if c
204
    s.close if s
205
  end
206

  
193 207
=begin
194 208
  def test_write_nonblock
195 209
    c, s = tcp_pair
test/stringio/test_stringio.rb
424 424
    f = StringIO.new("\u3042\u3044")
425 425
    assert_raise(ArgumentError) { f.readpartial(-1) }
426 426
    assert_raise(ArgumentError) { f.readpartial(1, 2, 3) }
427
    assert_equal("\u3042\u3044", f.readpartial)
427
    assert_raise(ArgumentError) { f.readpartial }
428
    assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.readpartial(100))
428 429
    f.rewind
429 430
    assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.readpartial(f.size))
430 431
  end
......
433 434
    f = StringIO.new("\u3042\u3044")
434 435
    assert_raise(ArgumentError) { f.read_nonblock(-1) }
435 436
    assert_raise(ArgumentError) { f.read_nonblock(1, 2, 3) }
436
    assert_equal("\u3042\u3044", f.read_nonblock)
437
    assert_raise(ArgumentError) { f.read_nonblock }
438
    assert_equal("\u3042\u3044".force_encoding("BINARY"), f.read_nonblock(100))
439
    assert_raise(EOFError) { f.read_nonblock(10) }
440
    f.rewind
441
    assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.read_nonblock(f.size))
442
  end
443

  
444
  def test_try_read_nonblock
445
    f = StringIO.new("\u3042\u3044")
446
    assert_raise(ArgumentError) { f.try_read_nonblock(-1) }
447
    assert_raise(ArgumentError) { f.try_read_nonblock(1, 2, 3) }
448
    assert_raise(ArgumentError) { f.try_read_nonblock }
449
    assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.try_read_nonblock(100))
450
    assert_equal(nil, f.try_read_nonblock(10))
437 451
    f.rewind
438 452
    assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.read_nonblock(f.size))
439 453
  end