Skip to content

Commit

Permalink
Async trait tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alpha-tango-kilo committed Nov 11, 2023
1 parent d3d37b4 commit 463d532
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 6 deletions.
15 changes: 10 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ repository = "https://codeberg.org/alpha-tango-kilo/close_already"

[features]
default = ["backend-threadpool"]
backend-async-std = ["async-std"]
backend-rayon = ["rayon"]
backend-smol = ["smol"]
backend-threadpool = ["threadpool"]
backend-tokio = ["tokio"]
backend-async-std = ["dep:async-std"]
backend-rayon = ["dep:rayon"]
backend-smol = ["dep:smol"]
backend-threadpool = ["dep:threadpool"]
backend-tokio = ["dep:tokio"]

[dependencies]
mutually_exclusive_features = "0.0.3"
Expand All @@ -27,5 +27,10 @@ smol = { version = "1", optional = true }
tokio = { version = "1", features = ["rt", "fs"], optional = true }
threadpool = { version = "1.7", optional = true }

[dev-dependencies]
tempfile = "3"
# Have to include this so we can get Async{Read,Write}Ext through feature unification
tokio = { version = "1", features = ["io-util"] }

[package.metadata.docs.rs]
default-target = "x86_64-pc-windows-msvc"
1 change: 1 addition & 0 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ clippy:
--exclude-no-default-features \
--exclude-all-features \
clippy \
--tests \
--target x86_64-pc-windows-msvc

# Run tests against all backends
Expand Down
65 changes: 64 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,5 +664,68 @@ mod tests {
}
}

// TODO: add trait implementation tests
#[cfg(any(
feature = "backend-async-std",
feature = "backend-smol",
feature = "backend-tokio",
))]
mod async_traits {
#[cfg(feature = "backend-async-std")]
use async_std::{
fs::File, io::prelude::*, io::SeekFrom, task as runtime,
};
#[cfg(feature = "backend-smol")]
use smol::{self as runtime, fs::File, io::*};
#[cfg(feature = "backend-tokio")]
use tokio::{fs::File, io::*};
#[cfg(feature = "backend-tokio")]
use tokio_shim::RuntimeShim as runtime;

#[cfg(feature = "backend-tokio")]
mod tokio_shim {
pub struct RuntimeShim;

#[cfg(feature = "backend-tokio")]
impl RuntimeShim {
pub fn block_on<F: std::future::Future>(
future: F,
) -> F::Output {
tokio::runtime::Builder::new_current_thread()
.build()
.unwrap()
.block_on(future)
}
}
}

#[test]
fn read() {
runtime::block_on(async {
let mut file = File::open("Cargo.toml").await.unwrap();
let mut buf = [0; 5];
let _ = file.read(&mut buf).await.expect("read should succeed");
});
}

#[test]
fn seek() {
runtime::block_on(async {
let mut file = File::open("Cargo.toml").await.unwrap();
file.seek(SeekFrom::End(5))
.await
.expect("seek should succeed");
});
}

#[test]
fn write() {
let std_file = tempfile::tempfile().unwrap();
runtime::block_on(async move {
let mut file = File::from(std_file);
file.write_all(&[1, 2, 3])
.await
.expect("write should succeed");
});
}
}
}

0 comments on commit 463d532

Please sign in to comment.