diff options
author | Eric Wong <normalperson@yhbt.net> | 2010-10-08 02:53:38 -0700 |
---|---|---|
committer | Eric Wong <e@yhbt.net> | 2010-10-08 09:58:58 +0000 |
commit | d225ede82d820d045bd7cfb826f444cf6601577c (patch) | |
tree | 1515ff26c7645a643048359827e61191c189095e | |
parent | e4599227f0da0f652cbcb52838e631d7384dcd0d (diff) | |
download | kgio-d225ede82d820d045bd7cfb826f444cf6601577c.tar.gz |
This matches behavior of all the core Ruby methods.
-rw-r--r-- | ext/kgio/read_write.c | 20 | ||||
-rw-r--r-- | test/lib_read_write.rb | 14 |
2 files changed, 27 insertions, 7 deletions
diff --git a/ext/kgio/read_write.c b/ext/kgio/read_write.c index 0ee7866..141b078 100644 --- a/ext/kgio/read_write.c +++ b/ext/kgio/read_write.c @@ -92,11 +92,14 @@ static VALUE my_read(int io_wait, int argc, VALUE *argv, VALUE io) long n; prepare_read(&a, argc, argv, io); - set_nonblocking(a.fd); + + if (a.len > 0) { + set_nonblocking(a.fd); retry: - n = (long)read(a.fd, a.ptr, a.len); - if (read_check(&a, n, "read", io_wait) != 0) - goto retry; + n = (long)read(a.fd, a.ptr, a.len); + if (read_check(&a, n, "read", io_wait) != 0) + goto retry; + } return a.buf; } @@ -159,10 +162,13 @@ static VALUE my_recv(int io_wait, int argc, VALUE *argv, VALUE io) long n; prepare_read(&a, argc, argv, io); + + if (a.len > 0) { retry: - n = (long)recv(a.fd, a.ptr, a.len, MSG_DONTWAIT); - if (read_check(&a, n, "recv", io_wait) != 0) - goto retry; + n = (long)recv(a.fd, a.ptr, a.len, MSG_DONTWAIT); + if (read_check(&a, n, "recv", io_wait) != 0) + goto retry; + } return a.buf; } diff --git a/test/lib_read_write.rb b/test/lib_read_write.rb index 6296586..6f90041 100644 --- a/test/lib_read_write.rb +++ b/test/lib_read_write.rb @@ -18,6 +18,20 @@ module LibReadWriteTest end end + def test_read_zero + assert_equal "", @rd.kgio_read(0) + buf = "foo" + assert_equal buf.object_id, @rd.kgio_read(0, buf).object_id + assert_equal "", buf + end + + def test_tryread_zero + assert_equal "", @rd.kgio_tryread(0) + buf = "foo" + assert_equal buf.object_id, @rd.kgio_tryread(0, buf).object_id + assert_equal "", buf + end + def test_read_eof @wr.close assert_nil @rd.kgio_read(5) |