Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

test(gpui): retry every Linux spawn of a copied binary while it is busy

Richie McIlroyd780c0b34b7b
diff --git a/apps/desktop-gpui/src/single_instance.rs b/apps/desktop-gpui/src/single_instance.rs
index 632315e48..2dba7a73d 100644
--- a/apps/desktop-gpui/src/single_instance.rs
+++ b/apps/desktop-gpui/src/single_instance.rs
@@ -1281,21 +1281,7 @@ mod tests {
std::fs::create_dir(&directory).unwrap();
let binary = directory.join("cap-gpui");
std::fs::copy("/bin/sleep", &binary).unwrap();
- // A test thread that forks while the copy's write descriptor is open
- // holds it until its exec, and exec of the copy fails with ETXTBSY
- // until then.
- let started = std::time::Instant::now();
- let mut child = loop {
- match std::process::Command::new(&binary).arg("30").spawn() {
- Err(error)
- if error.kind() == std::io::ErrorKind::ExecutableFileBusy
- && started.elapsed() < std::time::Duration::from_secs(5) =>
- {
- std::thread::sleep(std::time::Duration::from_millis(5));
- }
- result => break result.unwrap(),
- }
- };
+ let mut child = spawn_copied_sleep(&binary);
let pid = child.id() as i32;
let started = std::time::Instant::now();
// spawn can return before /proc stops exposing the child's pre-exec image.
@@ -1343,6 +1329,25 @@ mod tests {
std::fs::write(marker, std::process::id().to_string()).unwrap();
}
+ /// Runs a fresh copy of `/bin/sleep` for 30 s. A test thread that forks
+ /// while the copy's write descriptor is open holds it until its exec, and
+ /// exec of the copy fails with ETXTBSY until then.
+ #[cfg(target_os = "linux")]
+ fn spawn_copied_sleep(binary: &Path) -> std::process::Child {
+ let started = std::time::Instant::now();
+ loop {
+ match std::process::Command::new(binary).arg("30").spawn() {
+ Err(error)
+ if error.kind() == std::io::ErrorKind::ExecutableFileBusy
+ && started.elapsed() < std::time::Duration::from_secs(5) =>
+ {
+ std::thread::sleep(std::time::Duration::from_millis(5));
+ }
+ result => break result.unwrap(),
+ }
+ }
+ }
+
#[cfg(target_os = "linux")]
pub(super) struct LinuxAcquireFixture {
directory: std::path::PathBuf,
@@ -1366,10 +1371,7 @@ mod tests {
pub(super) fn start_incumbent(&mut self) -> u32 {
let binary = self.directory.join("cap-gpui");
std::fs::copy("/bin/sleep", &binary).unwrap();
- let child = std::process::Command::new(&binary)
- .arg("30")
- .spawn()
- .unwrap();
+ let child = spawn_copied_sleep(&binary);
let pid = child.id();
self.children.push(child);
let started = std::time::Instant::now();