浏览代码

test child future drop behavior

Evan Almloff 1 年之前
父节点
当前提交
fc7efd8006
共有 2 个文件被更改,包括 54 次插入6 次删除
  1. 51 0
      packages/core/tests/children_drop_futures.rs
  2. 3 6
      packages/core/tests/task.rs

+ 51 - 0
packages/core/tests/children_drop_futures.rs

@@ -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
+    );
+}

+ 3 - 6
packages/core/tests/task.rs

@@ -84,7 +84,7 @@ async fn yield_now_works() {
     SEQUENCE.with(|s| assert_eq!(s.borrow().len(), 20));
     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]
 #[tokio::test]
 async fn flushing() {
 async fn flushing() {
     thread_local! {
     thread_local! {
@@ -97,11 +97,8 @@ async fn flushing() {
             println!("App");
             println!("App");
             SEQUENCE.with(|s| s.borrow_mut().push(0));
             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(|| {
         use_hook(|| {
             spawn(async move {
             spawn(async move {
                 let mut channel = BROADCAST.with(|b| b.1.resubscribe());
                 let mut channel = BROADCAST.with(|b| b.1.resubscribe());