test child future drop behavior

This commit is contained in:
Evan Almloff 2024-03-04 15:26:16 -06:00
parent 797feb5657
commit fc7efd8006
2 changed files with 54 additions and 6 deletions

View file

@ -0,0 +1,51 @@
//! Verify that when children are dropped, they drop their futures before they are polled
use std::{sync::atomic::AtomicUsize, time::Duration};
use dioxus::prelude::*;
#[tokio::test]
async fn child_futures_drop_first() {
static POLL_COUNT: AtomicUsize = AtomicUsize::new(0);
fn app() -> Element {
if generation() == 0 {
rsx! {Child {}}
} else {
rsx! {}
}
}
fn Child() -> Element {
// Spawn a task that will increment POLL_COUNT every 10 milliseconds
// This should be dropped after the second time the parent is run
use_hook(|| {
spawn(async {
POLL_COUNT.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
});
});
rsx! {}
}
let mut dom = VirtualDom::new(app);
dom.rebuild(&mut dioxus_core::NoOpMutations);
// Here the parent and task could resolve at the same time, but because the task is in the child, dioxus should run the parent first because the child might be dropped
dom.mark_dirty(ScopeId::ROOT);
tokio::select! {
_ = dom.wait_for_work() => {}
_ = tokio::time::sleep(Duration::from_millis(500)) => panic!("timed out")
};
dom.render_immediate(&mut dioxus_core::NoOpMutations);
// 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!(
POLL_COUNT.fetch_add(0, std::sync::atomic::Ordering::Relaxed),
0
);
}

View file

@ -84,7 +84,7 @@ async fn yield_now_works() {
SEQUENCE.with(|s| assert_eq!(s.borrow().len(), 20));
}
/// Ensure that calling wait_for_flush waits for dioxus to finish its syncrhonous work
/// Ensure that calling wait_for_flush waits for dioxus to finish its synchronous work
#[tokio::test]
async fn flushing() {
thread_local! {
@ -97,11 +97,8 @@ async fn flushing() {
println!("App");
SEQUENCE.with(|s| s.borrow_mut().push(0));
}
use_hook(|| {
spawn(async move {
needs_update();
});
});
// The next two tasks mimic effects. They should only be run after the app has been rendered
use_hook(|| {
spawn(async move {
let mut channel = BROADCAST.with(|b| b.1.resubscribe());