add async feature #1

Merged
b4tman merged 62 commits from async into master 2023-08-18 06:29:46 +00:00
Showing only changes of commit 965c021d83 - Show all commits

View File

@ -282,7 +282,6 @@ where
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
use async_stream::stream;
use core::fmt::Debug; use core::fmt::Debug;
use tempfile::tempdir; use tempfile::tempdir;
@ -290,24 +289,21 @@ mod test {
struct TestData { struct TestData {
num: u64, num: u64,
test: String, test: String,
vnum: Vec<u64>,
vstr: Vec<String>,
} }
fn gen_data(count: usize) -> impl Iterator<Item = TestData> { fn gen_data(count: usize) -> impl Iterator<Item = TestData> {
(0..count).into_iter().map(|i| TestData { (0..count).into_iter().map(|i| TestData {
num: i as u64, num: i as u64,
test: "test".repeat(i), test: "test".repeat(i),
vnum: (0..i * 120).map(|x| (x ^ 0x345FE34) as u64).collect(),
vstr: (0..i * 111).map(|x| "test".repeat(x).to_string()).collect(),
}) })
} }
fn stream_iter<I, T>(it: I) -> impl Stream<Item = T> async fn assert_data_eq((x, y): (&TestData, TestData)) {
where assert_eq!(*x, y);
I: Iterator<Item = T>,
{
stream! {
for i in it {
yield i;
}
}
} }
#[tokio::test] #[tokio::test]
@ -324,7 +320,7 @@ mod test {
let items_iter = gen_data(5); let items_iter = gen_data(5);
let items: Vec<TestData> = items_iter.collect(); let items: Vec<TestData> = items_iter.collect();
let src = stream_iter(items.clone().into_iter()); let src = futures::stream::iter(items.clone());
pin_mut!(src); pin_mut!(src);
writer.load(src).await.expect("load"); writer.load(src).await.expect("load");
writer.finish().await.expect("finish write"); writer.finish().await.expect("finish write");
@ -344,7 +340,7 @@ mod test {
let items_iter = gen_data(5); let items_iter = gen_data(5);
let items: Vec<TestData> = items_iter.collect(); let items: Vec<TestData> = items_iter.collect();
let src = stream_iter(items.clone().into_iter()); let src = futures::stream::iter(items.clone());
pin_mut!(src); pin_mut!(src);
writer.load(src).await.expect("load"); writer.load(src).await.expect("load");
writer.finish().await.expect("finish write"); writer.finish().await.expect("finish write");
@ -372,7 +368,7 @@ mod test {
let items_iter = gen_data(5); let items_iter = gen_data(5);
let items: Vec<TestData> = items_iter.collect(); let items: Vec<TestData> = items_iter.collect();
let src = stream_iter(items.clone().into_iter()); let src = futures::stream::iter(items.clone());
pin_mut!(src); pin_mut!(src);
writer.load(src).await.expect("load"); writer.load(src).await.expect("load");
writer.finish().await.expect("finish write"); writer.finish().await.expect("finish write");
@ -381,13 +377,18 @@ mod test {
assert_eq!(items.len(), reader.len()); assert_eq!(items.len(), reader.len());
let dst_stream = reader.stream(); let dst_stream = reader.stream();
let src_stream = stream_iter(items.iter()); let src_stream = futures::stream::iter(items.iter());
async fn test_values((x, y): (&TestData, TestData)) { let mut count = 0;
assert_eq!(*x, y); src_stream
} .zip(dst_stream)
.map(|x| {
src_stream.zip(dst_stream).for_each(test_values).await; count += 1;
x
})
.for_each(assert_data_eq)
.await;
assert_eq!(count, items.len())
} }
/// sharing Reader instance between threads /// sharing Reader instance between threads
#[tokio::test] #[tokio::test]
@ -404,7 +405,7 @@ mod test {
let items_iter = gen_data(5); let items_iter = gen_data(5);
let items: Vec<TestData> = items_iter.collect(); let items: Vec<TestData> = items_iter.collect();
let src = stream_iter(items.clone().into_iter()); let src = futures::stream::iter(items.clone());
pin_mut!(src); pin_mut!(src);
writer.load(src).await.expect("load"); writer.load(src).await.expect("load");
writer.finish().await.expect("finish write"); writer.finish().await.expect("finish write");
@ -412,19 +413,15 @@ mod test {
let reader: Reader<TestData> = Reader::new(&tmpfile, 2048).await.expect("new reader"); let reader: Reader<TestData> = Reader::new(&tmpfile, 2048).await.expect("new reader");
assert_eq!(items.len(), reader.len()); assert_eq!(items.len(), reader.len());
async fn test_values((x, y): (&TestData, TestData)) {
assert_eq!(*x, y);
}
let reader = Arc::new(reader); let reader = Arc::new(reader);
for _ in 0..=3 { for _ in 0..=3 {
let cur_items = items.clone(); let cur_items = items.clone();
let cur_reader = Arc::clone(&reader); let cur_reader = Arc::clone(&reader);
tokio::spawn(async move { tokio::spawn(async move {
let dst_stream = cur_reader.stream(); let dst_stream = cur_reader.stream();
let src_stream = stream_iter(cur_items.iter()); let src_stream = futures::stream::iter(cur_items.iter());
src_stream.zip(dst_stream).for_each(test_values).await; src_stream.zip(dst_stream).for_each(assert_data_eq).await;
}); });
} }
} }