2022-02-11 02:00:15 +00:00
|
|
|
#![allow(non_snake_case)]
|
2021-12-19 04:03:59 +00:00
|
|
|
/*
|
|
|
|
Stress Miri as much as possible.
|
|
|
|
|
|
|
|
Prove that we don't leak memory and that our methods are safe.
|
|
|
|
|
|
|
|
Specifically:
|
|
|
|
- [ ] VirtualDom drops memory safely
|
|
|
|
- [ ] Borrowed components don't expose invalid pointers
|
|
|
|
- [ ] Async isn't busted
|
|
|
|
*/
|
|
|
|
|
2022-03-03 03:48:22 +00:00
|
|
|
use dioxus::prelude::*;
|
|
|
|
use dioxus_core::SchedulerMsg;
|
2021-12-19 04:03:59 +00:00
|
|
|
|
|
|
|
mod test_logging;
|
|
|
|
|
|
|
|
const IS_LOGGING_ENABLED: bool = false;
|
|
|
|
|
|
|
|
fn new_dom<P: 'static + Send>(app: Component<P>, props: P) -> VirtualDom {
|
|
|
|
test_logging::set_up_logging(IS_LOGGING_ENABLED);
|
|
|
|
VirtualDom::new_with_props(app, props)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This test ensures that if a component aborts early, it is replaced with a placeholder.
|
|
|
|
/// In debug, this should also toss a warning.
|
|
|
|
#[test]
|
|
|
|
fn test_memory_leak() {
|
2021-12-30 02:28:28 +00:00
|
|
|
fn app(cx: Scope) -> Element {
|
2022-01-02 07:15:04 +00:00
|
|
|
let val = cx.use_hook(|_| 0);
|
2021-12-19 04:03:59 +00:00
|
|
|
|
|
|
|
*val += 1;
|
|
|
|
|
|
|
|
if *val == 2 || *val == 4 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2022-01-02 07:15:04 +00:00
|
|
|
let name = cx.use_hook(|_| String::from("asd"));
|
2021-12-19 04:03:59 +00:00
|
|
|
|
|
|
|
cx.render(rsx!(
|
|
|
|
div { "Hello, world!" }
|
|
|
|
child()
|
|
|
|
child()
|
|
|
|
child()
|
|
|
|
child()
|
|
|
|
child()
|
|
|
|
child()
|
|
|
|
borrowed_child(na: name)
|
|
|
|
borrowed_child(na: name)
|
|
|
|
borrowed_child(na: name)
|
|
|
|
borrowed_child(na: name)
|
|
|
|
borrowed_child(na: name)
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Props)]
|
|
|
|
struct BorrowedProps<'a> {
|
|
|
|
na: &'a str,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn borrowed_child<'a>(cx: Scope<'a, BorrowedProps<'a>>) -> Element {
|
|
|
|
rsx!(cx, div {
|
|
|
|
"goodbye {cx.props.na}"
|
|
|
|
child()
|
|
|
|
child()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-30 02:28:28 +00:00
|
|
|
fn child(cx: Scope) -> Element {
|
2021-12-21 03:33:13 +00:00
|
|
|
rsx!(cx, div { "goodbye world" })
|
|
|
|
}
|
|
|
|
|
2021-12-19 04:03:59 +00:00
|
|
|
let mut dom = new_dom(app, ());
|
|
|
|
|
|
|
|
dom.rebuild();
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn memo_works_properly() {
|
2021-12-30 02:28:28 +00:00
|
|
|
fn app(cx: Scope) -> Element {
|
2022-01-02 07:15:04 +00:00
|
|
|
let val = cx.use_hook(|_| 0);
|
2021-12-19 04:03:59 +00:00
|
|
|
|
|
|
|
*val += 1;
|
|
|
|
|
|
|
|
if *val == 2 || *val == 4 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2022-01-02 07:15:04 +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}" }
|
2022-01-05 05:27:22 +00:00
|
|
|
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 {
|
2021-12-21 03:33:13 +00:00
|
|
|
rsx!(cx, div { "goodbye world" })
|
2021-12-19 04:03:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut dom = new_dom(app, ());
|
|
|
|
|
|
|
|
dom.rebuild();
|
|
|
|
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
|
|
|
dom.hard_diff(ScopeId(0));
|
|
|
|
dom.hard_diff(ScopeId(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn free_works_on_root_props() {
|
|
|
|
fn app(cx: Scope<Custom>) -> Element {
|
|
|
|
cx.render(rsx! {
|
|
|
|
child(a: "alpha")
|
|
|
|
child(a: "beta")
|
|
|
|
child(a: "gamma")
|
|
|
|
child(a: "delta")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Props, PartialEq)]
|
|
|
|
struct ChildProps {
|
|
|
|
a: &'static str,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn child(cx: Scope<ChildProps>) -> Element {
|
|
|
|
rsx!(cx, "child {cx.props.a}")
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Custom {
|
|
|
|
val: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Custom {
|
|
|
|
fn drop(&mut self) {
|
2022-01-03 06:12:39 +00:00
|
|
|
dbg!("dropped! {}", &self.val);
|
2021-12-21 03:33:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-31 07:40:12 +00:00
|
|
|
let mut dom = new_dom(app, Custom { val: String::from("asd") });
|
2021-12-21 03:33:13 +00:00
|
|
|
dom.rebuild();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn free_works_on_borrowed() {
|
2021-12-30 02:28:28 +00:00
|
|
|
fn app(cx: Scope) -> Element {
|
2021-12-21 03:33:13 +00:00
|
|
|
cx.render(rsx! {
|
|
|
|
child(a: "alpha", b: "asd".to_string())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
#[derive(Props)]
|
|
|
|
struct ChildProps<'a> {
|
|
|
|
a: &'a str,
|
|
|
|
b: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn child<'a>(cx: Scope<'a, ChildProps<'a>>) -> Element {
|
|
|
|
dbg!("rendering child");
|
|
|
|
rsx!(cx, "child {cx.props.a}, {cx.props.b}")
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for ChildProps<'_> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
dbg!("dropped child!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut dom = new_dom(app, ());
|
|
|
|
let _ = dom.rebuild();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn free_works_on_root_hooks() {
|
|
|
|
/*
|
|
|
|
On Drop, scopearena drops all the hook contents.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct Droppable<T>(T);
|
|
|
|
impl<T> Drop for Droppable<T> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
dbg!("dropping droppable");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-30 02:28:28 +00:00
|
|
|
fn app(cx: Scope) -> Element {
|
2022-01-02 07:15:04 +00:00
|
|
|
let name = cx.use_hook(|_| Droppable(String::from("asd")));
|
2021-12-21 03:33:13 +00:00
|
|
|
rsx!(cx, div { "{name.0}" })
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut dom = new_dom(app, ());
|
|
|
|
let _ = dom.rebuild();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn old_props_arent_stale() {
|
2021-12-30 02:28:28 +00:00
|
|
|
fn app(cx: Scope) -> Element {
|
2021-12-21 03:33:13 +00:00
|
|
|
dbg!("rendering parent");
|
2022-01-02 07:15:04 +00:00
|
|
|
let cnt = cx.use_hook(|_| 0);
|
2021-12-21 03:33:13 +00:00
|
|
|
*cnt += 1;
|
|
|
|
|
|
|
|
if *cnt == 1 {
|
|
|
|
rsx!(cx, div { child(a: "abcdef".to_string()) })
|
|
|
|
} else {
|
|
|
|
rsx!(cx, 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);
|
|
|
|
rsx!(cx, 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() {
|
2021-12-30 02:28:28 +00:00
|
|
|
fn app(cx: Scope) -> Element {
|
2021-12-21 03:33:13 +00:00
|
|
|
rsx!(cx, 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);
|
|
|
|
rsx!(cx, 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);
|
2021-12-19 04:03:59 +00:00
|
|
|
}
|
2022-01-05 05:27:22 +00:00
|
|
|
|
|
|
|
#[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 {
|
|
|
|
rsx!(cx, 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);
|
|
|
|
}
|
2022-01-29 02:43:01 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_pass_thru() {
|
2022-01-30 19:08:03 +00:00
|
|
|
#[inline_props]
|
|
|
|
fn NavContainer<'a>(cx: Scope, children: Element<'a>) -> Element {
|
|
|
|
cx.render(rsx! {
|
|
|
|
header {
|
2022-02-11 02:00:15 +00:00
|
|
|
nav { children }
|
2022-01-30 19:08:03 +00:00
|
|
|
}
|
|
|
|
})
|
2022-01-29 02:43:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-30 19:08:03 +00:00
|
|
|
fn NavMenu(cx: Scope) -> Element {
|
2022-01-29 02:43:01 +00:00
|
|
|
rsx!(cx,
|
2022-01-30 19:08:03 +00:00
|
|
|
NavBrand {}
|
|
|
|
div {
|
|
|
|
NavStart {}
|
|
|
|
NavEnd {}
|
2022-01-29 02:43:01 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-01-30 19:08:03 +00:00
|
|
|
fn NavBrand(cx: Scope) -> Element {
|
|
|
|
rsx!(cx, div {})
|
|
|
|
}
|
2022-01-29 02:43:01 +00:00
|
|
|
|
2022-01-30 19:08:03 +00:00
|
|
|
fn NavStart(cx: Scope) -> Element {
|
|
|
|
rsx!(cx, div {})
|
|
|
|
}
|
2022-01-29 02:43:01 +00:00
|
|
|
|
2022-01-30 19:08:03 +00:00
|
|
|
fn NavEnd(cx: Scope) -> Element {
|
|
|
|
rsx!(cx, div {})
|
|
|
|
}
|
2022-01-29 02:43:01 +00:00
|
|
|
|
2022-01-30 19:08:03 +00:00
|
|
|
#[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",
|
2022-02-11 02:00:15 +00:00
|
|
|
nav,
|
|
|
|
body,
|
|
|
|
footer,
|
2022-01-30 19:08:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-01-29 02:43:01 +00:00
|
|
|
|
2022-01-30 19:08:03 +00:00
|
|
|
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();
|
|
|
|
|
2022-02-11 02:00:15 +00:00
|
|
|
for _ in 0..40 {
|
2022-01-30 19:08:03 +00:00
|
|
|
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);
|
|
|
|
}
|
2022-01-29 02:43:01 +00:00
|
|
|
}
|