dioxus/packages/core/tests/miri_stress.rs

350 lines
9 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(0));
_ = 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!()
2022-11-29 21:31:04 +00:00
// dom.hard_diff(ScopeId(0));
// dom.hard_diff(ScopeId(0));
// dom.hard_diff(ScopeId(0));
// dom.hard_diff(ScopeId(0));
// dom.hard_diff(ScopeId(0));
// dom.hard_diff(ScopeId(0));
// dom.hard_diff(ScopeId(0));
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-11-29 21:31:04 +00:00
// #[test]
// fn old_props_arent_stale() {
// fn app(cx: Scope) -> Element {
// dbg!("rendering parent");
// let cnt = cx.use_hook(|| 0);
// *cnt += 1;
// if *cnt == 1 {
// render!(div { Child { a: "abcdef".to_string() } })
// } else {
// render!(div { Child { a: "abcdef".to_string() } })
// }
// }
// #[derive(Props, PartialEq)]
// struct ChildProps {
// a: String,
// }
// fn Child(cx: Scope<ChildProps>) -> Element {
// dbg!("rendering child", &cx.props.a);
// render!(div { "child {cx.props.a}" })
// }
// let mut dom = new_dom(app, ());
// let _ = dom.rebuild();
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
// dom.work_with_deadline(|| false);
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
// dom.work_with_deadline(|| false);
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
// dom.work_with_deadline(|| false);
// dbg!("forcing update to child");
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(1)));
// dom.work_with_deadline(|| false);
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(1)));
// dom.work_with_deadline(|| false);
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(1)));
// dom.work_with_deadline(|| false);
// }
// #[test]
// fn basic() {
// fn app(cx: Scope) -> Element {
// render!(div {
// Child { a: "abcdef".to_string() }
// })
// }
// #[derive(Props, PartialEq)]
// struct ChildProps {
// a: String,
// }
// fn Child(cx: Scope<ChildProps>) -> Element {
// dbg!("rendering child", &cx.props.a);
// render!(div { "child {cx.props.a}" })
// }
// let mut dom = new_dom(app, ());
// let _ = dom.rebuild();
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
// dom.work_with_deadline(|| false);
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
// dom.work_with_deadline(|| false);
// }
// #[test]
// fn leak_thru_children() {
// fn app(cx: Scope) -> Element {
// cx.render(rsx! {
// Child {
// name: "asd".to_string(),
// }
// });
// cx.render(rsx! {
// div {}
// })
// }
// #[inline_props]
// fn Child(cx: Scope, name: String) -> Element {
// render!(div { "child {name}" })
// }
// let mut dom = new_dom(app, ());
// let _ = dom.rebuild();
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
// dom.work_with_deadline(|| false);
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
// dom.work_with_deadline(|| false);
// }
// #[test]
// fn test_pass_thru() {
// #[inline_props]
// fn NavContainer<'a>(cx: Scope, children: Element<'a>) -> Element {
// cx.render(rsx! {
// header {
// nav { children }
// }
// })
// }
// fn NavMenu(cx: Scope) -> Element {
// render!( NavBrand {}
// div {
// NavStart {}
// NavEnd {}
// }
// )
// }
// fn NavBrand(cx: Scope) -> Element {
// render!(div {})
// }
// fn NavStart(cx: Scope) -> Element {
// render!(div {})
// }
// fn NavEnd(cx: Scope) -> Element {
// render!(div {})
// }
// #[inline_props]
// fn MainContainer<'a>(
// cx: Scope,
// nav: Element<'a>,
// body: Element<'a>,
// footer: Element<'a>,
// ) -> Element {
// cx.render(rsx! {
// div {
// class: "columns is-mobile",
// div {
// class: "column is-full",
// nav,
// body,
// footer,
// }
// }
// })
// }
// fn app(cx: Scope) -> Element {
// let nav = cx.render(rsx! {
// NavContainer {
// NavMenu {}
// }
// });
// let body = cx.render(rsx! {
// div {}
// });
// let footer = cx.render(rsx! {
// div {}
// });
// cx.render(rsx! {
// MainContainer {
// nav: nav,
// body: body,
// footer: footer,
// }
// })
// }
// let mut dom = new_dom(app, ());
// let _ = dom.rebuild();
// for _ in 0..40 {
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
// dom.work_with_deadline(|| false);
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
// dom.work_with_deadline(|| false);
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
// dom.work_with_deadline(|| false);
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(1)));
// dom.work_with_deadline(|| false);
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(1)));
// dom.work_with_deadline(|| false);
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(1)));
// dom.work_with_deadline(|| false);
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(2)));
// dom.work_with_deadline(|| false);
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(2)));
// dom.work_with_deadline(|| false);
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(2)));
// dom.work_with_deadline(|| false);
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(3)));
// dom.work_with_deadline(|| false);
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(3)));
// dom.work_with_deadline(|| false);
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(3)));
// dom.work_with_deadline(|| false);
// }
// }