From 25236519a71f27e7e732cfd58ad7734c43773159 Mon Sep 17 00:00:00 2001 From: Kaede Hoshikawa Date: Mon, 22 Aug 2022 00:28:41 +0900 Subject: [PATCH] Add some tests. --- .../yew/src/platform/rt_tokio/local_worker.rs | 47 +++++++++++++++++ packages/yew/src/platform/rt_tokio/mod.rs | 50 +++++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/packages/yew/src/platform/rt_tokio/local_worker.rs b/packages/yew/src/platform/rt_tokio/local_worker.rs index 0737a4c9e3b..5103f57eb03 100644 --- a/packages/yew/src/platform/rt_tokio/local_worker.rs +++ b/packages/yew/src/platform/rt_tokio/local_worker.rs @@ -141,3 +141,50 @@ impl LocalHandle { }); } } + +#[cfg(test)] +mod tests { + use futures::channel::oneshot; + use tokio::test; + use yew::platform::Runtime; + + use super::*; + + #[test] + async fn test_local_handle_exists() { + assert!(LocalHandle::try_current().is_none()); + + let runtime = Runtime::default(); + let (tx, rx) = oneshot::channel(); + + runtime.spawn_pinned(move || async move { + tx.send(LocalHandle::try_current().is_some()) + .expect("failed to send"); + }); + + assert!(rx.await.expect("failed to receive")); + } + + #[test] + async fn test_local_handle_spawns_on_same_worker() { + assert!(LocalHandle::try_current().is_none()); + + let runtime = Runtime::default(); + let (tx1, rx1) = oneshot::channel(); + let (tx2, rx2) = oneshot::channel(); + + runtime.spawn_pinned(move || async move { + let handle = LocalHandle::current(); + + tx1.send(std::thread::current().id()) + .expect("failed to send"); + + handle.spawn_local(async move { + tx2.send(std::thread::current().id()) + .expect("failed to send"); + }) + }); + + assert_eq!(rx1.await, rx2.await); + } +} diff --git a/packages/yew/src/platform/rt_tokio/mod.rs b/packages/yew/src/platform/rt_tokio/mod.rs index 40a30b87eaa..77a2c23b56c 100644 --- a/packages/yew/src/platform/rt_tokio/mod.rs +++ b/packages/yew/src/platform/rt_tokio/mod.rs @@ -105,3 +105,53 @@ impl Runtime { worker.spawn_pinned(create_task); } } + +#[cfg(test)] +mod tests { + use futures::channel::oneshot; + use tokio::test; + + use super::*; + + #[test] + async fn test_spawn_pinned_least_busy() { + let runtime = Runtime::new(2).expect("failed to create runtime."); + + let (tx1, rx1) = oneshot::channel(); + let (tx2, rx2) = oneshot::channel(); + + runtime.spawn_pinned(move || async move { + tx1.send(std::thread::current().id()) + .expect("failed to send!"); + }); + + runtime.spawn_pinned(move || async move { + tx2.send(std::thread::current().id()) + .expect("failed to send!"); + }); + + // first task and second task are not on the same thread. + assert_ne!(rx1.await, rx2.await); + } + + #[test] + async fn test_spawn_local_within_send() { + let runtime = Runtime::new(1).expect("failed to create runtime."); + + let (tx, rx) = oneshot::channel(); + + runtime.spawn_pinned(move || async move { + tokio::task::spawn(async move { + // tokio::task::spawn_local cannot spawn within a Send task. + // + // yew::platform::spawn_local can spawn within a Send task as long as runnting under + // a Yew Runtime. + spawn_local(async move { + tx.send(()).expect("failed to send!"); + }) + }); + }); + + assert_eq!(rx.await, Ok(())); + } +}