Make sure next_scan_complete resolves

Sometimes the scan state could change so quickly that the consumer
wouldn't notice that it ever went to a scanning state before going back
to an idle state, hence never resolving.

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2021-05-11 16:06:12 +02:00
parent ff5561d402
commit 02bd9c8420
3 changed files with 24 additions and 26 deletions

View file

@ -3060,7 +3060,8 @@ mod tests {
tree.flush_fs_events(&app).await; tree.flush_fs_events(&app).await;
fs::remove_file(dir.path().join("file1")).unwrap(); fs::remove_file(dir.path().join("file1")).unwrap();
app.read(|ctx| tree.read(ctx).next_scan_complete()).await; tree.update(&mut app, |tree, ctx| tree.next_scan_complete(ctx))
.await;
model.update(&mut app, |buffer, _| { model.update(&mut app, |buffer, _| {
assert_eq!(*events.borrow(), &[Event::FileHandleChanged]); assert_eq!(*events.borrow(), &[Event::FileHandleChanged]);

View file

@ -732,7 +732,6 @@ mod tests {
use gpui::App; use gpui::App;
use serde_json::json; use serde_json::json;
use std::collections::HashSet; use std::collections::HashSet;
use std::time;
use tempdir::TempDir; use tempdir::TempDir;
#[test] #[test]
@ -986,7 +985,7 @@ mod tests {
workspace.add_worktree(dir.path(), ctx); workspace.add_worktree(dir.path(), ctx);
workspace workspace
}); });
let worktree = app.read(|ctx| { let tree = app.read(|ctx| {
workspace workspace
.read(ctx) .read(ctx)
.worktrees() .worktrees()
@ -995,6 +994,7 @@ mod tests {
.unwrap() .unwrap()
.clone() .clone()
}); });
tree.flush_fs_events(&app).await;
// Create a new untitled buffer // Create a new untitled buffer
let editor = workspace.update(&mut app, |workspace, ctx| { let editor = workspace.update(&mut app, |workspace, ctx| {
@ -1027,15 +1027,12 @@ mod tests {
}); });
// When the save completes, the buffer's title is updated. // When the save completes, the buffer's title is updated.
editor tree.update(&mut app, |tree, ctx| tree.next_scan_complete(ctx))
.condition(&app, |editor, ctx| !editor.is_dirty(ctx))
.await; .await;
worktree app.read(|ctx| {
.condition_with_duration(time::Duration::from_millis(500), &app, |worktree, _| { assert!(!editor.is_dirty(ctx));
worktree.inode_for_path("the-new-name").is_some() assert_eq!(editor.title(ctx), "the-new-name");
}) });
.await;
app.read(|ctx| assert_eq!(editor.title(ctx), "the-new-name"));
// Edit the file and save it again. This time, there is no filename prompt. // Edit the file and save it again. This time, there is no filename prompt.
editor.update(&mut app, |editor, ctx| { editor.update(&mut app, |editor, ctx| {
@ -1057,7 +1054,7 @@ mod tests {
workspace.open_new_file(&(), ctx); workspace.open_new_file(&(), ctx);
workspace.split_pane(workspace.active_pane().clone(), SplitDirection::Right, ctx); workspace.split_pane(workspace.active_pane().clone(), SplitDirection::Right, ctx);
assert!(workspace assert!(workspace
.open_entry((worktree.id(), Path::new("the-new-name").into()), ctx) .open_entry((tree.id(), Path::new("the-new-name").into()), ctx)
.is_none()); .is_none());
}); });
let editor2 = workspace.update(&mut app, |workspace, ctx| { let editor2 = workspace.update(&mut app, |workspace, ctx| {

View file

@ -114,19 +114,17 @@ impl Worktree {
} }
} }
pub fn next_scan_complete(&self) -> impl Future<Output = ()> { pub fn next_scan_complete(&self, ctx: &mut ModelContext<Self>) -> impl Future<Output = ()> {
let mut scan_state_rx = self.scan_state.1.clone(); let scan_id = self.snapshot.scan_id;
let mut did_scan = matches!(*scan_state_rx.borrow(), ScanState::Scanning); ctx.spawn_stream(
async move { self.scan_state.1.clone(),
loop { move |this, _, ctx| {
if let ScanState::Scanning = *scan_state_rx.borrow() { if this.snapshot.scan_id > scan_id {
did_scan = true; ctx.halt_stream();
} else if did_scan {
break;
}
scan_state_rx.recv().await;
}
} }
},
|_, _| {},
)
} }
fn observe_scan_state(&mut self, scan_state: ScanState, ctx: &mut ModelContext<Self>) { fn observe_scan_state(&mut self, scan_state: ScanState, ctx: &mut ModelContext<Self>) {
@ -1495,7 +1493,8 @@ mod tests {
std::fs::remove_file(dir.path().join("b/c/file5")).unwrap(); std::fs::remove_file(dir.path().join("b/c/file5")).unwrap();
std::fs::rename(dir.path().join("b/c"), dir.path().join("d")).unwrap(); std::fs::rename(dir.path().join("b/c"), dir.path().join("d")).unwrap();
std::fs::rename(dir.path().join("a/file2"), dir.path().join("a/file2.new")).unwrap(); std::fs::rename(dir.path().join("a/file2"), dir.path().join("a/file2.new")).unwrap();
app.read(|ctx| tree.read(ctx).next_scan_complete()).await; tree.update(&mut app, |tree, ctx| tree.next_scan_complete(ctx))
.await;
app.read(|ctx| { app.read(|ctx| {
assert_eq!( assert_eq!(
@ -1557,7 +1556,8 @@ mod tests {
fs::write(dir.path().join("tracked-dir/tracked-file2"), "").unwrap(); fs::write(dir.path().join("tracked-dir/tracked-file2"), "").unwrap();
fs::write(dir.path().join("ignored-dir/ignored-file2"), "").unwrap(); fs::write(dir.path().join("ignored-dir/ignored-file2"), "").unwrap();
app.read(|ctx| tree.read(ctx).next_scan_complete()).await; tree.update(&mut app, |tree, ctx| tree.next_scan_complete(ctx))
.await;
app.read(|ctx| { app.read(|ctx| {
let tree = tree.read(ctx); let tree = tree.read(ctx);
let dot_git = tree.entry_for_path(".git").unwrap(); let dot_git = tree.entry_for_path(".git").unwrap();