miri_stress.rs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. cx.spawn(async {});
  12. if val == 2 || val == 4 {
  13. return render!(());
  14. }
  15. let name = cx.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 }
  26. BorrowedChild { name: name }
  27. BorrowedChild { name: name }
  28. BorrowedChild { name: name }
  29. BorrowedChild { name: name }
  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_to_vec(&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 render!(());
  61. }
  62. let name = cx.use_hook(|| String::from("asd"));
  63. render!(
  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_to_vec(&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: Scope<AppProps>) -> Element {
  96. let name: &AppProps = cx.use_hook(|| cx.clone());
  97. render!(child_component { inner: name.inner.clone() })
  98. }
  99. fn child_component(cx: Scope<AppProps>) -> Element {
  100. render!( div { "{cx.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_to_vec(&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_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. render! {
  135. div { background: "{big}", height: "stretch", width: "stretch", padding: "50",
  136. label { "hello" }
  137. div { background: "{mid}", height: "auto", width: "stretch", padding: "{padding}",
  138. label { "World" }
  139. div { background: "{small}", height: "auto", width: "stretch", padding: "20", label { "ddddddd" } }
  140. }
  141. }
  142. }
  143. }
  144. let rt = tokio::runtime::Builder::new_current_thread()
  145. .enable_time()
  146. .build()
  147. .unwrap();
  148. rt.block_on(async {
  149. let mut dom = VirtualDom::new(app);
  150. let _ = dom.rebuild_to_vec(&mut dioxus_core::NoOpMutations);
  151. for _ in 0..10 {
  152. dom.wait_for_work().await;
  153. let _edits = dom.render_immediate_to_vec();
  154. }
  155. });
  156. }