nonblock_no_tuple.patch

nonblock without a tuple - Aaron Patterson, 07/10/2013 06:24 AM

Download (20.3 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
......
375 396
    syswrite_nonblock(s)
376 397
  end
377 398

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

  
378 404
  ##
379 405
  # Writes +s+ to the stream.  +s+ will be converted to a String using
380 406
  # String#to_s.
ext/openssl/ossl_ssl.c
1369 1369
}
1370 1370

  
1371 1371
static VALUE
1372
ossl_ssl_read_internal(int argc, VALUE *argv, VALUE self, int nonblock)
1372
ossl_ssl_read_internal(int argc, VALUE *argv, VALUE self, int nonblock, int no_exception)
1373 1373
{
1374 1374
    SSL *ssl;
1375 1375
    int ilen, nread = 0;
......
1397 1397
	    case SSL_ERROR_NONE:
1398 1398
		goto end;
1399 1399
	    case SSL_ERROR_ZERO_RETURN:
1400
		if (no_exception) { return Qnil; }
1400 1401
		rb_eof_error();
1401 1402
	    case SSL_ERROR_WANT_WRITE:
1403
		if (no_exception) { return ID2SYM(rb_intern("wait_writable")); }
1402 1404
                write_would_block(nonblock);
1403 1405
                rb_io_wait_writable(FPTR_TO_FD(fptr));
1404 1406
                continue;
1405 1407
	    case SSL_ERROR_WANT_READ:
1408
		if (no_exception) { return ID2SYM(rb_intern("wait_readable")); }
1406 1409
                read_would_block(nonblock);
1407 1410
                rb_io_wait_readable(FPTR_TO_FD(fptr));
1408 1411
		continue;
1409 1412
	    case SSL_ERROR_SYSCALL:
1410
		if(ERR_peek_error() == 0 && nread == 0) rb_eof_error();
1413
		if(ERR_peek_error() == 0 && nread == 0) {
1414
		    if (no_exception) { return Qnil; }
1415
		    rb_eof_error();
1416
		}
1411 1417
		rb_sys_fail(0);
1412 1418
	    default:
1413 1419
		ossl_raise(eSSLError, "SSL_read");
......
1415 1421
        }
1416 1422
    }
1417 1423
    else {
1418
        ID meth = nonblock ? rb_intern("read_nonblock") : rb_intern("sysread");
1424
        ID meth;
1425
	if (nonblock) {
1426
	    if (no_exception) {
1427
		meth = rb_intern("try_read_nonblock");
1428
	    } else {
1429
		meth = rb_intern("read_nonblock");
1430
	    }
1431
	} else {
1432
	  meth = rb_intern("sysread");
1433
	}
1419 1434
        rb_warning("SSL session is not started yet.");
1420 1435
        return rb_funcall(ossl_ssl_get_io(self), meth, 2, len, str);
1421 1436
    }
......
1438 1453
static VALUE
1439 1454
ossl_ssl_read(int argc, VALUE *argv, VALUE self)
1440 1455
{
1441
    return ossl_ssl_read_internal(argc, argv, self, 0);
1456
    return ossl_ssl_read_internal(argc, argv, self, 0, 0);
1442 1457
}
1443 1458

  
1444 1459
/*
......
1455 1470
static VALUE
1456 1471
ossl_ssl_read_nonblock(int argc, VALUE *argv, VALUE self)
1457 1472
{
1458
    return ossl_ssl_read_internal(argc, argv, self, 1);
1473
    return ossl_ssl_read_internal(argc, argv, self, 1, 0);
1474
}
1475

  
1476
/*
1477
 * call-seq:
1478
 *    ssl.try_sysread_nonblock(length) => string, :wait_writable,
1479
 *      :wait_readable, or nil (for EOF)
1480
 *    ssl.try_sysread_nonblock(length, buffer) => buffer, :wait_writable,
1481
 *      :wait_readable, or nil (for EOF)
1482
 *
1483
 * Exactly the same as +sysread_nonblock+, except that instead of raising
1484
 * exceptions for EOF or when the read would block, it returns nil,
1485
 * :wait_readable or :wait_writable.
1486
 */
