Use run_until_parked instead of blocked in tests

Conrad Irwin created

Change summary

crates/gpui2/src/executor.rs         | 17 +++++++++++++++--
crates/gpui2/src/test.rs             |  2 +-
crates/gpui2_macros/src/test.rs      |  2 +-
crates/project2/src/project_tests.rs | 12 ------------
4 files changed, 17 insertions(+), 16 deletions(-)

Detailed changes

crates/gpui2/src/executor.rs 🔗

@@ -86,7 +86,20 @@ impl BackgroundExecutor {
         Task::Spawned(task)
     }
 
+    #[cfg(any(test, feature = "test-support"))]
+    pub fn block_test<R>(&self, future: impl Future<Output = R>) -> R {
+        self.block_internal(false, future)
+    }
+
     pub fn block<R>(&self, future: impl Future<Output = R>) -> R {
+        self.block_internal(true, future)
+    }
+
+    pub(crate) fn block_internal<R>(
+        &self,
+        background_only: bool,
+        future: impl Future<Output = R>,
+    ) -> R {
         pin_mut!(future);
         let (parker, unparker) = parking::pair();
         let awoken = Arc::new(AtomicBool::new(false));
@@ -102,7 +115,7 @@ impl BackgroundExecutor {
             match future.as_mut().poll(&mut cx) {
                 Poll::Ready(result) => return result,
                 Poll::Pending => {
-                    if !self.dispatcher.poll(true) {
+                    if !self.dispatcher.poll(background_only) {
                         if awoken.swap(false, SeqCst) {
                             continue;
                         }
@@ -184,7 +197,7 @@ impl BackgroundExecutor {
 
     #[cfg(any(test, feature = "test-support"))]
     pub fn simulate_random_delay(&self) -> impl Future<Output = ()> {
-        self.spawn(self.dispatcher.as_test().unwrap().simulate_random_delay())
+        self.dispatcher.as_test().unwrap().simulate_random_delay()
     }
 
     #[cfg(any(test, feature = "test-support"))]

crates/gpui2/src/test.rs 🔗

@@ -28,7 +28,7 @@ pub fn run_test(
             }
             let result = panic::catch_unwind(|| {
                 let dispatcher = TestDispatcher::new(StdRng::seed_from_u64(seed));
-                test_fn(dispatcher, seed);
+                test_fn(dispatcher.clone(), seed);
             });
 
             match result {

crates/gpui2_macros/src/test.rs 🔗

@@ -136,7 +136,7 @@ pub fn test(args: TokenStream, function: TokenStream) -> TokenStream {
                     &mut |dispatcher, _seed| {
                         let executor = gpui2::BackgroundExecutor::new(std::sync::Arc::new(dispatcher.clone()));
                         #cx_vars
-                        executor.block(#inner_fn_name(#inner_fn_args));
+                        executor.block_test(#inner_fn_name(#inner_fn_args));
                         #cx_teardowns
                     },
                     #on_failure_fn_name,

crates/project2/src/project_tests.rs 🔗

@@ -2942,7 +2942,6 @@ async fn test_buffer_deduping(cx: &mut gpui2::TestAppContext) {
 #[gpui2::test]
 async fn test_buffer_is_dirty(cx: &mut gpui2::TestAppContext) {
     init_test(cx);
-    dbg!("GAH");
 
     let fs = FakeFs::new(cx.executor().clone());
     fs.insert_tree(
@@ -2954,7 +2953,6 @@ async fn test_buffer_is_dirty(cx: &mut gpui2::TestAppContext) {
         }),
     )
     .await;
-    dbg!("NOOP");
 
     let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await;
 
@@ -2964,8 +2962,6 @@ async fn test_buffer_is_dirty(cx: &mut gpui2::TestAppContext) {
         .unwrap();
     let events = Arc::new(Mutex::new(Vec::new()));
 
-    dbg!("BOOP");
-
     // initially, the buffer isn't dirty.
     buffer1.update(cx, |buffer, cx| {
         cx.subscribe(&buffer1, {
@@ -2982,7 +2978,6 @@ async fn test_buffer_is_dirty(cx: &mut gpui2::TestAppContext) {
 
         buffer.edit([(1..2, "")], None, cx);
     });
-    dbg!("ADSASD");
 
     // after the first edit, the buffer is dirty, and emits a dirtied event.
     buffer1.update(cx, |buffer, cx| {
@@ -3000,7 +2995,6 @@ async fn test_buffer_is_dirty(cx: &mut gpui2::TestAppContext) {
             cx,
         );
     });
-    dbg!("1111");
 
     // after saving, the buffer is not dirty, and emits a saved event.
     buffer1.update(cx, |buffer, cx| {
@@ -3012,8 +3006,6 @@ async fn test_buffer_is_dirty(cx: &mut gpui2::TestAppContext) {
         buffer.edit([(2..2, "D")], None, cx);
     });
 
-    dbg!("5555555");
-
     // after editing again, the buffer is dirty, and emits another dirty event.
     buffer1.update(cx, |buffer, cx| {
         assert!(buffer.text() == "aBDc");
@@ -3035,7 +3027,6 @@ async fn test_buffer_is_dirty(cx: &mut gpui2::TestAppContext) {
         assert!(!buffer.is_dirty());
     });
 
-    dbg!("666666");
     assert_eq!(
         *events.lock(),
         &[language2::Event::Edited, language2::Event::DirtyChanged]
@@ -3055,8 +3046,6 @@ async fn test_buffer_is_dirty(cx: &mut gpui2::TestAppContext) {
         .detach();
     });
 
-    dbg!("0000000");
-
     fs.remove_file("/dir/file2".as_ref(), Default::default())
         .await
         .unwrap();
@@ -3084,7 +3073,6 @@ async fn test_buffer_is_dirty(cx: &mut gpui2::TestAppContext) {
         .detach();
     });
 
-    dbg!(";;;;;;");
     buffer3.update(cx, |buffer, cx| {
         buffer.edit([(0..0, "x")], None, cx);
     });