miri_stress.rs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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() -> Element {
  10. let val = generation();
  11. spawn(async {});
  12. if val == 2 || val == 4 {
  13. return render!({});
  14. }
  15. let mut name = use_hook(|| String::from("numbers: "));
  16. name.push_str("123 ");
  17. render!(
  18. div { "Hello, world!" }
  19. Child {}
  20. Child {}
  21. Child {}
  22. Child {}
  23. Child {}
  24. Child {}
  25. BorrowedChild { name: name.clone() }
  26. BorrowedChild { name: name.clone() }
  27. BorrowedChild { name: name.clone() }
  28. BorrowedChild { name: name.clone() }
  29. BorrowedChild { name: name.clone() }
  30. )
  31. }
  32. #[derive(Props, Clone, PartialEq)]
  33. struct BorrowedProps {
  34. name: String,
  35. }
  36. fn BorrowedChild(cx: BorrowedProps) -> Element {
  37. render! {
  38. div {
  39. "goodbye {cx.name}"
  40. Child {}
  41. Child {}
  42. }
  43. }
  44. }
  45. fn Child() -> Element {
  46. render!( div { "goodbye world" } )
  47. }
  48. let mut dom = VirtualDom::new(app);
  49. _ = dom.rebuild(&mut dioxus_core::NoOpMutations);
  50. for _ in 0..5 {
  51. dom.mark_dirty(ScopeId::ROOT);
  52. _ = dom.render_immediate_to_vec();
  53. }
  54. }
  55. #[test]
  56. fn memo_works_properly() {
  57. fn app() -> Element {
  58. let val = generation();
  59. if val == 2 || val == 4 {
  60. return None;
  61. }
  62. let name = use_hook(|| String::from("asd"));
  63. render!(
  64. div { "Hello, world! {name}" }
  65. Child { na: "asdfg".to_string() }
  66. )
  67. }
  68. #[derive(PartialEq, Clone, Props)]
  69. struct ChildProps {
  70. na: String,
  71. }
  72. fn Child(cx: ChildProps) -> Element {
  73. render!( div { "goodbye world" } )
  74. }
  75. let mut dom = VirtualDom::new(app);
  76. _ = dom.rebuild(&mut dioxus_core::NoOpMutations);
  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: AppProps) -> Element {
  96. let name: AppProps = use_hook(|| cx.clone());
  97. render!(child_component { inner: name.inner.clone() })
  98. }
  99. fn child_component(props: AppProps) -> Element {
  100. render!( div { "{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(&mut dioxus_core::NoOpMutations);
  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() -> Element {
  115. let colors = use_signal(|| vec!["green", "blue", "red"]);
  116. let padding = use_signal(|| 10);
  117. use_hook(|| {
  118. spawn(async move {
  119. sleep(Duration::from_millis(1000)).await;
  120. colors.with_mut(|colors| colors.reverse());
  121. })
  122. });
  123. use_hook(|| {
  124. spawn(async move {
  125. sleep(Duration::from_millis(10)).await;
  126. padding.with_mut(|padding| {
  127. if *padding < 65 {
  128. *padding += 1;
  129. } else {
  130. *padding = 5;
  131. }
  132. });
  133. })
  134. });
  135. let colors = colors();
  136. let big = colors[0];
  137. let mid = colors[1];
  138. let small = colors[2];
  139. render! {
  140. div { background: "{big}", height: "stretch", width: "stretch", padding: "50",
  141. label { "hello" }
  142. div { background: "{mid}", height: "auto", width: "stretch", padding: "{padding}",
  143. label { "World" }
  144. div { background: "{small}", height: "auto", width: "stretch", padding: "20", label { "ddddddd" } }
  145. }
  146. }
  147. }
  148. }
  149. let rt = tokio::runtime::Builder::new_current_thread()
  150. .enable_time()
  151. .build()
  152. .unwrap();
  153. rt.block_on(async {
  154. let mut dom = VirtualDom::new(app);
  155. let _ = dom.rebuild(&mut dioxus_core::NoOpMutations);
  156. for _ in 0..10 {
  157. dom.wait_for_work().await;
  158. let _edits = dom.render_immediate_to_vec();
  159. }
  160. });
  161. }