Close buffers in randomized integration test

Antonio Scandurra and Nathan Sobo created

Co-Authored-By: Nathan Sobo <nathan@zed.dev>

Change summary

crates/project/src/project.rs | 14 ++++----
crates/server/src/rpc.rs      | 53 ++++++++++++++++++++++++++----------
2 files changed, 45 insertions(+), 22 deletions(-)

Detailed changes

crates/project/src/project.rs 🔗

@@ -46,7 +46,7 @@ pub struct Project {
     collaborators: HashMap<PeerId, Collaborator>,
     subscriptions: Vec<client::Subscription>,
     language_servers_with_diagnostics_running: isize,
-    open_buffers: HashMap<usize, OpenBuffer>,
+    open_buffers: HashMap<u64, OpenBuffer>,
     opened_buffer: broadcast::Sender<()>,
     loading_buffers: HashMap<
         ProjectPath,
@@ -719,7 +719,7 @@ impl Project {
         cx: &mut ModelContext<Self>,
     ) -> Result<()> {
         match self.open_buffers.insert(
-            buffer.read(cx).remote_id() as usize,
+            buffer.read(cx).remote_id(),
             OpenBuffer::Loaded(buffer.downgrade()),
         ) {
             None => {}
@@ -2183,7 +2183,7 @@ impl Project {
     ) -> Result<()> {
         this.update(&mut cx, |this, cx| {
             let payload = envelope.payload.clone();
-            let buffer_id = payload.buffer_id as usize;
+            let buffer_id = payload.buffer_id;
             let ops = payload
                 .operations
                 .into_iter()
@@ -2213,7 +2213,7 @@ impl Project {
     ) -> Result<()> {
         this.update(&mut cx, |this, cx| {
             let payload = envelope.payload.clone();
-            let buffer_id = payload.buffer_id as usize;
+            let buffer_id = payload.buffer_id;
             let file = payload.file.ok_or_else(|| anyhow!("invalid file"))?;
             let worktree = this
                 .worktree_for_id(WorktreeId::from_proto(file.worktree_id), cx)
@@ -2601,7 +2601,7 @@ impl Project {
                     let buffer = loop {
                         let buffer = this.read_with(&cx, |this, cx| {
                             this.open_buffers
-                                .get(&(id as usize))
+                                .get(&id)
                                 .and_then(|buffer| buffer.upgrade(cx))
                         });
                         if let Some(buffer) = buffer {
@@ -2679,7 +2679,7 @@ impl Project {
         this.update(&mut cx, |this, cx| {
             let buffer = this
                 .open_buffers
-                .get(&(envelope.payload.buffer_id as usize))
+                .get(&envelope.payload.buffer_id)
                 .and_then(|buffer| buffer.upgrade(cx));
             if let Some(buffer) = buffer {
                 buffer.update(cx, |buffer, cx| {
@@ -2705,7 +2705,7 @@ impl Project {
         this.update(&mut cx, |this, cx| {
             let buffer = this
                 .open_buffers
-                .get(&(payload.buffer_id as usize))
+                .get(&payload.buffer_id)
                 .and_then(|buffer| buffer.upgrade(cx));
             if let Some(buffer) = buffer {
                 buffer.update(cx, |buffer, cx| {

crates/server/src/rpc.rs 🔗

@@ -4007,13 +4007,24 @@ mod tests {
                                 .clone()
                         };
 
-                        buffer.update(&mut cx, |buffer, cx| {
-                            log::info!(
-                                "Host: updating buffer {:?}",
-                                buffer.file().unwrap().full_path(cx)
-                            );
-                            buffer.randomly_edit(&mut *rng.borrow_mut(), 5, cx)
-                        });
+                        if rng.borrow_mut().gen_bool(0.1) {
+                            cx.update(|cx| {
+                                log::info!(
+                                    "Host: dropping buffer {:?}",
+                                    buffer.read(cx).file().unwrap().full_path(cx)
+                                );
+                                self.buffers.remove(&buffer);
+                                drop(buffer);
+                            });
+                        } else {
+                            buffer.update(&mut cx, |buffer, cx| {
+                                log::info!(
+                                    "Host: updating buffer {:?}",
+                                    buffer.file().unwrap().full_path(cx)
+                                );
+                                buffer.randomly_edit(&mut *rng.borrow_mut(), 5, cx)
+                            });
+                        }
                     }
                     _ => loop {
                         let path_component_count = rng.borrow_mut().gen_range(1..=5);
@@ -4093,14 +4104,26 @@ mod tests {
                         .clone()
                 };
 
-                buffer.update(&mut cx, |buffer, cx| {
-                    log::info!(
-                        "Guest {}: updating buffer {:?}",
-                        guest_id,
-                        buffer.file().unwrap().full_path(cx)
-                    );
-                    buffer.randomly_edit(&mut *rng.borrow_mut(), 5, cx)
-                });
+                if rng.borrow_mut().gen_bool(0.1) {
+                    cx.update(|cx| {
+                        log::info!(
+                            "Guest {}: dropping buffer {:?}",
+                            guest_id,
+                            buffer.read(cx).file().unwrap().full_path(cx)
+                        );
+                        self.buffers.remove(&buffer);
+                        drop(buffer);
+                    });
+                } else {
+                    buffer.update(&mut cx, |buffer, cx| {
+                        log::info!(
+                            "Guest {}: updating buffer {:?}",
+                            guest_id,
+                            buffer.file().unwrap().full_path(cx)
+                        );
+                        buffer.randomly_edit(&mut *rng.borrow_mut(), 5, cx)
+                    });
+                }
 
                 cx.background().simulate_random_delay().await;
             }