task.rs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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::time::Duration;
  4. static mut POLL_COUNT: usize = 0;
  5. #[tokio::test]
  6. async fn it_works() {
  7. let mut dom = VirtualDom::new(app);
  8. let _ = dom.rebuild();
  9. tokio::select! {
  10. _ = dom.wait_for_work() => {}
  11. _ = tokio::time::sleep(Duration::from_millis(500)) => {}
  12. };
  13. // By the time the tasks are finished, we should've accumulated ticks from two tasks
  14. // Be warned that by setting the delay to too short, tokio might not schedule in the tasks
  15. assert_eq!(unsafe { POLL_COUNT }, 135);
  16. }
  17. fn app(cx: Scope) -> Element {
  18. cx.use_hook(|| {
  19. cx.spawn(async {
  20. for x in 0..10 {
  21. tokio::time::sleep(Duration::from_micros(50)).await;
  22. unsafe { POLL_COUNT += x }
  23. }
  24. });
  25. cx.spawn(async {
  26. for x in 0..10 {
  27. tokio::time::sleep(Duration::from_micros(25)).await;
  28. unsafe { POLL_COUNT += x * 2 }
  29. }
  30. });
  31. });
  32. cx.render(rsx!(()))
  33. }