Merge pull request #122 from zed-industries/deterministic-random-synchronous-tests

Max Brunsfeld created

Use deterministic executor in randomized synchronous tests

Change summary

gpui/src/app.rs        | 25 ++++---------------------
gpui_macros/src/lib.rs | 30 ++++++++++++++++++++----------
2 files changed, 24 insertions(+), 31 deletions(-)

Detailed changes

gpui/src/app.rs 🔗

@@ -118,26 +118,6 @@ pub struct TestAppContext {
 }
 
 impl App {
-    pub fn test<T, F: FnOnce(&mut MutableAppContext) -> T>(
-        foreground_platform: Rc<platform::test::ForegroundPlatform>,
-        platform: Arc<dyn Platform>,
-        font_cache: Arc<FontCache>,
-        f: F,
-    ) -> T {
-        let foreground = Rc::new(executor::Foreground::test());
-        let cx = Rc::new(RefCell::new(MutableAppContext::new(
-            foreground,
-            Arc::new(executor::Background::new()),
-            platform,
-            foreground_platform,
-            font_cache,
-            (),
-        )));
-        cx.borrow_mut().weak_self = Some(Rc::downgrade(&cx));
-        let mut cx = cx.borrow_mut();
-        f(&mut *cx)
-    }
-
     pub fn new(asset_source: impl AssetSource) -> Result<Self> {
         let platform = platform::current::platform();
         let foreground_platform = platform::current::foreground_platform();
@@ -723,7 +703,10 @@ impl MutableAppContext {
             if let Some(arg) = arg.downcast_ref() {
                 handler(arg, cx);
             } else {
-                log::error!("Could not downcast argument for global action {}", name_clone);
+                log::error!(
+                    "Could not downcast argument for global action {}",
+                    name_clone
+                );
             }
         });
 

gpui_macros/src/lib.rs 🔗

@@ -73,13 +73,15 @@ pub fn test(args: TokenStream, function: TokenStream) -> TokenStream {
                     match last_segment.map(|s| s.ident.to_string()).as_deref() {
                         Some("TestAppContext") => {
                             let first_entity_id = ix * 100_000;
-                            inner_fn_args.extend(quote!(#namespace::TestAppContext::new(
-                                foreground_platform.clone(),
-                                platform.clone(),
-                                foreground.clone(),
-                                background.clone(),
-                                font_cache.clone(),
-                                #first_entity_id),
+                            inner_fn_args.extend(quote!(
+                                #namespace::TestAppContext::new(
+                                    foreground_platform.clone(),
+                                    platform.clone(),
+                                    foreground.clone(),
+                                    background.clone(),
+                                    font_cache.clone(),
+                                    #first_entity_id,
+                                ),
                             ));
                         }
                         Some("StdRng") => {
@@ -222,9 +224,17 @@ pub fn test(args: TokenStream, function: TokenStream) -> TokenStream {
                                 dbg!(seed);
                             }
 
-                            #namespace::App::test(foreground_platform.clone(), platform.clone(), font_cache.clone(), |cx| {
-                                #inner_fn_name(#inner_fn_args);
-                            });
+                            let (foreground, background) = #namespace::executor::deterministic(seed);
+                            let mut cx = #namespace::TestAppContext::new(
+                                foreground_platform.clone(),
+                                platform.clone(),
+                                foreground.clone(),
+                                background.clone(),
+                                font_cache.clone(),
+                                0,
+                            );
+                            cx.update(|cx| #inner_fn_name(#inner_fn_args));
+
                             atomic_seed.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                         }
                     });