dioxus/packages/core/tests/miri_stress.rs

215 lines
5.2 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() {
2021-12-30 02:28:28 +00:00
fn app(cx: Scope) -> Element {
2022-11-29 21:31:04 +00:00
let val = cx.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 {
2022-11-27 07:06:04 +00:00
return cx.render(rsx!(()));
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
cx.render(rsx!(
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 }
2021-12-19 04:03:59 +00:00
))
}
#[derive(Props)]
struct BorrowedProps<'a> {
2022-11-29 21:31:04 +00:00
name: &'a str,
2021-12-19 04:03:59 +00:00
}
fn BorrowedChild<'a>(cx: Scope<'a, BorrowedProps<'a>>) -> Element {
2022-11-29 21:31:04 +00:00
cx.render(rsx! {
div {
"goodbye {cx.props.name}"
Child {}
Child {}
}
2021-12-19 04:03:59 +00:00
})
}
fn Child(cx: Scope) -> Element {
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
2022-11-29 21:31:04 +00:00
_ = dom.rebuild();
for _ in 0..5 {
dom.mark_dirty(ScopeId::ROOT);
2022-11-29 21:31:04 +00:00
_ = dom.render_immediate();
}
2021-12-19 04:03:59 +00:00
}
#[test]
fn memo_works_properly() {
2021-12-30 02:28:28 +00:00
fn app(cx: Scope) -> Element {
2022-11-29 21:31:04 +00:00
let val = cx.generation();
2021-12-19 04:03:59 +00:00
2022-11-29 21:31:04 +00:00
if val == 2 || val == 4 {
return cx.render(rsx!(()));
2021-12-19 04:03:59 +00:00
}
let name = cx.use_hook(|| String::from("asd"));
2021-12-19 04:03:59 +00:00
cx.render(rsx!(
2022-01-03 06:12:39 +00:00
div { "Hello, world! {name}" }
Child { na: "asdfg".to_string() }
2021-12-19 04:03:59 +00:00
))
}
#[derive(PartialEq, Props)]
struct ChildProps {
na: String,
}
fn Child(cx: Scope<ChildProps>) -> Element {
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
2022-12-05 21:03:52 +00:00
_ = dom.rebuild();
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 {
let name: &AppProps = cx.use_hook(|| cx.props.clone());
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 {
render!(div { "{cx.props.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() });
2021-12-21 03:33:13 +00:00
let _ = dom.rebuild();
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(cx: Scope) -> Element {
let colors = use_state(cx, || vec!["green", "blue", "red"]);
let padding = use_state(cx, || 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];
cx.render(rsx! {
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",
}
}
},
}
})
}
let rt = tokio::runtime::Builder::new_current_thread()
.enable_time()
.build()
.unwrap();
rt.block_on(async {
let mut dom = VirtualDom::new(app);
let _ = dom.rebuild();
for _ in 0..10 {
dom.wait_for_work().await;
let _edits = dom.render_immediate();
2022-12-30 19:34:44 +00:00
}
});
}