diff --git a/Cargo.lock b/Cargo.lock index b78d8c32b5d6da5cbd4066d036d44e030303e613..8e2210ed19505e6b9a10a1a9af94de2c7d87a4d3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14355,6 +14355,7 @@ version = "0.1.0" dependencies = [ "anyhow", "async-task", + "futures 0.3.31", "parking_lot", "rand 0.8.5", "rand_chacha 0.3.1", diff --git a/crates/scheduler/Cargo.toml b/crates/scheduler/Cargo.toml index 9d0fe4d1880d2bfb320d17a462408a5179eecf83..444b7998c2d1e5861413092f99d6b8859ddba5dc 100644 --- a/crates/scheduler/Cargo.toml +++ b/crates/scheduler/Cargo.toml @@ -9,6 +9,7 @@ path = "src/scheduler.rs" [dependencies] anyhow = "1.0" async-task = "4.5" +futures = "0.3" rand = { version = "0.8", features = ["small_rng"] } rand_chacha = "0.3" parking_lot = "0.12" diff --git a/crates/scheduler/src/scheduler.rs b/crates/scheduler/src/scheduler.rs index b6124951fce9a0b798da54a1d562248e7ffdfc16..d3c2abcd6ba2ce5e368f563cae083b5b5c61197b 100644 --- a/crates/scheduler/src/scheduler.rs +++ b/crates/scheduler/src/scheduler.rs @@ -9,6 +9,8 @@ use std::future::Future; use std::marker::PhantomData; use std::sync::Arc; +use futures::channel::oneshot; +use futures::executor; use std::thread::{self, ThreadId}; #[derive(Copy, Clone, PartialEq, Eq, Hash)] @@ -192,28 +194,22 @@ mod tests { fn test_background_task_with_foreground_wait() { let scheduler = Arc::new(TestScheduler::new(SchedulerConfig::default())); - let flag = Arc::new(AtomicBool::new(false)); - assert!(!flag.load(Ordering::SeqCst)); + // Create a oneshot channel to send data from background to foreground + let (tx, rx) = oneshot::channel(); - // Spawn background task + // Spawn background task that sends 42 let bg_executor = BackgroundExecutor::new(scheduler.clone()).unwrap(); - let _background_task = bg_executor.spawn({ - let flag = flag.clone(); - async move { - flag.store(true, Ordering::SeqCst); - } - }); - - // Spawn foreground task (nothing special, just demonstrates both types) - let fg_executor = ForegroundExecutor::new(scheduler.clone()).unwrap(); - let _fg_task = fg_executor.spawn(async move { - // Foreground-specific work if needed + let _background_task = bg_executor.spawn(async move { + tx.send(42).unwrap(); }); // Run all tasks scheduler.run(); - // Background task should have run and set the flag - assert!(flag.load(Ordering::SeqCst)); + // Block on receiving the value from the background task + let received = executor::block_on(rx).unwrap(); + + // Assert on the result + assert_eq!(received, 42); } }