diff options
author | Matthew Draper <matthew@trebex.net> | 2012-02-28 23:39:09 +1030 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2012-02-28 20:37:23 +0000 |
commit | 8a3f48341248b919b5c4816bf4f7079eb35d6882 (patch) | |
tree | 057b62e8a24fef38a4961648955c82f57f777ce8 | |
parent | 55365e01dc0b2b5db01441004f4c6921d1fd04d6 (diff) | |
download | mogilefs-client-8a3f48341248b919b5c4816bf4f7079eb35d6882.tar.gz |
Leave size unchanged, so it's available for easy comparison with buf.bytesize. [ew: added test case] Acked-by: Eric Wong <normalperson@yhbt.net>
-rw-r--r-- | lib/mogilefs/socket_common.rb | 4 | ||||
-rw-r--r-- | test/socket_test.rb | 14 |
2 files changed, 16 insertions, 2 deletions
diff --git a/lib/mogilefs/socket_common.rb b/lib/mogilefs/socket_common.rb index a716d35..5ba8c10 100644 --- a/lib/mogilefs/socket_common.rb +++ b/lib/mogilefs/socket_common.rb @@ -44,9 +44,9 @@ module MogileFS::SocketCommon def read(size, buf = "", timeout = 5) timed_read(size, buf, timeout) or return # nil/EOF - while (size -= buf.bytesize) > 0 + while (remaining = size - buf.bytesize) > 0 tmp ||= "" - timed_read(size, tmp, timeout) or return buf # truncated + timed_read(remaining, tmp, timeout) or return buf # truncated buf << tmp end diff --git a/test/socket_test.rb b/test/socket_test.rb index c4dcf02..e217da5 100644 --- a/test/socket_test.rb +++ b/test/socket_test.rb @@ -95,4 +95,18 @@ module SocketTest accepted.close assert_nil sock.timed_gets end + + def test_read_in_full + sock = MogileFS::Socket.tcp(@host, @port) + accepted = @srv.accept + thr = Thread.new do + accepted.write "HELLO" + sleep 0.1 + accepted.write " " + sleep 0.1 + accepted.write "WORLD!!!" + end + buf = sock.read("HELLO WORLD!!!".bytesize) + assert_equal "HELLO WORLD!!!", buf + end end |