1487
static VALUE
1488
ossl_ssl_try_read_nonblock(int argc, VALUE *argv, VALUE self)
1489
{
1490
    return ossl_ssl_read_internal(argc, argv, self, 1, 1);
1459 1491
}
1460 1492

  
1461 1493
static VALUE
1462
ossl_ssl_write_internal(VALUE self, VALUE str, int nonblock)
1494
ossl_ssl_write_internal(VALUE self, VALUE str, int nonblock, int no_exception)
1463 1495
{
1464 1496
    SSL *ssl;
1465 1497
    int nwrite = 0;
......
1476 1508
	    case SSL_ERROR_NONE:
1477 1509
		goto end;
1478 1510
	    case SSL_ERROR_WANT_WRITE:
1511
		if (no_exception) { return ID2SYM(rb_intern("wait_writable")); }
1479 1512
                write_would_block(nonblock);
1480 1513
                rb_io_wait_writable(FPTR_TO_FD(fptr));
1481 1514
                continue;
1482 1515
	    case SSL_ERROR_WANT_READ:
1516
		if (no_exception) { return ID2SYM(rb_intern("wait_readable")); }
1483 1517
                read_would_block(nonblock);
1484 1518
                rb_io_wait_readable(FPTR_TO_FD(fptr));
1485 1519
                continue;
......
1509 1543
static VALUE
1510 1544
ossl_ssl_write(VALUE self, VALUE str)
1511 1545
{
1512
    return ossl_ssl_write_internal(self, str, 0);
1546
    return ossl_ssl_write_internal(self, str, 0, 0);
1513 1547
}
1514 1548

  
1515 1549
/*
......
1522 1556
static VALUE
1523 1557
ossl_ssl_write_nonblock(VALUE self, VALUE str)
1524 1558
{
1525
    return ossl_ssl_write_internal(self, str, 1);
1559
    return ossl_ssl_write_internal(self, str, 1, 0);
1560
}
1561

  
1562
/*
1563
 * call-seq:
1564
 *    ssl.syswrite_nonblock(string) => Integer, :wait_readable or
1565
 *      :wait_writable
1566
 *
1567
 * Exactly the same as +syswrite_nonblock+, except that instead of
1568
 * raising an exception if the write would block, returns
1569
 * :wait_readable or :wait_writable.
1570
 */
1571
static VALUE
1572
ossl_ssl_try_write_nonblock(VALUE self, VALUE str)
1573
{
1574
    return ossl_ssl_write_internal(self, str, 1, 1);
1526 1575
}
1527 1576

  
1528 1577
/*
......
2167 2216
    rb_define_method(cSSLSocket, "accept_nonblock",     ossl_ssl_accept_nonblock, 0);
2168 2217
    rb_define_method(cSSLSocket, "sysread",    ossl_ssl_read, -1);
2169 2218
    rb_define_private_method(cSSLSocket, "sysread_nonblock",    ossl_ssl_read_nonblock, -1);
2219
    rb_define_private_method(cSSLSocket, "try_sysread_nonblock",    ossl_ssl_try_read_nonblock, -1);
2170 2220
    rb_define_method(cSSLSocket, "syswrite",   ossl_ssl_write, 1);
2171 2221
    rb_define_private_method(cSSLSocket, "syswrite_nonblock",    ossl_ssl_write_nonblock, 1);
2222
    rb_define_private_method(cSSLSocket, "try_syswrite_nonblock",    ossl_ssl_try_write_nonblock, 1);
2172 2223
    rb_define_method(cSSLSocket, "sysclose",   ossl_ssl_close, 0);
2173 2224
    rb_define_method(cSSLSocket, "cert",       ossl_ssl_get_cert, 0);
2174 2225
    rb_define_method(cSSLSocket, "peer_cert",  ossl_ssl_get_peer_cert, 0);
ext/stringio/stringio.c
1341 1341
    return val;
1342 1342
}
1343 1343

  
1344
/*
1345
 * call-seq:
1346
 *   strio.sysread(integer[, outbuf])    -> string or nil
1347
 *
1348
 * Exactly the same as +sysread+, except that instead of raising an
1349
 * EOFError at EOF, returns nil. This matches the +read_nonblock+
1350
 * protocol from the IO class.
1351
 */
1352
static VALUE
1353
strio_try_sysread(int argc, VALUE *argv, VALUE self)
1354
{
1355
    if (argc == 0) { rb_raise(rb_eArgError, "wrong number of arguments (0 for 1)", argc); }
1356

  
1357
    VALUE val = strio_read(argc, argv, self);
1358
    if (NIL_P(val)) { return Qnil; }
1359
    return val;
1360
}
1361

  
1344 1362
#define strio_syswrite rb_io_write
1345 1363

  
1346 1364
#define strio_isatty strio_false
......
1542 1560
	rb_define_method(mReadable, "sysread", strio_sysread, -1);
1543 1561
	rb_define_method(mReadable, "readpartial", strio_sysread, -1);
1544 1562
	rb_define_method(mReadable, "read_nonblock", strio_sysread, -1);
1563
        rb_define_method(StringIO, "try_read_nonblock", strio_try_sysread, -1);
1545 1564
	rb_include_module(StringIO, mReadable);
1546 1565
    }
