Allow checking out AppendBufs from Pool

This commit is contained in:
Carl Lerche
2016-07-23 09:54:18 -07:00
parent d650404bb8
commit 41b722dee8
2 changed files with 37 additions and 1 deletions
+31
View File
@@ -1,4 +1,5 @@
use bytes::{ByteStr, Buf, MutBuf};
use bytes::alloc::Pool;
use bytes::buf::AppendBuf;
#[test]
@@ -28,3 +29,33 @@ pub fn test_initial_buf_empty() {
assert_eq!(dst, b"hello world");
}
}
#[test]
pub fn test_append_buf_from_pool() {
let pool = Pool::with_capacity(2, 256);
// Run in a loop a bunch in hope that if there is a memory issue, it will
// be exposed
for _ in 0..1000 {
let mut buf = pool.new_append_buf().unwrap();
let mut dst: Vec<u8> = vec![];
assert_eq!(buf.remaining(), 256);
buf.write_slice(b"hello world");
assert_eq!(buf.remaining(), 245);
assert_eq!(buf.bytes(), b"hello world");
let view1 = buf.slice(0, 11);
view1.buf().copy_to(&mut dst).unwrap();
assert_eq!(dst, b"hello world");
assert_eq!(view1, buf.slice(0, 11));
drop(buf);
let mut buf = pool.new_append_buf().unwrap();
buf.write_slice(b"zomg no no no no");
assert_eq!(dst, b"hello world");
}
}