Allow simulate_guest and simulate_host to fail when host disconnects

This commit is contained in:
Antonio Scandurra 2022-04-08 11:29:00 +02:00
parent fae9048a2a
commit da976012a9

View file

@ -1118,6 +1118,7 @@ mod tests {
},
time::Duration,
};
use util::TryFutureExt;
use workspace::{Item, SplitDirection, Workspace, WorkspaceParams};
#[cfg(test)]
@ -5151,6 +5152,7 @@ mod tests {
});
host_language_registry.add(Arc::new(language));
let host_disconnected = Rc::new(AtomicBool::new(false));
user_ids.push(host.current_user_id(&host_cx));
clients.push(cx.foreground().spawn(host.simulate_host(
host_project,
@ -5158,7 +5160,7 @@ mod tests {
operations.clone(),
max_operations,
rng.clone(),
&mut host_cx,
host_cx,
)));
while operations.get() < max_operations {
@ -5200,24 +5202,28 @@ mod tests {
operations.clone(),
max_operations,
rng.clone(),
host_disconnected.clone(),
guest_cx,
)));
log::info!("Guest {} added", guest_id);
} else if rng.lock().gen_bool(0.05) {
host_disconnected.store(true, SeqCst);
server.forbid_connections();
server.disconnect_client(user_ids[0]);
cx.foreground().advance_clock(RECEIVE_TIMEOUT);
let mut clients = futures::future::join_all(clients).await;
cx.foreground().run_until_parked();
let store = server.store.read();
let (host, host_cx) = clients.remove(0);
let (host, mut host_cx) = clients.remove(0);
host.project
.as_ref()
.unwrap()
.read_with(&host_cx, |project, _| assert!(!project.is_shared()));
for (guest, guest_cx) in clients {
assert!(store
for (guest, mut guest_cx) in clients {
assert!(server
.store
.read()
.contacts_for_user(guest.current_user_id(&guest_cx))
.is_empty());
guest
@ -5225,7 +5231,9 @@ mod tests {
.as_ref()
.unwrap()
.read_with(&guest_cx, |project, _| assert!(project.is_read_only()));
guest_cx.update(|_| drop(guest));
}
host_cx.update(|_| drop(host));
return;
}
@ -5619,7 +5627,16 @@ mod tests {
}
async fn simulate_host(
&mut self,
mut self,
project: ModelHandle<Project>,
files: Arc<Mutex<Vec<PathBuf>>>,
operations: Rc<Cell<usize>>,
max_operations: usize,
rng: Arc<Mutex<StdRng>>,
mut cx: TestAppContext,
) -> (Self, TestAppContext) {
async fn simulate_host_internal(
client: &mut TestClient,
project: ModelHandle<Project>,
files: Arc<Mutex<Vec<PathBuf>>>,
operations: Rc<Cell<usize>>,
@ -5654,11 +5671,15 @@ mod tests {
}
}
10..=80 if !files.lock().is_empty() => {
let buffer = if self.buffers.is_empty() || rng.lock().gen() {
let buffer = if client.buffers.is_empty() || rng.lock().gen() {
let file = files.lock().choose(&mut *rng.lock()).unwrap().clone();
let (worktree, path) = project
.update(cx, |project, cx| {
project.find_or_create_local_worktree(file.clone(), true, cx)
project.find_or_create_local_worktree(
file.clone(),
true,
cx,
)
})
.await?;
let project_path =
@ -5673,10 +5694,11 @@ mod tests {
.update(cx, |project, cx| project.open_buffer(project_path, cx))
.await
.unwrap();
self.buffers.insert(buffer.clone());
client.buffers.insert(buffer.clone());
buffer
} else {
self.buffers
client
.buffers
.iter()
.choose(&mut *rng.lock())
.unwrap()
@ -5689,7 +5711,7 @@ mod tests {
"Host: dropping buffer {:?}",
buffer.read(cx).file().unwrap().full_path(cx)
);
self.buffers.remove(&buffer);
client.buffers.remove(&buffer);
drop(buffer);
});
} else {
@ -5730,12 +5752,25 @@ mod tests {
cx.background().simulate_random_delay().await;
}
self.project = Some(project);
log::info!("Host done");
Ok(())
}
simulate_host_internal(
&mut self,
project.clone(),
files,
operations,
max_operations,
rng,
&mut cx,
)
.log_err()
.await;
log::info!("Host done");
self.project = Some(project);
(self, cx)
}
pub async fn simulate_guest(
mut self,
guest_id: usize,
@ -5743,11 +5778,22 @@ mod tests {
operations: Rc<Cell<usize>>,
max_operations: usize,
rng: Arc<Mutex<StdRng>>,
host_disconnected: Rc<AtomicBool>,
mut cx: TestAppContext,
) -> (Self, TestAppContext) {
async fn simulate_guest_internal(
client: &mut TestClient,
guest_id: usize,
project: ModelHandle<Project>,
operations: Rc<Cell<usize>>,
max_operations: usize,
rng: Arc<Mutex<StdRng>>,
cx: &mut TestAppContext,
) -> anyhow::Result<()> {
while operations.get() < max_operations {
let buffer = if self.buffers.is_empty() || rng.lock().gen() {
let worktree = if let Some(worktree) = project.read_with(&cx, |project, cx| {
let buffer = if client.buffers.is_empty() || rng.lock().gen() {
let worktree = if let Some(worktree) =
project.read_with(cx, |project, cx| {
project
.worktrees(&cx)
.filter(|worktree| {
@ -5765,7 +5811,7 @@ mod tests {
operations.set(operations.get() + 1);
let (worktree_root_name, project_path) =
worktree.read_with(&cx, |worktree, _| {
worktree.read_with(cx, |worktree, _| {
let entry = worktree
.entries(false)
.filter(|e| e.is_file())
@ -5784,25 +5830,25 @@ mod tests {
worktree_root_name,
);
let buffer = project
.update(&mut cx, |project, cx| {
.update(cx, |project, cx| {
project.open_buffer(project_path.clone(), cx)
})
.await
.unwrap();
.await?;
log::info!(
"Guest {}: opened path {:?} in worktree {} ({}) with buffer id {}",
guest_id,
project_path.1,
project_path.0,
worktree_root_name,
buffer.read_with(&cx, |buffer, _| buffer.remote_id())
buffer.read_with(cx, |buffer, _| buffer.remote_id())
);
self.buffers.insert(buffer.clone());
client.buffers.insert(buffer.clone());
buffer
} else {
operations.set(operations.get() + 1);
self.buffers
client
.buffers
.iter()
.choose(&mut *rng.lock())
.unwrap()
@ -5818,12 +5864,12 @@ mod tests {
guest_id,
buffer.read(cx).file().unwrap().full_path(cx)
);
self.buffers.remove(&buffer);
client.buffers.remove(&buffer);
drop(buffer);
});
}
10..=19 => {
let completions = project.update(&mut cx, |project, cx| {
let completions = project.update(cx, |project, cx| {
log::info!(
"Guest {}: requesting completions for buffer {} ({:?})",
guest_id,
@ -5834,17 +5880,19 @@ mod tests {
project.completions(&buffer, offset, cx)
});
let completions = cx.background().spawn(async move {
completions.await.expect("completions request failed");
completions
.await
.map_err(|err| anyhow!("completions request failed: {:?}", err))
});
if rng.lock().gen_bool(0.3) {
log::info!("Guest {}: detaching completions request", guest_id);
completions.detach();
cx.update(|cx| completions.detach_and_log_err(cx));
} else {
completions.await;
completions.await?;
}
}
20..=29 => {
let code_actions = project.update(&mut cx, |project, cx| {
let code_actions = project.update(cx, |project, cx| {
log::info!(
"Guest {}: requesting code actions for buffer {} ({:?})",
guest_id,
@ -5855,17 +5903,19 @@ mod tests {
project.code_actions(&buffer, range, cx)
});
let code_actions = cx.background().spawn(async move {
code_actions.await.expect("code actions request failed");
code_actions.await.map_err(|err| {
anyhow!("code actions request failed: {:?}", err)
})
});
if rng.lock().gen_bool(0.3) {
log::info!("Guest {}: detaching code actions request", guest_id);
code_actions.detach();
cx.update(|cx| code_actions.detach_and_log_err(cx));
} else {
code_actions.await;
code_actions.await?;
}
}
30..=39 if buffer.read_with(&cx, |buffer, _| buffer.is_dirty()) => {
let (requested_version, save) = buffer.update(&mut cx, |buffer, cx| {
30..=39 if buffer.read_with(cx, |buffer, _| buffer.is_dirty()) => {
let (requested_version, save) = buffer.update(cx, |buffer, cx| {
log::info!(
"Guest {}: saving buffer {} ({:?})",
guest_id,
@ -5875,18 +5925,21 @@ mod tests {
(buffer.version(), buffer.save(cx))
});
let save = cx.background().spawn(async move {
let (saved_version, _) = save.await.expect("save request failed");
let (saved_version, _) = save
.await
.map_err(|err| anyhow!("save request failed: {:?}", err))?;
assert!(saved_version.observed_all(&requested_version));
Ok::<_, anyhow::Error>(())
});
if rng.lock().gen_bool(0.3) {
log::info!("Guest {}: detaching save request", guest_id);
save.detach();
cx.update(|cx| save.detach_and_log_err(cx));
} else {
save.await;
save.await?;
}
}
40..=44 => {
let prepare_rename = project.update(&mut cx, |project, cx| {
let prepare_rename = project.update(cx, |project, cx| {
log::info!(
"Guest {}: preparing rename for buffer {} ({:?})",
guest_id,
@ -5897,17 +5950,19 @@ mod tests {
project.prepare_rename(buffer, offset, cx)
});
let prepare_rename = cx.background().spawn(async move {
prepare_rename.await.expect("prepare rename request failed");
prepare_rename.await.map_err(|err| {
anyhow!("prepare rename request failed: {:?}", err)
})
});
if rng.lock().gen_bool(0.3) {
log::info!("Guest {}: detaching prepare rename request", guest_id);
prepare_rename.detach();
cx.update(|cx| prepare_rename.detach_and_log_err(cx));
} else {
prepare_rename.await;
prepare_rename.await?;
}
}
45..=49 => {
let definitions = project.update(&mut cx, |project, cx| {
let definitions = project.update(cx, |project, cx| {
log::info!(
"Guest {}: requesting definitions for buffer {} ({:?})",
guest_id,
@ -5918,18 +5973,21 @@ mod tests {
project.definition(&buffer, offset, cx)
});
let definitions = cx.background().spawn(async move {
definitions.await.expect("definitions request failed")
definitions
.await
.map_err(|err| anyhow!("definitions request failed: {:?}", err))
});
if rng.lock().gen_bool(0.3) {
log::info!("Guest {}: detaching definitions request", guest_id);
definitions.detach();
cx.update(|cx| definitions.detach_and_log_err(cx));
} else {
self.buffers
.extend(definitions.await.into_iter().map(|loc| loc.buffer));
client
.buffers
.extend(definitions.await?.into_iter().map(|loc| loc.buffer));
}
}
50..=54 => {
let highlights = project.update(&mut cx, |project, cx| {
let highlights = project.update(cx, |project, cx| {
log::info!(
"Guest {}: requesting highlights for buffer {} ({:?})",
guest_id,
@ -5940,33 +5998,37 @@ mod tests {
project.document_highlights(&buffer, offset, cx)
});
let highlights = cx.background().spawn(async move {
highlights.await.expect("highlights request failed");
highlights
.await
.map_err(|err| anyhow!("highlights request failed: {:?}", err))
});
if rng.lock().gen_bool(0.3) {
log::info!("Guest {}: detaching highlights request", guest_id);
highlights.detach();
cx.update(|cx| highlights.detach_and_log_err(cx));
} else {
highlights.await;
highlights.await?;
}
}
55..=59 => {
let search = project.update(&mut cx, |project, cx| {
let search = project.update(cx, |project, cx| {
let query = rng.lock().gen_range('a'..='z');
log::info!("Guest {}: project-wide search {:?}", guest_id, query);
project.search(SearchQuery::text(query, false, false), cx)
});
let search = cx
.background()
.spawn(async move { search.await.expect("search request failed") });
let search = cx.background().spawn(async move {
search
.await
.map_err(|err| anyhow!("search request failed: {:?}", err))
});
if rng.lock().gen_bool(0.3) {
log::info!("Guest {}: detaching search request", guest_id);
search.detach();
cx.update(|cx| search.detach_and_log_err(cx));
} else {
self.buffers.extend(search.await.into_keys());
client.buffers.extend(search.await?.into_keys());
}
}
_ => {
buffer.update(&mut cx, |buffer, cx| {
buffer.update(cx, |buffer, cx| {
log::info!(
"Guest {}: updating buffer {} ({:?})",
guest_id,
@ -5979,8 +6041,29 @@ mod tests {
}
cx.background().simulate_random_delay().await;
}
Ok(())
}
log::info!("Guest {} done", guest_id);
match simulate_guest_internal(
&mut self,
guest_id,
project.clone(),
operations,
max_operations,
rng,
&mut cx,
)
.await
{
Ok(()) => log::info!("guest {} done", guest_id),
Err(err) => {
if host_disconnected.load(SeqCst) {
log::error!("guest {} simulation error - {:?}", guest_id, err);
} else {
panic!("guest {} simulation error - {:?}", guest_id, err);
}
}
}
self.project = Some(project);
(self, cx)