miri_stress.rs 9.0 KB

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