dioxus/packages/core/tests/task.rs

47 lines
1.4 KiB
Rust
Raw Normal View History

//! Verify that tasks get polled by the virtualdom properly, and that we escape wait_for_work safely
2022-11-06 01:48:34 -07:00
#[cfg(not(miri))]
2022-11-06 01:48:34 -07:00
#[tokio::test]
async fn it_works() {
2023-12-17 16:46:32 -06:00
use dioxus::prelude::*;
use std::{sync::atomic::AtomicUsize, time::Duration};
static POLL_COUNT: AtomicUsize = AtomicUsize::new(0);
fn app() -> Element {
2024-01-15 09:05:46 -06:00
use_hook(|| {
2024-01-11 15:18:11 -06:00
spawn(async {
2023-12-17 16:46:32 -06:00
for x in 0..10 {
tokio::time::sleep(Duration::from_micros(50)).await;
POLL_COUNT.fetch_add(x, std::sync::atomic::Ordering::Relaxed);
}
});
2024-01-11 15:18:11 -06:00
spawn(async {
2023-12-17 16:46:32 -06:00
for x in 0..10 {
tokio::time::sleep(Duration::from_micros(25)).await;
POLL_COUNT.fetch_add(x * 2, std::sync::atomic::Ordering::Relaxed);
}
});
});
2024-01-16 13:18:46 -06:00
rsx!({ () })
2023-12-17 16:46:32 -06:00
}
2022-11-06 01:48:34 -07:00
let mut dom = VirtualDom::new(app);
2024-01-15 11:06:27 -06:00
dom.rebuild(&mut dioxus_core::NoOpMutations);
2022-11-06 01:48:34 -07:00
2022-11-11 18:29:27 -08:00
tokio::select! {
_ = dom.wait_for_work() => {}
2022-11-29 16:31:04 -05:00
_ = tokio::time::sleep(Duration::from_millis(500)) => {}
2022-11-11 18:29:27 -08:00
};
2022-11-23 23:15:01 -08:00
// By the time the tasks are finished, we should've accumulated ticks from two tasks
// Be warned that by setting the delay to too short, tokio might not schedule in the tasks
2023-07-19 12:26:32 -07:00
assert_eq!(
POLL_COUNT.fetch_add(0, std::sync::atomic::Ordering::Relaxed),
135
);
2022-11-06 01:48:34 -07:00
}