Make host reconnection test pass when mutating worktree while offline

This commit is contained in:
Antonio Scandurra 2022-12-20 17:30:58 +01:00
parent 1a3940a12e
commit 52babc51a0
7 changed files with 319 additions and 197 deletions

View file

@ -1081,6 +1081,17 @@ impl Project {
cx: &mut ModelContext<Self>,
) -> Result<()> {
self.set_collaborators_from_proto(message.collaborators, cx)?;
for worktree in self.worktrees.iter() {
if let Some(worktree) = worktree.upgrade(&cx) {
worktree.update(cx, |worktree, _| {
if let Some(worktree) = worktree.as_local_mut() {
worktree.reshare()
} else {
Ok(())
}
})?;
}
}
Ok(())
}

View file

@ -166,6 +166,7 @@ enum ScanState {
struct ShareState {
project_id: u64,
snapshots_tx: watch::Sender<LocalSnapshot>,
reshared: watch::Sender<()>,
_maintain_remote_snapshot: Task<Option<()>>,
}
@ -967,9 +968,11 @@ impl LocalWorktree {
let (share_tx, share_rx) = oneshot::channel();
if self.share.is_some() {
let _ = share_tx.send(Ok(()));
let _ = share_tx.send(());
} else {
let (snapshots_tx, mut snapshots_rx) = watch::channel_with(self.snapshot());
let (reshared_tx, mut reshared_rx) = watch::channel();
let _ = reshared_rx.try_recv();
let worktree_id = cx.model_id() as u64;
for (path, summary) in self.diagnostic_summaries.iter() {
@ -982,47 +985,48 @@ impl LocalWorktree {
}
}
let maintain_remote_snapshot = cx.background().spawn({
let rpc = self.client.clone();
let _maintain_remote_snapshot = cx.background().spawn({
let client = self.client.clone();
async move {
let mut prev_snapshot = match snapshots_rx.recv().await {
Some(snapshot) => {
let update = proto::UpdateWorktree {
project_id,
worktree_id,
abs_path: snapshot.abs_path().to_string_lossy().into(),
root_name: snapshot.root_name().to_string(),
updated_entries: snapshot
.entries_by_path
.iter()
.map(Into::into)
.collect(),
removed_entries: Default::default(),
scan_id: snapshot.scan_id as u64,
is_last_update: true,
};
if let Err(error) = send_worktree_update(&rpc, update).await {
let _ = share_tx.send(Err(error));
return Err(anyhow!("failed to send initial update worktree"));
} else {
let _ = share_tx.send(Ok(()));
snapshot
let mut share_tx = Some(share_tx);
let mut prev_snapshot = LocalSnapshot {
ignores_by_parent_abs_path: Default::default(),
git_repositories: Default::default(),
removed_entry_ids: Default::default(),
next_entry_id: Default::default(),
snapshot: Snapshot {
id: WorktreeId(worktree_id as usize),
abs_path: Path::new("").into(),
root_name: Default::default(),
root_char_bag: Default::default(),
entries_by_path: Default::default(),
entries_by_id: Default::default(),
scan_id: 0,
is_complete: true,
},
};
while let Some(snapshot) = snapshots_rx.recv().await {
#[cfg(any(test, feature = "test-support"))]
const MAX_CHUNK_SIZE: usize = 2;
#[cfg(not(any(test, feature = "test-support")))]
const MAX_CHUNK_SIZE: usize = 256;
let update =
snapshot.build_update(&prev_snapshot, project_id, worktree_id, true);
for update in proto::split_worktree_update(update, MAX_CHUNK_SIZE) {
while let Err(error) = client.request(update.clone()).await {
log::error!("failed to send worktree update: {}", error);
log::info!("waiting for worktree to be reshared");
if reshared_rx.next().await.is_none() {
return Ok(());
}
}
}
None => {
share_tx
.send(Err(anyhow!("worktree dropped before share completed")))
.ok();
return Err(anyhow!("failed to send initial update worktree"));
}
};
while let Some(snapshot) = snapshots_rx.recv().await {
send_worktree_update(
&rpc,
snapshot.build_update(&prev_snapshot, project_id, worktree_id, true),
)
.await?;
if let Some(share_tx) = share_tx.take() {
let _ = share_tx.send(());
}
prev_snapshot = snapshot;
}
@ -1034,21 +1038,28 @@ impl LocalWorktree {
self.share = Some(ShareState {
project_id,
snapshots_tx,
_maintain_remote_snapshot: maintain_remote_snapshot,
reshared: reshared_tx,
_maintain_remote_snapshot,
});
}
cx.foreground().spawn(async move {
share_rx
.await
.unwrap_or_else(|_| Err(anyhow!("share ended")))
})
cx.foreground()
.spawn(async move { share_rx.await.map_err(|_| anyhow!("share ended")) })
}
pub fn unshare(&mut self) {
self.share.take();
}
pub fn reshare(&mut self) -> Result<()> {
let share = self
.share
.as_mut()
.ok_or_else(|| anyhow!("can't reshare a worktree that wasn't shared"))?;
*share.reshared.borrow_mut() = ();
Ok(())
}
pub fn is_shared(&self) -> bool {
self.share.is_some()
}
@ -2936,19 +2947,6 @@ impl<'a> TryFrom<(&'a CharBag, proto::Entry)> for Entry {
}
}
async fn send_worktree_update(client: &Arc<Client>, update: proto::UpdateWorktree) -> Result<()> {
#[cfg(any(test, feature = "test-support"))]
const MAX_CHUNK_SIZE: usize = 2;
#[cfg(not(any(test, feature = "test-support")))]
const MAX_CHUNK_SIZE: usize = 256;
for update in proto::split_worktree_update(update, MAX_CHUNK_SIZE) {
client.request(update).await?;
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;