task.rs 1.3 KB

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