dioxus/packages/core/tests/task.rs

43 lines
1.1 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
2022-11-22 18:38:27 -08:00
use dioxus::prelude::*;
use std::time::Duration;
2022-11-06 01:48:34 -07:00
2022-11-23 23:15:01 -08:00
static mut POLL_COUNT: usize = 0;
2022-11-06 01:48:34 -07:00
#[tokio::test]
async fn it_works() {
let mut dom = VirtualDom::new(app);
2022-11-11 18:29:27 -08:00
let _ = dom.rebuild();
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
assert_eq!(unsafe { POLL_COUNT }, 135);
2022-11-06 01:48:34 -07:00
}
fn app(cx: Scope) -> Element {
2022-11-11 18:29:27 -08:00
cx.use_hook(|| {
cx.spawn(async {
for x in 0..10 {
2022-11-23 23:15:01 -08:00
tokio::time::sleep(Duration::from_micros(50)).await;
unsafe { POLL_COUNT += x }
2022-11-11 18:29:27 -08:00
}
});
cx.spawn(async {
for x in 0..10 {
2022-11-23 23:15:01 -08:00
tokio::time::sleep(Duration::from_micros(25)).await;
unsafe { POLL_COUNT += x * 2 }
2022-11-11 18:29:27 -08:00
}
});
2022-11-06 01:48:34 -07:00
});
2022-11-22 18:38:27 -08:00
cx.render(rsx!(()))
2022-11-06 01:48:34 -07:00
}