1547 1566
    {
io.c
2367 2367
rb_readwrite_sys_fail(int writable, const char *mesg);
2368 2368

  
2369 2369
static VALUE
2370
io_getpartial(int argc, VALUE *argv, VALUE io, int nonblock)
2370
io_getpartial(int argc, VALUE *argv, VALUE io, int nonblock, int no_exception)
2371 2371
{
2372 2372
    rb_io_t *fptr;
2373 2373
    VALUE length, str;
......
2403 2403
        if (n < 0) {
2404 2404
            if (!nonblock && rb_io_wait_readable(fptr->fd))
2405 2405
                goto again;
2406
            if (nonblock && (errno == EWOULDBLOCK || errno == EAGAIN))
2407
                rb_readwrite_sys_fail(RB_IO_WAIT_READABLE, "read would block");
2406
            if (nonblock && (errno == EWOULDBLOCK || errno == EAGAIN)) {
2407
                if (no_exception)
2408
                    return ID2SYM(rb_intern("wait_readable"));
2409
                else
2410
		    rb_readwrite_sys_fail(RB_IO_WAIT_READABLE, "read would block");
2411
            }
2408 2412
            rb_sys_fail_path(fptr->pathv);
2409 2413
        }
2410 2414
    }
......
2480 2484
{
2481 2485
    VALUE ret;
2482 2486

  
2483
    ret = io_getpartial(argc, argv, io, 0);
2487
    ret = io_getpartial(argc, argv, io, 0, 0);
2484 2488
    if (NIL_P(ret))
2485 2489
        rb_eof_error();
2486 2490
    return ret;
......
2542 2546
{
2543 2547
    VALUE ret;
2544 2548

  
2545
    ret = io_getpartial(argc, argv, io, 1);
2549
    ret = io_getpartial(argc, argv, io, 1, 0);
2546 2550
    if (NIL_P(ret))
2547 2551
        rb_eof_error();
2548 2552
    return ret;
2549 2553
}
2550 2554

  
2555
/**
2556
 *  call-seq:
2557
 *     ios.try_read_nonblock(maxlen)              -> string, nil, or :wait_readable
2558
 *     ios.try_read_nonblock(maxlen, outbuf)      -> outbuf, nil, or :wait_readable
2559
 *
2560
 *  +try_read_nonblock+ is identical to +read_nonblock+,
2561
 *  except that instead of raising exceptions, blocking
2562
 *  calls will return :wait_readable, and EOF will
2563
 *  return nil.
2564
 */
2565
static VALUE
2566
io_try_read_nonblock(int argc, VALUE *argv, VALUE io)
2567
{
2568
    VALUE ret;
2569

  
2570
    ret = io_getpartial(argc, argv, io, 1, 1);
2571
    if (NIL_P(ret))
2572
        return Qnil;
2573
    else
2574
        return ret;
2575
}
2576

  
2577

  
2578
static VALUE
2579
io_write_nonblock(VALUE io, VALUE str, int no_exception)
2580
{
2581
    rb_io_t *fptr;
2582
    long n;
2583

  
2584
    if (!RB_TYPE_P(str, T_STRING))
2585
	str = rb_obj_as_string(str);
2586

  
2587
    io = GetWriteIO(io);
2588
    GetOpenFile(io, fptr);
2589
    rb_io_check_writable(fptr);
2590

  
2591
    if (io_fflush(fptr) < 0)
2592
        rb_sys_fail(0);
2593

  
2594
    rb_io_set_nonblock(fptr);
2595
    n = write(fptr->fd, RSTRING_PTR(str), RSTRING_LEN(str));
2596

  
2597
    if (n == -1) {
2598
        if (errno == EWOULDBLOCK || errno == EAGAIN) {
2599
	    if (no_exception) {
2600
		return ID2SYM(rb_intern("wait_writable"));
2601
	    } else {
2602
		rb_readwrite_sys_fail(RB_IO_WAIT_WRITABLE, "write would block");
2603
	    }
2604
	}
2605
        rb_sys_fail_path(fptr->pathv);
2606
    }
2607

  
2608
    return LONG2FIX(n);
2609
}
2610

  
2551 2611
/*
2552 2612
 *  call-seq:
2553 2613
 *     ios.write_nonblock(string)   -> integer
......
2604 2664
static VALUE
2605 2665
rb_io_write_nonblock(VALUE io, VALUE str)
2606 2666
{
2607
    rb_io_t *fptr;
2608
    long n;
2609

  
2610
    if (!RB_TYPE_P(str, T_STRING))
2611
	str = rb_obj_as_string(str);
2612

  
2613
    io = GetWriteIO(io);
2614
    GetOpenFile(io, fptr);
2615
    rb_io_check_writable(fptr);
2616

  
2617
    if (io_fflush(fptr) < 0)
2618
        rb_sys_fail(0);
2619

  
2620
    rb_io_set_nonblock(fptr);
2621
    n = write(fptr->fd, RSTRING_PTR(str), RSTRING_LEN(str));
2622

  
2623
    if (n == -1) {
2624
        if (errno == EWOULDBLOCK || errno == EAGAIN)
2625
            rb_readwrite_sys_fail(RB_IO_WAIT_WRITABLE, "write would block");
2626
        rb_sys_fail_path(fptr->pathv);
2627
    }
2667
    return io_write_nonblock(io, str, 0);
2668
}
2628 2669

  
2629
    return LONG2FIX(n);
2670
/*
2671
 *  call-seq:
2672
 *    ios.try_write_nonblock(string)    -> integer or :wait_writable
2673
 *
2674
 *  Works exactly like write_nonblock, with one exception:
2675
 *
2676
 *  * if the write would block, <code>try_write_nonblock</code> returns
2677
 *    :wait_writable rather than raising IO::WaitWritable
2678
 */
2679
static VALUE
2680
rb_io_try_write_nonblock(VALUE io, VALUE str)
2681
{
2682
    return io_write_nonblock(io, str, 1);
2630 2683
}
2631 2684

  
2632 2685
/*
......
10756 10809
			 RUBY_METHOD_FUNC(0), Qnil, rb_eEOFError, (VALUE)0);
10757 10810
    }
10758 10811
    else {
10759
        tmp = io_getpartial(argc, argv, ARGF.current_file, nonblock);
10812
        tmp = io_getpartial(argc, argv, ARGF.current_file, nonblock, 0);
10760 10813
    }
10761 10814
    if (NIL_P(tmp)) {
10762 10815
        if (ARGF.next_p == -1) {
......
11798 11851
    rb_define_method(rb_cIO, "readlines",  rb_io_readlines, -1);
11799 11852

  
11800 11853
    rb_define_method(rb_cIO, "read_nonblock",  io_read_nonblock, -1);
11854
    rb_define_method(rb_cIO, "try_read_nonblock", io_try_read_nonblock, -1);
11801 11855
    rb_define_method(rb_cIO, "write_nonblock", rb_io_write_nonblock, 1);
11856
    rb_define_method(rb_cIO, "try_write_nonblock", rb_io_try_write_nonblock, 1);
11802 11857
    rb_define_method(rb_cIO, "readpartial",  io_readpartial, -1);
11803 11858
    rb_define_method(rb_cIO, "read",  io_read, -1);
11804 11859
    rb_define_method(rb_cIO, "write", io_write_m, 1);
test/openssl/test_pair.rb
156 156
      ret = nil
157 157
      assert_nothing_raised("[ruby-core:20298]") { ret = s2.read_nonblock(10) }
158 158
      assert_equal("def\n", ret)
159
      s1.close
160
      assert_raise(EOFError) { s2.read_nonblock(10) }
161
    }
162
  end
163

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

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

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

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

  
179 215
  def test_write_nonblock_with_buffered_data
180 216
    ssl_pair {|s1, s2|
181 217
      s1.write "foo"
......
186 222
    }
187 223
  end
188 224

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

  
189 235
  def test_connect_accept_nonblock
190 236
    host = "127.0.0.1"
191 237
    port = 0
test/ruby/test_io.rb
1119 1119
    }
1120 1120
  end
1121 1121

  
1122
  def test_try_write_nonblock
1123
    skip "IO#write_nonblock is not supported on file/pipe." if /mswin|bccwin|mingw/ =~ RUBY_PLATFORM
1124
    pipe(proc do |w|
1125
      w.try_write_nonblock(1)
1126
      w.close
1127
    end, proc do |r|
1128
      assert_equal("1", r.read)
1129
    end)
1130
  end
1131

  
1122 1132
  def test_read_nonblock_error
1123 1133
    return if !have_nonblock?
1124 1134
    skip "IO#read_nonblock is not supported on file/pipe." if /mswin|bccwin|mingw/ =~ RUBY_PLATFORM
......
1129 1139
        assert_kind_of(IO::WaitReadable, $!)
1130 1140
      end
1131 1141
    }
1142

  
1143
    with_pipe {|r, w|
1144
      begin
1145
        r.read_nonblock 4096, ""
1146
      rescue Errno::EWOULDBLOCK
1147
        assert_kind_of(IO::WaitReadable, $!)
1148
      end
1149
    }
1150
  end
1151

  
1152
  def test_try_read_nonblock
1153
    return if !have_nonblock?
1154
    skip "IO#try_read_nonblock is not supported on file/pipe." if /mswin|bccwin|mingw/ =~ RUBY_PLATFORM
1155
    with_pipe {|r, w|
1156
      assert_equal :wait_readable, r.try_read_nonblock(4096)
1157
      w.puts "HI!"
1158
      assert_equal "HI!\n", r.try_read_nonblock(4096)
1159
      w.close
1160
      assert_equal nil, r.try_read_nonblock(4096)
1161
    }
1162
  end
1163

  
1164
  def test_try_read_nonblock_with_buffer
1165
    return if !have_nonblock?
1166
    skip "IO#try_read_nonblock is not supported on file/pipe." if /mswin|bccwin|mingw/ =~ RUBY_PLATFORM
1167
    with_pipe {|r, w|
1168
      assert_equal :wait_readable, r.try_read_nonblock(4096, "")
1169
      w.puts "HI!"
1170
      buf = "buf"
1171
      value = r.try_read_nonblock(4096, buf)
1172
      assert_equal value, "HI!\n"
1173
      assert buf.equal?(value)
1174
      w.close
1175
      assert_equal nil, r.try_read_nonblock(4096, "")
1176
    }
1132 1177
  end
1133 1178

  
1134 1179
  def test_write_nonblock_error
......
1145 1190
    }
1146 1191
  end
1147 1192

  
1193
  def test_try_write_nonblock
1194
    return if !have_nonblock?
1195
    skip "IO#write_nonblock is not supported on file/pipe." if /mswin|bccwin|mingw/ =~ RUBY_PLATFORM
1196
    with_pipe {|r, w|
1197
      loop {
1198
        ret = w.try_write_nonblock "a"*100000
1199
        if ret.is_a?(Symbol)
1200
          assert ret == :wait_writable
1201
          break
1202
        end
1203
      }
1204
    }
1205
  end
1206

  
1148 1207
  def test_gets
1149 1208
    pipe(proc do |w|
1150 1209
      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 :wait_readable, c.try_read_nonblock(100)
196
    assert_equal :wait_readable, 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 :wait_readable, 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
437 437
    f = StringIO.new("\u3042\u3044")
438 438
    assert_raise(ArgumentError) { f.readpartial(-1) }
439 439
    assert_raise(ArgumentError) { f.readpartial(1, 2, 3) }
440
    assert_equal("\u3042\u3044", f.readpartial)
440
    assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.readpartial(100))
441 441
    f.rewind
442 442
    assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.readpartial(f.size))
443 443
    f.rewind
......
450 450
    f = StringIO.new("\u3042\u3044")
451 451
    assert_raise(ArgumentError) { f.read_nonblock(-1) }
452 452
    assert_raise(ArgumentError) { f.read_nonblock(1, 2, 3) }
453
    assert_equal("\u3042\u3044", f.read_nonblock)
453
    assert_equal("\u3042\u3044".force_encoding("BINARY"), f.read_nonblock(100))
454
    assert_raise(EOFError) { f.read_nonblock(10) }
455
    f.rewind
456
    assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.read_nonblock(f.size))
457
  end
458

  
459
  def test_try_read_nonblock
460
    f = StringIO.new("\u3042\u3044")
461
    assert_raise(ArgumentError) { f.try_read_nonblock(-1) }
462
    assert_raise(ArgumentError) { f.try_read_nonblock(1, 2, 3) }
463
    assert_raise(ArgumentError) { f.try_read_nonblock }
464
    assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.try_read_nonblock(100))
465
    assert_equal(nil, f.try_read_nonblock(10))
454 466
    f.rewind
455 467
    assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.read_nonblock(f.size))
456 468
    f.rewind