Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Add unit test for ChunkedCursor #2907

Merged
merged 2 commits into from
Aug 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 41 additions & 6 deletions core/src/raw/oio/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ impl oio::Stream for Cursor {
}
}

/// ChunkedCursor is used represents a non-contiguous bytes in memory.
///
/// This is useful when we buffer users' random writes without copy. ChunkedCursor implements
/// [`oio::Stream`] so it can be used in [`oio::Write::sink`] directly.
///
/// # TODO
///
/// we can do some compaction during runtime. For example, merge 4K data
Expand Down Expand Up @@ -205,11 +210,6 @@ impl ChunkedCursor {
self.inner.iter().skip(self.idx).map(|v| v.len()).sum()
}

/// Reset current cursor to start.
pub fn reset(&mut self) {
self.idx = 0;
}

/// Clear the entire cursor.
pub fn clear(&mut self) {
self.idx = 0;
Expand All @@ -234,7 +234,7 @@ impl oio::Stream for ChunkedCursor {
}

fn poll_reset(&mut self, _: &mut Context<'_>) -> Poll<Result<()>> {
self.reset();
self.idx = 0;
Poll::Ready(Ok(()))
}
}
Expand Down Expand Up @@ -402,6 +402,8 @@ impl VectorCursor {
#[cfg(test)]
mod tests {
use super::*;
use crate::raw::oio::StreamExt;
use pretty_assertions::assert_eq;

#[test]
fn test_vector_cursor() {
Expand All @@ -422,4 +424,37 @@ mod tests {
vc.take(5);
assert_eq!(vc.peak_exact(1), Bytes::from("r"));
}

#[tokio::test]
async fn test_chunked_cursor() -> Result<()> {
let mut c = ChunkedCursor::new();

c.push(Bytes::from("hello"));
assert_eq!(c.len(), 5);
assert!(!c.is_empty());

c.push(Bytes::from("world"));
assert_eq!(c.len(), 10);
assert!(!c.is_empty());

let bs = c.next().await.unwrap().unwrap();
assert_eq!(bs, Bytes::from("hello"));
assert_eq!(c.len(), 5);
assert!(!c.is_empty());

let bs = c.next().await.unwrap().unwrap();
assert_eq!(bs, Bytes::from("world"));
assert_eq!(c.len(), 0);
assert!(c.is_empty());

c.reset().await?;
assert_eq!(c.len(), 10);
assert!(!c.is_empty());

c.clear();
assert_eq!(c.len(), 0);
assert!(c.is_empty());

Ok(())
}
}