use std::fs::{File, OpenOptions, TryLockError};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::{Duration, Instant};
use anyhow::{Context, Result, bail, ensure};
use clap::ValueEnum;
const POLL: Duration = Duration::from_millis(200);
const ANNOUNCE: Duration = Duration::from_secs(30);
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
#[value(rename_all = "lower")]
pub(crate) enum Scope {
Target,
Project,
None,
}
pub(crate) fn directory(scope: Scope) -> Result<Option<PathBuf>> {
match scope {
Scope::None => Ok(None),
Scope::Target => Ok(Some(crate::dist::target_dir()?)),
Scope::Project => Ok(Some(project(&crate::dist::workspace_root()?)?)),
}
}
fn project(root: &Path) -> Result<PathBuf> {
let output = Command::new("git")
.current_dir(root)
.env_remove("GIT_DIR")
.env_remove("GIT_WORK_TREE")
.env_remove("GIT_COMMON_DIR")
.args(["rev-parse", "--path-format=absolute", "--git-common-dir"])
.output()
.context("locating the shared Git directory for the e2e run lock")?;
ensure!(
output.status.success(),
"cannot locate the e2e project's shared Git directory: {}",
String::from_utf8_lossy(&output.stderr).trim()
);
let directory = std::str::from_utf8(&output.stdout)
.context("the shared Git directory is not UTF-8")?
.trim_end_matches(['\r', '\n']);
ensure!(
!directory.is_empty(),
"Git returned an empty shared directory"
);
Ok(PathBuf::from(directory))
}
pub(crate) fn acquire(
directory: Option<&Path>,
name: &str,
wait: Option<Duration>,
) -> Result<Option<File>> {
let Some(directory) = directory else {
return Ok(None);
};
std::fs::create_dir_all(directory)
.with_context(|| format!("creating {} for the e2e run lock", directory.display()))?;
let path = directory.join(name);
let file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(false)
.open(&path)
.with_context(|| format!("opening the e2e run lock at {}", path.display()))?;
if held(&file, &path)? {
return Ok(Some(file));
}
eprintln!("waiting for another e2e runner ({})", path.display());
let started = Instant::now();
let mut announced = started;
loop {
std::thread::sleep(POLL);
if held(&file, &path)? {
eprintln!(
"got the e2e run lock after {}s",
started.elapsed().as_secs()
);
return Ok(Some(file));
}
let waited = started.elapsed();
if let Some(cap) = wait
&& waited >= cap
{
bail!(
"gave up waiting for the e2e run lock at {} after {}s: another \
runner still holds it. Let it finish and start again, or ask \
for less -- a module or --last-session needs less budget after \
the wait than the whole suite does. A longer --lock-wait (0 \
waits for ever) only helps if the outer timeout is raised with \
it and there is still room to run afterwards. `--lock` says \
how wide this is: the default holds runs apart only when they \
share a build directory, so a peer in another worktree is \
either running `--lock project` or sharing CARGO_TARGET_DIR \
with you",
path.display(),
waited.as_secs()
);
}
if announced.elapsed() >= ANNOUNCE {
announced = Instant::now();
eprintln!("still waiting for the e2e run lock ({}s)", waited.as_secs());
}
}
}
fn held(file: &File, path: &Path) -> Result<bool> {
match file.try_lock() {
Ok(()) => Ok(true),
Err(TryLockError::WouldBlock) => Ok(false),
Err(TryLockError::Error(error)) => {
Err(error).with_context(|| format!("locking the e2e project at {}", path.display()))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
const NAME: &str = "yew-e2e.lock";
use std::process::{Child, Stdio};
use std::time::{Duration, Instant};
fn git(root: &Path, args: &[&str]) {
let output = Command::new("git")
.current_dir(root)
.args(args)
.output()
.unwrap();
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
}
struct Runner(Child);
impl Drop for Runner {
fn drop(&mut self) {
let _ = self.0.kill();
let _ = self.0.wait();
}
}
#[test]
fn child_runner() {
let Some(directory) = std::env::var_os("YEW_E2E_LOCK_TEST_DIR") else {
return;
};
let _lock = acquire(Some(Path::new(&directory)), NAME, None).unwrap();
std::fs::write(
std::env::var_os("YEW_E2E_LOCK_TEST_READY").unwrap(),
"ready",
)
.unwrap();
std::thread::sleep(Duration::from_secs(30));
}
#[test]
fn a_contended_runner_gives_up_at_its_cap_and_waits_within_it() {
let temp = tempfile::tempdir().unwrap();
let directory = temp.path().join("target");
let holder = acquire(Some(&directory), NAME, None).unwrap();
let cap = Duration::from_millis(300);
let started = Instant::now();
let error = acquire(Some(&directory), NAME, Some(cap)).unwrap_err();
let waited = started.elapsed();
assert!(waited >= cap, "gave up early, after {waited:?}");
assert!(
waited < Duration::from_secs(10),
"never gave up: {waited:?}"
);
assert!(error.to_string().contains("gave up waiting"), "{error:?}");
drop(holder);
acquire(Some(&directory), NAME, Some(cap)).unwrap();
}
#[test]
fn a_build_directory_that_is_not_there_yet_is_made() {
let temp = tempfile::tempdir().unwrap();
let directory = temp.path().join("target/nested");
let _lock = acquire(Some(&directory), NAME, None).unwrap();
assert!(directory.join(NAME).is_file());
}
#[test]
fn separate_build_directories_are_independent() {
let temp = tempfile::tempdir().unwrap();
let mine = temp.path().join("mine/target");
let theirs = temp.path().join("theirs/target");
let _held = acquire(Some(&mine), NAME, None).unwrap();
let _also = acquire(Some(&theirs), NAME, Some(Duration::from_millis(100))).unwrap();
}
#[test]
fn no_lock_is_asked_for_and_none_is_taken() {
assert_eq!(directory(Scope::None).unwrap(), None);
assert!(
acquire(None, NAME, Some(Duration::from_millis(0)))
.unwrap()
.is_none()
);
}
#[test]
fn the_default_scope_is_the_build_directory_and_not_the_project() {
let target = directory(Scope::Target).unwrap().unwrap();
let project = directory(Scope::Project).unwrap().unwrap();
assert_eq!(target, crate::dist::target_dir().unwrap());
assert_ne!(target, project);
}
#[test]
fn the_project_scope_is_shared_by_every_worktree_of_one_project() {
let temp = tempfile::tempdir().unwrap();
let root = temp.path().join("project");
let linked = temp.path().join("worktree");
let other = temp.path().join("other");
std::fs::create_dir(&root).unwrap();
std::fs::create_dir(&other).unwrap();
git(&root, &["init", "--quiet"]);
git(
&root,
&[
"-c",
"user.name=Test",
"-c",
"user.email=test@example.invalid",
"-c",
"commit.gpgsign=false",
"commit",
"--quiet",
"--allow-empty",
"-m",
"fixture",
],
);
git(
&root,
&[
"worktree",
"add",
"--quiet",
"--detach",
linked.to_str().unwrap(),
],
);
git(&other, &["init", "--quiet"]);
assert_eq!(project(&root).unwrap(), project(&linked).unwrap());
assert_ne!(project(&root).unwrap(), project(&other).unwrap());
}
#[test]
fn a_killed_runner_releases_ownership() {
let temp = tempfile::tempdir().unwrap();
let held_by_child = temp.path().join("target");
let elsewhere = temp.path().join("other-target");
let ready = temp.path().join("ready");
let mut runner = Runner(
Command::new(std::env::current_exe().unwrap())
.args(["--exact", "run_lock::tests::child_runner", "--nocapture"])
.env("YEW_E2E_LOCK_TEST_DIR", &held_by_child)
.env("YEW_E2E_LOCK_TEST_READY", &ready)
.stdout(Stdio::null())
.spawn()
.unwrap(),
);
let deadline = Instant::now() + Duration::from_secs(10);
while !ready.exists() {
assert!(
runner.0.try_wait().unwrap().is_none(),
"child exited before locking"
);
assert!(Instant::now() < deadline, "child never acquired the lock");
std::thread::sleep(Duration::from_millis(10));
}
let probe = OpenOptions::new()
.read(true)
.write(true)
.open(held_by_child.join(NAME))
.unwrap();
assert!(matches!(probe.try_lock(), Err(TryLockError::WouldBlock)));
let _independent = acquire(Some(&elsewhere), NAME, None).unwrap();
let (tx, rx) = std::sync::mpsc::channel();
let waiting = std::thread::spawn({
let held_by_child = held_by_child.clone();
move || {
tx.send(acquire(Some(&held_by_child), NAME, None)).unwrap();
}
});
assert!(rx.recv_timeout(Duration::from_millis(100)).is_err());
runner.0.kill().unwrap();
runner.0.wait().unwrap();
let lock = rx.recv_timeout(Duration::from_secs(5)).unwrap().unwrap();
waiting.join().unwrap();
assert!(matches!(probe.try_lock(), Err(TryLockError::WouldBlock)));
drop(lock);
probe.try_lock().unwrap();
}
}