1
0

miri_stress.rs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #![allow(non_snake_case)]
  2. use std::rc::Rc;
  3. use dioxus::prelude::*;
  4. use dioxus_core::NoOpMutations;
  5. /// This test checks that we should release all memory used by the virtualdom when it exits.
  6. ///
  7. /// When miri runs, it'll let us know if we leaked or aliased.
  8. #[test]
  9. fn test_memory_leak() {
  10. fn app() -> Element {
  11. let val = generation();
  12. spawn(async {});
  13. if val == 2 || val == 4 {
  14. return rsx!({});
  15. }
  16. let mut name = use_hook(|| String::from("numbers: "));
  17. name.push_str("123 ");
  18. rsx!(
  19. div { "Hello, world!" }
  20. Child {}
  21. Child {}
  22. Child {}
  23. Child {}
  24. Child {}
  25. Child {}
  26. BorrowedChild { name: name.clone() }
  27. BorrowedChild { name: name.clone() }
  28. BorrowedChild { name: name.clone() }
  29. BorrowedChild { name: name.clone() }
  30. BorrowedChild { name: name.clone() }
  31. )
  32. }
  33. #[derive(Props, Clone, PartialEq)]
  34. struct BorrowedProps {
  35. name: String,
  36. }
  37. fn BorrowedChild(cx: BorrowedProps) -> Element {
  38. rsx! {
  39. div {
  40. "goodbye {cx.name}"
  41. Child {}
  42. Child {}
  43. }
  44. }
  45. }
  46. fn Child() -> Element {
  47. rsx!( div { "goodbye world" } )
  48. }
  49. let mut dom = VirtualDom::new(app);
  50. dom.rebuild(&mut dioxus_core::NoOpMutations);
  51. for _ in 0..5 {
  52. dom.mark_dirty(ScopeId::ROOT);
  53. _ = dom.render_immediate_to_vec();
  54. }
  55. }
  56. #[test]
  57. fn memo_works_properly() {
  58. fn app() -> Element {
  59. let val = generation();
  60. if val == 2 || val == 4 {
  61. return None;
  62. }
  63. let name = use_hook(|| String::from("asd"));
  64. rsx!(
  65. div { "Hello, world! {name}" }
  66. Child { na: "asdfg".to_string() }
  67. )
  68. }
  69. #[derive(PartialEq, Clone, Props)]
  70. struct ChildProps {
  71. na: String,
  72. }
  73. fn Child(cx: ChildProps) -> Element {
  74. rsx!( div { "goodbye world" } )
  75. }
  76. let mut dom = VirtualDom::new(app);
  77. dom.rebuild(&mut dioxus_core::NoOpMutations);
  78. // todo!()
  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. // dom.hard_diff(ScopeId::ROOT);
  86. }
  87. #[test]
  88. fn free_works_on_root_hooks() {
  89. /*
  90. On Drop, scopearena drops all the hook contents. and props
  91. */
  92. #[derive(PartialEq, Clone, Props)]
  93. struct AppProps {
  94. inner: Rc<String>,
  95. }
  96. fn app(cx: AppProps) -> Element {
  97. let name: AppProps = use_hook(|| cx.clone());
  98. rsx!(child_component { inner: name.inner.clone() })
  99. }
  100. fn child_component(props: AppProps) -> Element {
  101. rsx!( div { "{props.inner}" } )
  102. }
  103. let ptr = Rc::new("asdasd".to_string());
  104. let mut dom = VirtualDom::new_with_props(app, AppProps { inner: ptr.clone() });
  105. dom.rebuild(&mut dioxus_core::NoOpMutations);
  106. // ptr gets cloned into props and then into the hook
  107. assert_eq!(Rc::strong_count(&ptr), 5);
  108. drop(dom);
  109. assert_eq!(Rc::strong_count(&ptr), 1);
  110. }
  111. #[test]
  112. fn supports_async() {
  113. use std::time::Duration;
  114. use tokio::time::sleep;
  115. fn app() -> Element {
  116. let mut colors = use_signal(|| vec!["green", "blue", "red"]);
  117. let mut padding = use_signal(|| 10);
  118. use_hook(|| {
  119. spawn(async move {
  120. loop {
  121. sleep(Duration::from_millis(1000)).await;
  122. colors.with_mut(|colors| colors.reverse());
  123. }
  124. })
  125. });
  126. use_hook(|| {
  127. spawn(async move {
  128. loop {
  129. sleep(Duration::from_millis(10)).await;
  130. padding.with_mut(|padding| {
  131. if *padding < 65 {
  132. *padding += 1;
  133. } else {
  134. *padding = 5;
  135. }
  136. });
  137. }
  138. })
  139. });
  140. let colors = colors.read();
  141. let big = colors[0];
  142. let mid = colors[1];
  143. let small = colors[2];
  144. rsx! {
  145. div { background: "{big}", height: "stretch", width: "stretch", padding: "50",
  146. label { "hello" }
  147. div { background: "{mid}", height: "auto", width: "stretch", padding: "{padding}",
  148. label { "World" }
  149. div { background: "{small}", height: "auto", width: "stretch", padding: "20", label { "ddddddd" } }
  150. }
  151. }
  152. }
  153. }
  154. let rt = tokio::runtime::Builder::new_current_thread()
  155. .enable_time()
  156. .build()
  157. .unwrap();
  158. rt.block_on(async {
  159. let mut dom = VirtualDom::new(app);
  160. dom.rebuild(&mut dioxus_core::NoOpMutations);
  161. for _ in 0..10 {
  162. dom.wait_for_work().await;
  163. dom.render_immediate(&mut NoOpMutations);
  164. }
  165. });
  166. }