1
0

task.rs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //! Verify that tasks get polled by the virtualdom properly, and that we escape wait_for_work safely
  2. #[cfg(not(miri))]
  3. #[tokio::test]
  4. async fn it_works() {
  5. use dioxus::prelude::*;
  6. use std::{sync::atomic::AtomicUsize, time::Duration};
  7. static POLL_COUNT: AtomicUsize = AtomicUsize::new(0);
  8. fn app(cx: Scope) -> Element {
  9. cx.use_hook(|| {
  10. cx.spawn(async {
  11. for x in 0..10 {
  12. tokio::time::sleep(Duration::from_micros(50)).await;
  13. POLL_COUNT.fetch_add(x, std::sync::atomic::Ordering::Relaxed);
  14. }
  15. });
  16. cx.spawn(async {
  17. for x in 0..10 {
  18. tokio::time::sleep(Duration::from_micros(25)).await;
  19. POLL_COUNT.fetch_add(x * 2, std::sync::atomic::Ordering::Relaxed);
  20. }
  21. });
  22. });
  23. cx.render(rsx!(()))
  24. }
  25. let mut dom = VirtualDom::new(app);
  26. let _ = dom.rebuild(&mut dioxus_core::NoOpMutations);
  27. tokio::select! {
  28. _ = dom.wait_for_work() => {}
  29. _ = tokio::time::sleep(Duration::from_millis(500)) => {}
  30. };
  31. // By the time the tasks are finished, we should've accumulated ticks from two tasks
  32. // Be warned that by setting the delay to too short, tokio might not schedule in the tasks
  33. assert_eq!(
  34. POLL_COUNT.fetch_add(0, std::sync::atomic::Ordering::Relaxed),
  35. 135
  36. );
  37. }