miri_stress.rs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. #![allow(non_snake_case)]
  2. use std::rc::Rc;
  3. use dioxus::prelude::*;
  4. /// This test checks that we should release all memory used by the virtualdom when it exits.
  5. ///
  6. /// When miri runs, it'll let us know if we leaked or aliased.
  7. #[test]
  8. fn test_memory_leak() {
  9. fn app(cx: Scope) -> Element {
  10. let val = cx.generation();
  11. cx.spawn(async {
  12. tokio::time::sleep(std::time::Duration::from_millis(100000)).await;
  13. });
  14. if val == 2 || val == 4 {
  15. return cx.render(rsx!(()));
  16. }
  17. let name = cx.use_hook(|| String::from("numbers: "));
  18. name.push_str("123 ");
  19. cx.render(rsx!(
  20. div { "Hello, world!" }
  21. Child {}
  22. Child {}
  23. Child {}
  24. Child {}
  25. Child {}
  26. Child {}
  27. BorrowedChild { name: name }
  28. BorrowedChild { name: name }
  29. BorrowedChild { name: name }
  30. BorrowedChild { name: name }
  31. BorrowedChild { name: name }
  32. ))
  33. }
  34. #[derive(Props)]
  35. struct BorrowedProps<'a> {
  36. name: &'a str,
  37. }
  38. fn BorrowedChild<'a>(cx: Scope<'a, BorrowedProps<'a>>) -> Element {
  39. cx.render(rsx! {
  40. div {
  41. "goodbye {cx.props.name}"
  42. Child {}
  43. Child {}
  44. }
  45. })
  46. }
  47. fn Child(cx: Scope) -> Element {
  48. render!(div { "goodbye world" })
  49. }
  50. let mut dom = VirtualDom::new(app);
  51. _ = dom.rebuild();
  52. for _ in 0..5 {
  53. dom.mark_dirty(ScopeId(0));
  54. _ = dom.render_immediate();
  55. }
  56. }
  57. #[test]
  58. fn memo_works_properly() {
  59. fn app(cx: Scope) -> Element {
  60. let val = cx.generation();
  61. if val == 2 || val == 4 {
  62. return cx.render(rsx!(()));
  63. }
  64. let name = cx.use_hook(|| String::from("asd"));
  65. cx.render(rsx!(
  66. div { "Hello, world! {name}" }
  67. Child { na: "asdfg".to_string() }
  68. ))
  69. }
  70. #[derive(PartialEq, Props)]
  71. struct ChildProps {
  72. na: String,
  73. }
  74. fn Child(cx: Scope<ChildProps>) -> Element {
  75. render!(div { "goodbye world" })
  76. }
  77. let mut dom = VirtualDom::new(app);
  78. dom.rebuild();
  79. todo!()
  80. // dom.hard_diff(ScopeId(0));
  81. // dom.hard_diff(ScopeId(0));
  82. // dom.hard_diff(ScopeId(0));
  83. // dom.hard_diff(ScopeId(0));
  84. // dom.hard_diff(ScopeId(0));
  85. // dom.hard_diff(ScopeId(0));
  86. // dom.hard_diff(ScopeId(0));
  87. }
  88. #[test]
  89. fn free_works_on_root_hooks() {
  90. /*
  91. On Drop, scopearena drops all the hook contents. and props
  92. */
  93. #[derive(PartialEq, Clone, Props)]
  94. struct AppProps {
  95. inner: Rc<String>,
  96. }
  97. fn app(cx: Scope<AppProps>) -> Element {
  98. let name: &AppProps = cx.use_hook(|| cx.props.clone());
  99. render!(child_component { inner: name.inner.clone() })
  100. }
  101. fn child_component(cx: Scope<AppProps>) -> Element {
  102. render!(div { "{cx.props.inner}" })
  103. }
  104. let ptr = Rc::new("asdasd".to_string());
  105. let mut dom = VirtualDom::new_with_props(app, AppProps { inner: ptr.clone() });
  106. let _ = dom.rebuild();
  107. // ptr gets cloned into props and then into the hook
  108. assert_eq!(Rc::strong_count(&ptr), 4);
  109. drop(dom);
  110. assert_eq!(Rc::strong_count(&ptr), 1);
  111. }
  112. // #[test]
  113. // fn old_props_arent_stale() {
  114. // fn app(cx: Scope) -> Element {
  115. // dbg!("rendering parent");
  116. // let cnt = cx.use_hook(|| 0);
  117. // *cnt += 1;
  118. // if *cnt == 1 {
  119. // render!(div { Child { a: "abcdef".to_string() } })
  120. // } else {
  121. // render!(div { Child { a: "abcdef".to_string() } })
  122. // }
  123. // }
  124. // #[derive(Props, PartialEq)]
  125. // struct ChildProps {
  126. // a: String,
  127. // }
  128. // fn Child(cx: Scope<ChildProps>) -> Element {
  129. // dbg!("rendering child", &cx.props.a);
  130. // render!(div { "child {cx.props.a}" })
  131. // }
  132. // let mut dom = new_dom(app, ());
  133. // let _ = dom.rebuild();
  134. // dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
  135. // dom.work_with_deadline(|| false);
  136. // dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
  137. // dom.work_with_deadline(|| false);
  138. // dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
  139. // dom.work_with_deadline(|| false);
  140. // dbg!("forcing update to child");
  141. // dom.handle_message(SchedulerMsg::Immediate(ScopeId(1)));
  142. // dom.work_with_deadline(|| false);
  143. // dom.handle_message(SchedulerMsg::Immediate(ScopeId(1)));
  144. // dom.work_with_deadline(|| false);
  145. // dom.handle_message(SchedulerMsg::Immediate(ScopeId(1)));
  146. // dom.work_with_deadline(|| false);
  147. // }
  148. // #[test]
  149. // fn basic() {
  150. // fn app(cx: Scope) -> Element {
  151. // render!(div {
  152. // Child { a: "abcdef".to_string() }
  153. // })
  154. // }
  155. // #[derive(Props, PartialEq)]
  156. // struct ChildProps {
  157. // a: String,
  158. // }
  159. // fn Child(cx: Scope<ChildProps>) -> Element {
  160. // dbg!("rendering child", &cx.props.a);
  161. // render!(div { "child {cx.props.a}" })
  162. // }
  163. // let mut dom = new_dom(app, ());
  164. // let _ = dom.rebuild();
  165. // dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
  166. // dom.work_with_deadline(|| false);
  167. // dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
  168. // dom.work_with_deadline(|| false);
  169. // }
  170. // #[test]
  171. // fn leak_thru_children() {
  172. // fn app(cx: Scope) -> Element {
  173. // cx.render(rsx! {
  174. // Child {
  175. // name: "asd".to_string(),
  176. // }
  177. // });
  178. // cx.render(rsx! {
  179. // div {}
  180. // })
  181. // }
  182. // #[inline_props]
  183. // fn Child(cx: Scope, name: String) -> Element {
  184. // render!(div { "child {name}" })
  185. // }
  186. // let mut dom = new_dom(app, ());
  187. // let _ = dom.rebuild();
  188. // dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
  189. // dom.work_with_deadline(|| false);
  190. // dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
  191. // dom.work_with_deadline(|| false);
  192. // }
  193. // #[test]
  194. // fn test_pass_thru() {
  195. // #[inline_props]
  196. // fn NavContainer<'a>(cx: Scope, children: Element<'a>) -> Element {
  197. // cx.render(rsx! {
  198. // header {
  199. // nav { children }
  200. // }
  201. // })
  202. // }
  203. // fn NavMenu(cx: Scope) -> Element {
  204. // render!( NavBrand {}
  205. // div {
  206. // NavStart {}
  207. // NavEnd {}
  208. // }
  209. // )
  210. // }
  211. // fn NavBrand(cx: Scope) -> Element {
  212. // render!(div {})
  213. // }
  214. // fn NavStart(cx: Scope) -> Element {
  215. // render!(div {})
  216. // }
  217. // fn NavEnd(cx: Scope) -> Element {
  218. // render!(div {})
  219. // }
  220. // #[inline_props]
  221. // fn MainContainer<'a>(
  222. // cx: Scope,
  223. // nav: Element<'a>,
  224. // body: Element<'a>,
  225. // footer: Element<'a>,
  226. // ) -> Element {
  227. // cx.render(rsx! {
  228. // div {
  229. // class: "columns is-mobile",
  230. // div {
  231. // class: "column is-full",
  232. // nav,
  233. // body,
  234. // footer,
  235. // }
  236. // }
  237. // })
  238. // }
  239. // fn app(cx: Scope) -> Element {
  240. // let nav = cx.render(rsx! {
  241. // NavContainer {
  242. // NavMenu {}
  243. // }
  244. // });
  245. // let body = cx.render(rsx! {
  246. // div {}
  247. // });
  248. // let footer = cx.render(rsx! {
  249. // div {}
  250. // });
  251. // cx.render(rsx! {
  252. // MainContainer {
  253. // nav: nav,
  254. // body: body,
  255. // footer: footer,
  256. // }
  257. // })
  258. // }
  259. // let mut dom = new_dom(app, ());
  260. // let _ = dom.rebuild();
  261. // for _ in 0..40 {
  262. // dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
  263. // dom.work_with_deadline(|| false);
  264. // dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
  265. // dom.work_with_deadline(|| false);
  266. // dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
  267. // dom.work_with_deadline(|| false);
  268. // dom.handle_message(SchedulerMsg::Immediate(ScopeId(1)));
  269. // dom.work_with_deadline(|| false);
  270. // dom.handle_message(SchedulerMsg::Immediate(ScopeId(1)));
  271. // dom.work_with_deadline(|| false);
  272. // dom.handle_message(SchedulerMsg::Immediate(ScopeId(1)));
  273. // dom.work_with_deadline(|| false);
  274. // dom.handle_message(SchedulerMsg::Immediate(ScopeId(2)));
  275. // dom.work_with_deadline(|| false);
  276. // dom.handle_message(SchedulerMsg::Immediate(ScopeId(2)));
  277. // dom.work_with_deadline(|| false);
  278. // dom.handle_message(SchedulerMsg::Immediate(ScopeId(2)));
  279. // dom.work_with_deadline(|| false);
  280. // dom.handle_message(SchedulerMsg::Immediate(ScopeId(3)));
  281. // dom.work_with_deadline(|| false);
  282. // dom.handle_message(SchedulerMsg::Immediate(ScopeId(3)));
  283. // dom.work_with_deadline(|| false);
  284. // dom.handle_message(SchedulerMsg::Immediate(ScopeId(3)));
  285. // dom.work_with_deadline(|| false);
  286. // }
  287. // }