Browse Source

[git-packetline] Use maybe_async to deduplicate tests - neat

pull/86/head
Sebastian Thiel 1 month ago
parent
commit
439a7b76c3
No known key found for this signature in database GPG Key ID: 9CB5EE7895E8268B
  1. 164
      Cargo.lock
  2. 5
      git-packetline/Cargo.toml
  3. 12
      git-packetline/src/write/async_io.rs
  4. 12
      git-packetline/src/write/blocking_io.rs
  5. 14
      git-packetline/src/write/mod.rs
  6. 2
      git-packetline/tests/async-packetline.rs
  7. 2
      git-packetline/tests/blocking-packetline.rs
  8. 57
      git-packetline/tests/write/mod.rs

164
Cargo.lock

@ -62,6 +62,16 @@ version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a2f58b0bb10c380af2b26e57212856b8c9a59e0925b4c20f4a174a49734eaf7"
[[package]]
name = "async-attributes"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3203e79f4dd9bdda415ed03cf14dae5a2bf775c683a00f94e9cd1faf0f596e5"
dependencies = [
"quote",
"syn",
]
[[package]]
name = "async-channel"
version = "1.6.1"
@ -73,6 +83,36 @@ dependencies = [
"futures-core",
]
[[package]]
name = "async-executor"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "871f9bb5e0a22eeb7e8cf16641feb87c9dc67032ccf8ff49e772eb9941d3a965"
dependencies = [
"async-task",
"concurrent-queue",
"fastrand",
"futures-lite",
"once_cell",
"slab",
]
[[package]]
name = "async-global-executor"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9586ec52317f36de58453159d48351bc244bc24ced3effc1fce22f3d48664af6"
dependencies = [
"async-channel",
"async-executor",
"async-io",
"async-mutex",
"blocking",
"futures-lite",
"num_cpus",
"once_cell",
]
[[package]]
name = "async-io"
version = "1.4.1"
@ -93,6 +133,58 @@ dependencies = [
"winapi",
]
[[package]]
name = "async-lock"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6a8ea61bf9947a1007c5cada31e647dbc77b103c679858150003ba697ea798b"
dependencies = [
"event-listener",
]
[[package]]
name = "async-mutex"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "479db852db25d9dbf6204e6cb6253698f175c15726470f78af0d918e99d6156e"
dependencies = [
"event-listener",
]
[[package]]
name = "async-std"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9f06685bad74e0570f5213741bea82158279a4103d988e57bfada11ad230341"
dependencies = [
"async-attributes",
"async-channel",
"async-global-executor",
"async-io",
"async-lock",
"crossbeam-utils",
"futures-channel",
"futures-core",
"futures-io",
"futures-lite",
"gloo-timers",
"kv-log-macro",
"log",
"memchr",
"num_cpus",
"once_cell",
"pin-project-lite",
"pin-utils",
"slab",
"wasm-bindgen-futures",
]
[[package]]
name = "async-task"
version = "4.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e91831deabf0d6d7ec49552e489aed63b7456a7a3c46cff62adad428110b0af0"
[[package]]
name = "async-trait"
version = "0.1.50"
@ -104,6 +196,12 @@ dependencies = [
"syn",
]
[[package]]
name = "atomic-waker"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "065374052e7df7ee4047b1160cca5e1467a12351a40b3da123c870ba0b8eda2a"
[[package]]
name = "atty"
version = "0.2.14"
@ -160,6 +258,20 @@ dependencies = [
"generic-array",
]
[[package]]
name = "blocking"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c5e170dbede1f740736619b776d7251cb1b9095c435c34d8ca9f57fcd2f335e9"
dependencies = [
"async-channel",
"async-task",
"atomic-waker",
"fastrand",
"futures-lite",
"once_cell",
]
[[package]]
name = "bstr"
version = "0.2.16"
@ -883,11 +995,13 @@ dependencies = [
name = "git-packetline"
version = "0.6.0"
dependencies = [
"async-std",
"bstr",
"futures-io",
"futures-lite",
"git-odb",
"hex",
"maybe-async",
"pin-project-lite",
"quick-error",
"serde",
@ -1034,6 +1148,19 @@ dependencies = [
"serde_json",
]
[[package]]
name = "gloo-timers"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47204a46aaff920a1ea58b11d03dec6f704287d27561724a4631e450654a891f"
dependencies = [
"futures-channel",
"futures-core",
"js-sys",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "half"
version = "1.7.1"
@ -1183,6 +1310,15 @@ dependencies = [
"rayon",
]
[[package]]
name = "kv-log-macro"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0de8b303297635ad57c9f5059fd9cee7a47f8e8daa09df0fcd07dd39fb22977f"
dependencies = [
"log",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
@ -1252,6 +1388,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710"
dependencies = [
"cfg-if",
"value-bag",
]
[[package]]
@ -1510,6 +1647,12 @@ version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc0e1f259c92177c30a4c9d177246edd0a3568b25756a977d0632cf8fa37e905"
[[package]]
name = "pin-utils"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "pkg-config"
version = "0.3.19"
@ -2272,6 +2415,15 @@ dependencies = [
"cargo_toml",
]
[[package]]
name = "value-bag"
version = "1.0.0-alpha.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b676010e055c99033117c2343b33a40a30b91fecd6c49055ac9cd2d6c305ab1"
dependencies = [
"ctor",
]
[[package]]
name = "vcpkg"
version = "0.2.12"
@ -2338,6 +2490,18 @@ dependencies = [
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-futures"
version = "0.4.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "81b8b767af23de6ac18bf2168b690bed2902743ddf0fb39252e36f9e2bfc63ea"
dependencies = [
"cfg-if",
"js-sys",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.73"

5
git-packetline/Cargo.toml

@ -25,7 +25,7 @@ required-features = ["async-io"]
[[test]]
name = "blocking-packetline"
path = "tests/blocking-packetline.rs"
required-features = ["blocking-io"]
required-features = ["blocking-io", "maybe-async/is_sync"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@ -35,9 +35,12 @@ serde = { version = "1.0.114", optional = true, default-features = false, featur
quick-error = "2.0.0"
hex = "0.4.2"
bstr = { version = "0.2.13", default-features = false, features = ["std"] }
# async support
futures-io = { version = "0.3.15", optional = true }
futures-lite = { version = "1.11.3", optional = true }
pin-project-lite = { version = "0.2.6", optional = true }
[dev-dependencies]
async-std = { version = "1.9.0", features = ["attributes"] }
git-odb = { version = "^0.15.0", path = "../git-odb" }
maybe-async = "0.2.4"

12
git-packetline/src/write/async_io.rs

@ -37,7 +37,7 @@ impl<T: AsyncWrite + Unpin> Writer<T> {
}
/// Non-IO methods
impl<T: AsyncWrite + Unpin> Writer<T> {
impl<T> Writer<T> {
/// If called, each call to [`write()`][io::Write::write()] will write bytes as is.
pub fn enable_binary_mode(&mut self) {
self.inner.suffix = &[];
@ -47,16 +47,6 @@ impl<T: AsyncWrite + Unpin> Writer<T> {
pub fn enable_text_mode(&mut self) {
self.inner.suffix = &[b'\n'];
}
/// As [`enable_text_mode()`][Writer::enable_text_mode()], but suitable for chaining.
pub fn text_mode(mut self) -> Self {
self.enable_text_mode();
self
}
/// As [`enable_binary_mode()`][Writer::enable_binary_mode()], but suitable for chaining.
pub fn binary_mode(mut self) -> Self {
self.enable_binary_mode();
self
}
}
impl<T: AsyncWrite + Unpin> AsyncWrite for Writer<T> {

12
git-packetline/src/write/blocking_io.rs

@ -30,15 +30,9 @@ impl<T> Writer<T> {
pub fn enable_text_mode(&mut self) {
self.binary = false;
}
/// As [`enable_text_mode()`][Writer::enable_text_mode()], but suitable for chaining.
pub fn text_mode(mut self) -> Self {
self.binary = false;
self
}
/// As [`enable_binary_mode()`][Writer::enable_binary_mode()], but suitable for chaining.
pub fn binary_mode(mut self) -> Self {
self.binary = true;
self
/// Return the inner writer, consuming self.
pub fn into_inner(self) -> T {
self.inner
}
}

14
git-packetline/src/write/mod.rs

@ -7,3 +7,17 @@ pub use async_io::Writer;
mod blocking_io;
#[cfg(feature = "blocking-io")]
pub use blocking_io::Writer;
/// Common methods
impl<T> Writer<T> {
/// As [`enable_text_mode()`][Writer::enable_text_mode()], but suitable for chaining.
pub fn text_mode(mut self) -> Self {
self.enable_text_mode();
self
}
/// As [`enable_binary_mode()`][Writer::enable_binary_mode()], but suitable for chaining.
pub fn binary_mode(mut self) -> Self {
self.enable_binary_mode();
self
}
}

2
git-packetline/tests/async-packetline.rs

@ -12,3 +12,5 @@ pub fn assert_err_display<T: std::fmt::Debug, E: std::error::Error>(
#[cfg(all(feature = "async-io", not(feature = "blocking-io")))]
mod async_io;
#[cfg(all(feature = "async-io", not(feature = "blocking-io")))]
mod write;

2
git-packetline/tests/blocking-packetline.rs

@ -12,3 +12,5 @@ pub fn assert_err_display<T: std::fmt::Debug, E: std::error::Error>(
#[cfg(feature = "blocking-io")]
mod blocking;
#[cfg(feature = "blocking-io")]
mod write;

57
git-packetline/tests/write/mod.rs

@ -0,0 +1,57 @@
use bstr::ByteSlice;
#[cfg(all(feature = "async-io", not(feature = "blocking-io")))]
use futures_lite::prelude::*;
use git_packetline::Writer;
#[cfg(feature = "blocking-io")]
use std::io::Write;
const MAX_DATA_LEN: usize = 65516;
const MAX_LINE_LEN: usize = 4 + MAX_DATA_LEN;
#[maybe_async::test(feature = "blocking-io", async(feature = "async-io", async_std::test))]
async fn each_write_results_in_one_line() -> crate::Result {
let mut w = Writer::new(Vec::new());
w.write_all(b"hello").await?;
w.write(b"world!").await?;
let buf = w.into_inner();
assert_eq!(buf.as_bstr(), b"0009hello000aworld!".as_bstr());
Ok(())
}
#[maybe_async::test(feature = "blocking-io", async(feature = "async-io", async_std::test))]
async fn write_text_and_write_binary() -> crate::Result {
let buf = {
let mut w = Writer::new(Vec::new()).text_mode();
w.write_all(b"hello").await?;
w = w.binary_mode();
w.write(b"world").await?;
w.into_inner()
};
assert_eq!(buf.as_bstr(), b"000ahello\n0009world".as_bstr());
Ok(())
}
#[maybe_async::test(feature = "blocking-io", async(feature = "async-io", async_std::test))]
async fn huge_writes_are_split_into_lines() -> crate::Result {
let buf = {
let data = {
let mut v = Vec::new();
v.resize(MAX_DATA_LEN * 2, 0);
v
};
let mut w = Writer::new(Vec::new());
w.write(&data).await?;
w.into_inner()
};
assert_eq!(buf.len(), MAX_LINE_LEN * 2);
Ok(())
}
#[maybe_async::test(feature = "blocking-io", async(feature = "async-io", async_std::test))]
async fn empty_writes_fail_with_error() {
let res = Writer::new(Vec::new()).write(&[]).await;
assert_eq!(
res.unwrap_err().to_string(),
"empty packet lines are not permitted as '0004' is invalid"
);
}
Loading…
Cancel
Save