miri_stress.rs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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(_props: ChildProps) -> Element {
  74. rsx!( div { "goodbye world" } )
  75. }
  76. let mut dom = VirtualDom::new(app);
  77. dom.rebuild(&mut dioxus_core::NoOpMutations);
  78. }
  79. #[test]
  80. fn free_works_on_root_hooks() {
  81. /*
  82. On Drop, scopearena drops all the hook contents. and props
  83. */
  84. #[derive(PartialEq, Clone, Props)]
  85. struct AppProps {
  86. inner: Rc<String>,
  87. }
  88. fn app(cx: AppProps) -> Element {
  89. let name: AppProps = use_hook(|| cx.clone());
  90. rsx!(child_component { inner: name.inner.clone() })
  91. }
  92. fn child_component(props: AppProps) -> Element {
  93. rsx!( div { "{props.inner}" } )
  94. }
  95. let ptr = Rc::new("asdasd".to_string());
  96. let mut dom = VirtualDom::new_with_props(app, AppProps { inner: ptr.clone() });
  97. dom.rebuild(&mut dioxus_core::NoOpMutations);
  98. // ptr gets cloned into props and then into the hook
  99. assert_eq!(Rc::strong_count(&ptr), 5);
  100. drop(dom);
  101. assert_eq!(Rc::strong_count(&ptr), 1);
  102. }
  103. #[test]
  104. fn supports_async() {
  105. use std::time::Duration;
  106. use tokio::time::sleep;
  107. fn app() -> Element {
  108. let mut colors = use_signal(|| vec!["green", "blue", "red"]);
  109. let mut padding = use_signal(|| 10);
  110. use_hook(|| {
  111. spawn(async move {
  112. loop {
  113. sleep(Duration::from_millis(1000)).await;
  114. colors.with_mut(|colors| colors.reverse());
  115. }
  116. })
  117. });
  118. use_hook(|| {
  119. spawn(async move {
  120. loop {
  121. sleep(Duration::from_millis(10)).await;
  122. padding.with_mut(|padding| {
  123. if *padding < 65 {
  124. *padding += 1;
  125. } else {
  126. *padding = 5;
  127. }
  128. });
  129. }
  130. })
  131. });
  132. let colors = colors.read();
  133. let big = colors[0];
  134. let mid = colors[1];
  135. let small = colors[2];
  136. rsx! {
  137. div { background: "{big}", height: "stretch", width: "stretch", padding: "50",
  138. label { "hello" }
  139. div { background: "{mid}", height: "auto", width: "stretch", padding: "{padding}",
  140. label { "World" }
  141. div { background: "{small}", height: "auto", width: "stretch", padding: "20", label { "ddddddd" } }
  142. }
  143. }
  144. }
  145. }
  146. let rt = tokio::runtime::Builder::new_current_thread()
  147. .enable_time()
  148. .build()
  149. .unwrap();
  150. rt.block_on(async {
  151. let mut dom = VirtualDom::new(app);
  152. dom.rebuild(&mut dioxus_core::NoOpMutations);
  153. for _ in 0..10 {
  154. dom.wait_for_work().await;
  155. dom.render_immediate(&mut NoOpMutations);
  156. }
  157. });
  158. }