From 7557af45681900b4da37bdce7f22c57945c41f34 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sat, 27 Jul 2024 02:05:17 +0200 Subject: [PATCH] Make eyeball tests independent of tokio --- eyeball/Cargo.toml | 2 ++ eyeball/tests/it/main.rs | 15 +++++++++++++++ eyeball/tests/it/shared.rs | 33 +++++++++++++++------------------ eyeball/tests/it/unique.rs | 29 ++++++++++++++++------------- 4 files changed, 48 insertions(+), 31 deletions(-) diff --git a/eyeball/Cargo.toml b/eyeball/Cargo.toml index 0b609c9..40b9337 100644 --- a/eyeball/Cargo.toml +++ b/eyeball/Cargo.toml @@ -25,7 +25,9 @@ tokio-util = { version = "0.7.8", optional = true } divan = { version = "0.1.14", optional = true } [dev-dependencies] +futures-executor = "0.3.30" futures-util.workspace = true +macro_rules_attribute = "0.2.0" stream_assert.workspace = true tokio = { workspace = true, features = ["macros", "rt"] } diff --git a/eyeball/tests/it/main.rs b/eyeball/tests/it/main.rs index 9a1fcbe..101aaa8 100644 --- a/eyeball/tests/it/main.rs +++ b/eyeball/tests/it/main.rs @@ -1,3 +1,18 @@ +macro_rules! test { + ( + $(#[$post_attr:meta])* + async fn $name:ident() $(-> $ret:ty)? $bl:block + ) => { + $(#[$post_attr])* + #[core::prelude::v1::test] + fn $name () $(-> $ret)? { + futures_executor::block_on(async { + $bl + }) + } + }; +} + #[cfg(feature = "async-lock")] mod async_lock; mod shared; diff --git a/eyeball/tests/it/shared.rs b/eyeball/tests/it/shared.rs index 894328d..c232848 100644 --- a/eyeball/tests/it/shared.rs +++ b/eyeball/tests/it/shared.rs @@ -1,7 +1,8 @@ use eyeball::SharedObservable; -use tokio::task::JoinSet; +use futures_util::future::join; +use macro_rules_attribute::apply; -#[tokio::test] +#[apply(test!)] async fn lag() { let ob = SharedObservable::new("hello, world!".to_owned()); let mut rx1 = ob.subscribe(); @@ -15,35 +16,31 @@ async fn lag() { assert_eq!(rx2.next().await, Some("B".to_owned())); } -#[tokio::test] -#[cfg_attr(miri, ignore)] +#[apply(test!)] async fn separate_tasks() { let ob = SharedObservable::new(Box::new([0; 256])); - let mut subscriber = ob.subscribe(); - let handle = tokio::spawn(async move { + + let recv_fut = async { let mut value = subscriber.next().await.unwrap(); while let Some(update) = subscriber.next().await { value = update; } assert_eq!(value, Box::new([32; 256])); assert_eq!(subscriber.next().await, None); - }); - - let mut join_set = JoinSet::new(); - for i in 1..=32 { - let ob = ob.clone(); - join_set.spawn(async move { + }; + let set_fut = async { + for i in 1..=32 { ob.set(Box::new([i; 256])); - }); - tokio::task::yield_now().await; - } - drop(ob); + tokio::task::yield_now().await; + } + drop(ob); + }; - handle.await.unwrap(); + join(recv_fut, set_fut).await; } -#[tokio::test] +#[apply(test!)] async fn lag_no_clone() { // no Clone impl struct Foo(String); diff --git a/eyeball/tests/it/unique.rs b/eyeball/tests/it/unique.rs index 17b34aa..937a128 100644 --- a/eyeball/tests/it/unique.rs +++ b/eyeball/tests/it/unique.rs @@ -1,6 +1,8 @@ use eyeball::Observable; +use futures_util::future::join; +use macro_rules_attribute::apply; -#[tokio::test] +#[apply(test!)] async fn lag() { let mut ob = Observable::new("hello, world!".to_owned()); let mut rx1 = Observable::subscribe(&ob); @@ -14,30 +16,31 @@ async fn lag() { assert_eq!(rx2.next().await, Some("B".to_owned())); } -#[tokio::test] +#[apply(test!)] async fn separate_tasks() { let mut ob = Observable::new(Box::new([0; 256])); - let mut subscriber = Observable::subscribe(&ob); - let handle = tokio::spawn(async move { + + let recv_fut = async { let mut value = subscriber.next().await.unwrap(); while let Some(update) = subscriber.next().await { value = update; } assert_eq!(value, Box::new([32; 256])); assert_eq!(subscriber.next().await, None); - }); - - for i in 1..=32 { - Observable::set(&mut ob, Box::new([i; 256])); - tokio::task::yield_now().await; - } - drop(ob); + }; + let set_fut = async { + for i in 1..=32 { + Observable::set(&mut ob, Box::new([i; 256])); + tokio::task::yield_now().await; + } + drop(ob); + }; - handle.await.unwrap(); + join(recv_fut, set_fut).await; } -#[tokio::test] +#[apply(test!)] async fn lag_no_clone() { // no Clone impl struct Foo(String);