2022-11-19 15:24:28 -08:00
|
|
|
//! 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
|
|
|
|
2024-01-28 01:30:33 -08:00
|
|
|
use std::{sync::atomic::AtomicUsize, time::Duration};
|
|
|
|
|
|
|
|
use dioxus::prelude::*;
|
|
|
|
|
|
|
|
async fn run_vdom(app: fn() -> Element) {
|
|
|
|
let mut dom = VirtualDom::new(app);
|
|
|
|
|
|
|
|
dom.rebuild(&mut dioxus_core::NoOpMutations);
|
|
|
|
|
|
|
|
tokio::select! {
|
|
|
|
_ = dom.wait_for_work() => {}
|
|
|
|
_ = tokio::time::sleep(Duration::from_millis(500)) => {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-11-06 01:48:34 -07:00
|
|
|
#[tokio::test]
|
2024-02-05 12:24:50 -08:00
|
|
|
async fn running_async() {
|
2023-12-17 16:46:32 -06:00
|
|
|
static POLL_COUNT: AtomicUsize = AtomicUsize::new(0);
|
|
|
|
|
2024-01-11 10:48:04 -06:00
|
|
|
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-02-04 23:03:52 -08:00
|
|
|
rsx!({})
|
2023-12-17 16:46:32 -06:00
|
|
|
}
|
|
|
|
|
2024-01-28 01:30:33 -08:00
|
|
|
run_vdom(app).await;
|
|
|
|
|
|
|
|
// 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),
|
|
|
|
135
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Prove that yield_now doesn't cause a deadlock
|
|
|
|
#[tokio::test]
|
|
|
|
async fn yield_now_works() {
|
|
|
|
thread_local! {
|
|
|
|
static SEQUENCE: std::cell::RefCell<Vec<usize>> = std::cell::RefCell::new(Vec::new());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn app() -> Element {
|
|
|
|
// these two tasks should yield to eachother
|
|
|
|
use_hook(|| {
|
|
|
|
spawn(async move {
|
2024-01-30 17:33:14 -08:00
|
|
|
for _ in 0..10 {
|
2024-01-28 01:30:33 -08:00
|
|
|
tokio::task::yield_now().await;
|
|
|
|
SEQUENCE.with(|s| s.borrow_mut().push(1));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
use_hook(|| {
|
|
|
|
spawn(async move {
|
2024-01-30 17:33:14 -08:00
|
|
|
for _ in 0..10 {
|
2024-01-28 01:30:33 -08:00
|
|
|
tokio::task::yield_now().await;
|
|
|
|
SEQUENCE.with(|s| s.borrow_mut().push(2));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2024-02-04 23:03:52 -08:00
|
|
|
rsx!({})
|
2024-01-28 01:30:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
run_vdom(app).await;
|
|
|
|
|
|
|
|
SEQUENCE.with(|s| assert_eq!(s.borrow().len(), 20));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Ensure that calling wait_for_flush waits for dioxus to finish its syncrhonous work
|
|
|
|
#[tokio::test]
|
|
|
|
async fn flushing() {
|
|
|
|
thread_local! {
|
|
|
|
static SEQUENCE: std::cell::RefCell<Vec<usize>> = std::cell::RefCell::new(Vec::new());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn app() -> Element {
|
|
|
|
use_hook(|| {
|
|
|
|
spawn(async move {
|
2024-01-30 18:17:45 -08:00
|
|
|
for _ in 0..10 {
|
2024-01-28 01:30:33 -08:00
|
|
|
flush_sync().await;
|
|
|
|
SEQUENCE.with(|s| s.borrow_mut().push(1));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
use_hook(|| {
|
|
|
|
spawn(async move {
|
2024-01-30 18:17:45 -08:00
|
|
|
for _ in 0..10 {
|
2024-01-28 01:30:33 -08:00
|
|
|
flush_sync().await;
|
|
|
|
SEQUENCE.with(|s| s.borrow_mut().push(2));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2024-02-04 23:03:52 -08:00
|
|
|
rsx!({})
|
2024-01-28 01:30:33 -08: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
|
|
|
|
2024-01-28 01:30:33 -08:00
|
|
|
let fut = async {
|
|
|
|
// Trigger the flush by waiting for work
|
|
|
|
for _ in 0..40 {
|
|
|
|
tokio::select! {
|
|
|
|
_ = dom.wait_for_work() => {}
|
|
|
|
_ = tokio::time::sleep(Duration::from_millis(1)) => {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-11-11 18:29:27 -08:00
|
|
|
tokio::select! {
|
2024-01-28 01:30:33 -08:00
|
|
|
_ = fut => {}
|
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
|
|
|
|
2024-01-28 01:30:33 -08:00
|
|
|
SEQUENCE.with(|s| assert_eq!(s.borrow().len(), 20));
|
2022-11-06 01:48:34 -07:00
|
|
|
}
|