1use crate::{
2 elements::Empty, executor, platform, Element, ElementBox, Entity, FontCache, Handle,
3 LeakDetector, MutableAppContext, Platform, RenderContext, Subscription, TestAppContext, View,
4};
5use futures::StreamExt;
6use parking_lot::Mutex;
7use smol::channel;
8use std::{
9 panic::{self, RefUnwindSafe},
10 rc::Rc,
11 sync::{
12 atomic::{AtomicU64, Ordering::SeqCst},
13 Arc,
14 },
15};
16
17#[cfg(test)]
18#[ctor::ctor]
19fn init_logger() {
20 if std::env::var("RUST_LOG").is_ok() {
21 env_logger::init();
22 }
23}
24
25// #[global_allocator]
26// static ALLOC: dhat::Alloc = dhat::Alloc;
27
28pub fn run_test(
29 mut num_iterations: u64,
30 mut starting_seed: u64,
31 max_retries: usize,
32 test_fn: &mut (dyn RefUnwindSafe
33 + Fn(
34 &mut MutableAppContext,
35 Rc<platform::test::ForegroundPlatform>,
36 Arc<executor::Deterministic>,
37 u64,
38 bool,
39 )),
40) {
41 // let _profiler = dhat::Profiler::new_heap();
42
43 let is_randomized = num_iterations > 1;
44 if is_randomized {
45 if let Ok(value) = std::env::var("SEED") {
46 starting_seed = value.parse().expect("invalid SEED variable");
47 }
48 if let Ok(value) = std::env::var("ITERATIONS") {
49 num_iterations = value.parse().expect("invalid ITERATIONS variable");
50 }
51 }
52
53 let atomic_seed = AtomicU64::new(starting_seed as u64);
54 let mut retries = 0;
55
56 loop {
57 let result = panic::catch_unwind(|| {
58 let foreground_platform = Rc::new(platform::test::foreground_platform());
59 let platform = Arc::new(platform::test::platform());
60 let font_system = platform.fonts();
61 let font_cache = Arc::new(FontCache::new(font_system));
62
63 loop {
64 let seed = atomic_seed.fetch_add(1, SeqCst);
65 let is_last_iteration = seed + 1 >= starting_seed + num_iterations;
66
67 if is_randomized {
68 dbg!(seed);
69 }
70
71 let deterministic = executor::Deterministic::new(seed);
72 let leak_detector = Arc::new(Mutex::new(LeakDetector::default()));
73 let mut cx = TestAppContext::new(
74 foreground_platform.clone(),
75 platform.clone(),
76 deterministic.build_foreground(usize::MAX),
77 deterministic.build_background(),
78 font_cache.clone(),
79 leak_detector.clone(),
80 0,
81 );
82 cx.update(|cx| {
83 test_fn(
84 cx,
85 foreground_platform.clone(),
86 deterministic.clone(),
87 seed,
88 is_last_iteration,
89 );
90 });
91
92 cx.update(|cx| cx.remove_all_windows());
93 deterministic.run_until_parked();
94 cx.update(|_| {}); // flush effects
95
96 leak_detector.lock().detect();
97 if is_last_iteration {
98 break;
99 }
100 }
101 });
102
103 match result {
104 Ok(_) => {
105 break;
106 }
107 Err(error) => {
108 if retries < max_retries {
109 retries += 1;
110 println!("retrying: attempt {}", retries);
111 } else {
112 if is_randomized {
113 eprintln!("failing seed: {}", atomic_seed.load(SeqCst) - 1);
114 }
115 panic::resume_unwind(error);
116 }
117 }
118 }
119 }
120}
121
122pub struct Observation<T> {
123 rx: channel::Receiver<T>,
124 _subscription: Subscription,
125}
126
127impl<T> futures::Stream for Observation<T> {
128 type Item = T;
129
130 fn poll_next(
131 mut self: std::pin::Pin<&mut Self>,
132 cx: &mut std::task::Context<'_>,
133 ) -> std::task::Poll<Option<Self::Item>> {
134 self.rx.poll_next_unpin(cx)
135 }
136}
137
138pub fn observe<T: Entity>(entity: &impl Handle<T>, cx: &mut TestAppContext) -> Observation<()> {
139 let (tx, rx) = smol::channel::unbounded();
140 let _subscription = cx.update(|cx| {
141 cx.observe(entity, move |_, _| {
142 let _ = smol::block_on(tx.send(()));
143 })
144 });
145
146 Observation { rx, _subscription }
147}
148
149pub fn subscribe<T: Entity>(
150 entity: &impl Handle<T>,
151 cx: &mut TestAppContext,
152) -> Observation<T::Event>
153where
154 T::Event: Clone,
155{
156 let (tx, rx) = smol::channel::unbounded();
157 let _subscription = cx.update(|cx| {
158 cx.subscribe(entity, move |_, event, _| {
159 let _ = smol::block_on(tx.send(event.clone()));
160 })
161 });
162
163 Observation { rx, _subscription }
164}
165
166pub struct EmptyView;
167
168impl Entity for EmptyView {
169 type Event = ();
170}
171
172impl View for EmptyView {
173 fn ui_name() -> &'static str {
174 "empty view"
175 }
176
177 fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
178 Element::boxed(Empty::new())
179 }
180}