dioxus/packages/core/tests/miri_stress.rs

195 lines
4.8 KiB
Rust
Raw Normal View History

2022-02-11 02:00:15 +00:00
#![allow(non_snake_case)]
2021-12-19 04:03:59 +00:00
2022-11-29 21:31:04 +00:00
use std::rc::Rc;
2021-12-19 04:03:59 +00:00
use dioxus::prelude::*;
2021-12-19 04:03:59 +00:00
2022-11-29 21:31:04 +00:00
/// This test checks that we should release all memory used by the virtualdom when it exits.
///
/// When miri runs, it'll let us know if we leaked or aliased.
2021-12-19 04:03:59 +00:00
#[test]
fn test_memory_leak() {
fn app() -> Element {
2024-01-11 01:21:15 +00:00
let val = generation();
2021-12-19 04:03:59 +00:00
2022-12-14 00:04:41 +00:00
cx.spawn(async {});
2021-12-19 04:03:59 +00:00
2022-11-29 21:31:04 +00:00
if val == 2 || val == 4 {
2024-01-11 01:21:15 +00:00
return render!(());
2021-12-19 04:03:59 +00:00
}
2022-11-29 21:31:04 +00:00
let name = cx.use_hook(|| String::from("numbers: "));
name.push_str("123 ");
2021-12-19 04:03:59 +00:00
2024-01-11 01:21:15 +00:00
render!(
2021-12-19 04:03:59 +00:00
div { "Hello, world!" }
Child {}
Child {}
Child {}
Child {}
Child {}
Child {}
2022-11-29 21:31:04 +00:00
BorrowedChild { name: name }
BorrowedChild { name: name }
BorrowedChild { name: name }
BorrowedChild { name: name }
BorrowedChild { name: name }
2024-01-11 01:21:15 +00:00
)
2021-12-19 04:03:59 +00:00
}
2024-01-11 17:11:44 +00:00
#[derive(Props, Clone, PartialEq)]
struct BorrowedProps {
name: String,
2021-12-19 04:03:59 +00:00
}
2024-01-11 17:11:44 +00:00
fn BorrowedChild(cx: BorrowedProps) -> Element {
2024-01-11 01:21:15 +00:00
render! {
2022-11-29 21:31:04 +00:00
div {
2024-01-11 17:11:44 +00:00
"goodbye {cx.name}"
2022-11-29 21:31:04 +00:00
Child {}
Child {}
}
2024-01-11 01:21:15 +00:00
}
2021-12-19 04:03:59 +00:00
}
2024-01-11 17:11:44 +00:00
fn Child() -> Element {
2024-01-11 01:21:15 +00:00
render!( div { "goodbye world" } )
2021-12-21 03:33:13 +00:00
}
2022-11-27 07:06:04 +00:00
let mut dom = VirtualDom::new(app);
2021-12-19 04:03:59 +00:00
2024-01-11 01:21:15 +00:00
_ = dom.rebuild_to_vec(&mut dioxus_core::NoOpMutations);
2022-11-29 21:31:04 +00:00
for _ in 0..5 {
dom.mark_dirty(ScopeId::ROOT);
2024-01-11 01:21:15 +00:00
_ = dom.render_immediate_to_vec();
2022-11-29 21:31:04 +00:00
}
2021-12-19 04:03:59 +00:00
}
#[test]
fn memo_works_properly() {
fn app() -> Element {
2024-01-11 01:21:15 +00:00
let val = generation();
2021-12-19 04:03:59 +00:00
2022-11-29 21:31:04 +00:00
if val == 2 || val == 4 {
2024-01-11 01:21:15 +00:00
return render!(());
2021-12-19 04:03:59 +00:00
}
let name = cx.use_hook(|| String::from("asd"));
2021-12-19 04:03:59 +00:00
2024-01-11 01:21:15 +00:00
render!(
2022-01-03 06:12:39 +00:00
div { "Hello, world! {name}" }
Child { na: "asdfg".to_string() }
2024-01-11 01:21:15 +00:00
)
2021-12-19 04:03:59 +00:00
}
#[derive(PartialEq, Props)]
struct ChildProps {
na: String,
}
fn Child(cx: Scope<ChildProps>) -> Element {
2024-01-11 01:21:15 +00:00
render!( div { "goodbye world" } )
2021-12-19 04:03:59 +00:00
}
2022-11-29 21:31:04 +00:00
let mut dom = VirtualDom::new(app);
2021-12-21 03:33:13 +00:00
2024-01-11 01:21:15 +00:00
_ = dom.rebuild_to_vec(&mut dioxus_core::NoOpMutations);
2022-12-05 22:16:54 +00:00
// todo!()
// dom.hard_diff(ScopeId::ROOT);
// dom.hard_diff(ScopeId::ROOT);
// dom.hard_diff(ScopeId::ROOT);
// dom.hard_diff(ScopeId::ROOT);
// dom.hard_diff(ScopeId::ROOT);
// dom.hard_diff(ScopeId::ROOT);
// dom.hard_diff(ScopeId::ROOT);
2021-12-21 03:33:13 +00:00
}
#[test]
fn free_works_on_root_hooks() {
/*
2022-11-29 21:31:04 +00:00
On Drop, scopearena drops all the hook contents. and props
2021-12-21 03:33:13 +00:00
*/
2022-11-29 21:31:04 +00:00
#[derive(PartialEq, Clone, Props)]
struct AppProps {
inner: Rc<String>,
2021-12-21 03:33:13 +00:00
}
2022-11-29 21:31:04 +00:00
fn app(cx: Scope<AppProps>) -> Element {
2024-01-11 17:11:44 +00:00
let name: &AppProps = cx.use_hook(|| cx.clone());
2022-11-29 21:31:04 +00:00
render!(child_component { inner: name.inner.clone() })
2021-12-21 03:33:13 +00:00
}
2022-11-29 21:31:04 +00:00
fn child_component(cx: Scope<AppProps>) -> Element {
2024-01-11 17:11:44 +00:00
render!( div { "{cx.inner}" } )
2021-12-21 03:33:13 +00:00
}
2022-11-29 21:31:04 +00:00
let ptr = Rc::new("asdasd".to_string());
let mut dom = VirtualDom::new_with_props(app, AppProps { inner: ptr.clone() });
2024-01-11 01:21:15 +00:00
let _ = dom.rebuild_to_vec(&mut dioxus_core::NoOpMutations);
2021-12-21 03:33:13 +00:00
2022-11-29 21:31:04 +00:00
// ptr gets cloned into props and then into the hook
assert_eq!(Rc::strong_count(&ptr), 4);
2021-12-21 03:33:13 +00:00
2022-11-29 21:31:04 +00:00
drop(dom);
2021-12-21 03:33:13 +00:00
2022-11-29 21:31:04 +00:00
assert_eq!(Rc::strong_count(&ptr), 1);
2021-12-21 03:33:13 +00:00
}
2022-12-30 19:34:44 +00:00
#[test]
fn supports_async() {
use std::time::Duration;
use tokio::time::sleep;
fn app() -> Element {
2024-01-11 17:11:44 +00:00
let colors = use_signal(|| vec!["green", "blue", "red"]);
let padding = use_signal(|| 10);
2022-12-30 19:34:44 +00:00
use_effect(cx, colors, |colors| async move {
2022-12-30 19:34:44 +00:00
sleep(Duration::from_millis(1000)).await;
colors.with_mut(|colors| colors.reverse());
});
use_effect(cx, padding, |padding| async move {
2022-12-30 19:34:44 +00:00
sleep(Duration::from_millis(10)).await;
padding.with_mut(|padding| {
if *padding < 65 {
*padding += 1;
} else {
*padding = 5;
}
});
});
let big = colors[0];
let mid = colors[1];
let small = colors[2];
2024-01-11 01:21:15 +00:00
render! {
div { background: "{big}", height: "stretch", width: "stretch", padding: "50",
label { "hello" }
div { background: "{mid}", height: "auto", width: "stretch", padding: "{padding}",
label { "World" }
div { background: "{small}", height: "auto", width: "stretch", padding: "20", label { "ddddddd" } }
2022-12-30 19:34:44 +00:00
}
}
2024-01-11 01:21:15 +00:00
}
2022-12-30 19:34:44 +00:00
}
let rt = tokio::runtime::Builder::new_current_thread()
.enable_time()
.build()
.unwrap();
rt.block_on(async {
let mut dom = VirtualDom::new(app);
2024-01-11 01:21:15 +00:00
let _ = dom.rebuild_to_vec(&mut dioxus_core::NoOpMutations);
2022-12-30 19:34:44 +00:00
for _ in 0..10 {
dom.wait_for_work().await;
2024-01-11 01:21:15 +00:00
let _edits = dom.render_immediate_to_vec();
2022-12-30 19:34:44 +00:00
}
});
}