Permalink
Browse files

Fix error in read_from when the number of bytes read is less than the…

… maximum possible
  • Loading branch information...
1 parent 055bef9 commit 641327b5937dd8d6fdf4a8ed1de9e5e8047b5724 @loganb loganb committed Feb 14, 2012
Showing with 25 additions and 3 deletions.
  1. +2 −2 ext/iobuffer.c
  2. +23 −1 spec/buffer_spec.rb
View
@@ -702,8 +702,8 @@ static int buffer_read_from(struct buffer *buf, int fd)
}
total_bytes_read += bytes_read;
- buf->tail->end += nbytes;
- buf->size += nbytes;
+ buf->tail->end += bytes_read;
+ buf->size += bytes_read;
if(buf->tail->end == buf->node_size) {
buf->tail->next = buffer_node_new(buf);
View
@@ -170,7 +170,29 @@
@buffer.read_from(rp)
@buffer.size.should == 2*str.length
end
-
+
+ it "Can handle lots of data" do
+ (rp, wp) = File.pipe
+ rng = Random.new(1)
+
+ total = 0
+ 100.times do
+ chunk_size = rng.rand(2048) #We don't actually know the pipe buffer size!
+ if rng.rand > 0.5
+ wp.write("x" * chunk_size)
+ @buffer.read_from(rp)
+ else
+ @buffer.append("x" * chunk_size)
+ end
+ total += chunk_size
+ end
+ wp.close
+ @buffer.read_from(rp)
+
+ @buffer.size.should == total
+ @buffer.read.should == "x" * total
+ end
+
#######
private
#######

0 comments on commit 641327b

Please sign in to comment.