miri_stress.rs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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::ROOT);
  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::ROOT);
  79. // dom.hard_diff(ScopeId::ROOT);
  80. // dom.hard_diff(ScopeId::ROOT);
  81. // dom.hard_diff(ScopeId::ROOT);
  82. // dom.hard_diff(ScopeId::ROOT);
  83. // dom.hard_diff(ScopeId::ROOT);
  84. // dom.hard_diff(ScopeId::ROOT);
  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 supports_async() {
  112. use std::time::Duration;
  113. use tokio::time::sleep;
  114. fn app(cx: Scope) -> Element {
  115. let colors = use_state(cx, || vec!["green", "blue", "red"]);
  116. let padding = use_state(cx, || 10);
  117. use_effect(cx, colors, |colors| async move {
  118. sleep(Duration::from_millis(1000)).await;
  119. colors.with_mut(|colors| colors.reverse());
  120. });
  121. use_effect(cx, padding, |padding| async move {
  122. sleep(Duration::from_millis(10)).await;
  123. padding.with_mut(|padding| {
  124. if *padding < 65 {
  125. *padding += 1;
  126. } else {
  127. *padding = 5;
  128. }
  129. });
  130. });
  131. let big = colors[0];
  132. let mid = colors[1];
  133. let small = colors[2];
  134. cx.render(rsx! {
  135. div {
  136. background: "{big}",
  137. height: "stretch",
  138. width: "stretch",
  139. padding: "50",
  140. label {
  141. "hello",
  142. }
  143. div {
  144. background: "{mid}",
  145. height: "auto",
  146. width: "stretch",
  147. padding: "{padding}",
  148. label {
  149. "World",
  150. }
  151. div {
  152. background: "{small}",
  153. height: "auto",
  154. width: "stretch",
  155. padding: "20",
  156. label {
  157. "ddddddd",
  158. }
  159. }
  160. },
  161. }
  162. })
  163. }
  164. let rt = tokio::runtime::Builder::new_current_thread()
  165. .enable_time()
  166. .build()
  167. .unwrap();
  168. rt.block_on(async {
  169. let mut dom = VirtualDom::new(app);
  170. let _ = dom.rebuild();
  171. for _ in 0..10 {
  172. dom.wait_for_work().await;
  173. let _edits = dom.render_immediate();
  174. }
  175. });
  176. }