test(cli): what holds the books is known; the thinned tests say what their names say
Plan P15 unit A5, fix round. Each of these fails without its fix (checked by
disabling the fix): a serve, a command and `close`/`doctor` meeting a closing
server or a command's server (orphan of a killed host, another command's),
a command's own server holding server.lock and books.lock while it works and
refusing the app's start and making `serve` wait, a command's server holding none
of the invoker's environment, pipes or folder, a command racing the app's start,
two commands at once with nothing running, a bridge that held a host following the
app that takes its place, the app asking a host to close, and server.info itself.
Thinned tests, mended: the_host_starts_without_a_scope_when_systemd_run_fails
drove `books create`, which never starts a host, and passed with the fallback
broken; it now drives `serve` and fails with the fallback broken.
with_nothing_running_a_command_runs_its_own_server… asserted
`success || stderr contains "backup"`, which passes on a failure; it asserts the
real outcome (exit 1, "No backups are set up"; a backup that is made is
books_are_backed_up_now_from_the_command_line).
the_bridge_reaches_the_books_serve_holds… has its close after the last lease back
(serve let go, the books close). a_command_leaves_nothing_running_and_serve_closes…
has "a lease arriving within the delay cancels the close" back (a second `serve`
within `close_after` keeps the same host). books_commands_hold_the_books_while_they_run
points to the test of a command's own server.
Moved tests, one line each (what each still protects, read against the diff of
e9a520138):
a_command_leaves_nothing_running_and_serve_closes_after_its_last_holder: nothing
left by a command or a bridge, serve's delay and its cancel (restored).
serve_and_the_bridges_share_one_host_and_one_server: one host, one server, two
`serve`s racing, bridges and a command joining.
the_bridge_answers_initialize_itself_during_a_slow_start, …gives_up_on_books_that_never_open,
a_call_cancelled_at_once_is_never_made, a_signalled_bridge_goes_at_once,
books_made_mid_session_and_bad_requests: still the bridge's waiting on a host whose
server is slow (`serve` with `server_delay`), the same knobs.
books_commands_hold_the_books_while_they_run: commands lease a host that serve holds.
a_command_ends_at_its_deadline_during_a_restart: still a command ending at its
deadline while update.lock is held (`token list`; `books list` reads the folder).
a_bridge_started_without_stderr_keeps_its_descriptors_apart,
the_commands_given_run_as_the_agents_own_token,
an_agent_is_connected_and_reaches_the_books_by_what_it_was_given: unchanged bodies
behind a `serve` that holds the books; the properties are the bridge's and the
token's, not who started the host.
doctor_says_whether_the_books_are_open_and_what_holds_them: asserts the new closed words.
the_host_serve_starts_is_clean_from_a_hostile_serve: the host's scrub, from serve.
the_viewers_new_books_are_made_by_the_server: new books through a server that runs.
update.rs (9 moved): the diff is the harness only (`keep_host`, the request key); their
subjects (restart onto a replaced program, update.lock, staging, verifying, applying
before a host starts) are the host's, which `serve` starts the same way; read in full
for the_host_stages_a_daily_update_and_the_next_start_applies_it (the next start is now
serve's), the others by the diff and a green run.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
2275c34e86
commit
da0ed04bb3
2 files changed
+674
-9
No files matched your search
@@ -293,8 +293,12 @@ fn with_nothing_running_a_command_runs_its_own_server_or_says_to_start_wanderboo
|
||||
let token = command(&["token", "create", "--book", BOOK]);
|
||||
assert!(token.status.success(), "{token:?}");
|
||||
let backed = command(&["books", "back-up", BOOK]);
|
||||
// The command's own server did the work, and said the true thing: no
|
||||
// backup is set up in these books (a backup that is made is
|
||||
// `books_are_backed_up_now_from_the_command_line`, open_while_held.rs).
|
||||
assert_eq!(backed.status.code(), Some(1), "{backed:?}");
|
||||
assert!(
|
||||
backed.status.success() || String::from_utf8_lossy(&backed.stderr).contains("backup"),
|
||||
String::from_utf8_lossy(&backed.stderr).contains("No backups are set up"),
|
||||
"{backed:?}"
|
||||
);
|
||||
assert!(!server_lock_held(&run), "the command's server was left");
|
||||
|
||||
@@ -553,10 +553,22 @@ fn a_command_leaves_nothing_running_and_serve_closes_after_its_last_holder() {
|
||||
// comes in time cancels nothing, and no second host starts.
|
||||
home.create("beta");
|
||||
assert_eq!(home.pids("host").len(), 1, "one host");
|
||||
let host = home.host().expect("serve's host");
|
||||
held.child.kill().unwrap();
|
||||
held.child.wait().unwrap();
|
||||
std::thread::sleep(Duration::from_millis(300));
|
||||
assert!(home.host().is_some(), "closed before the delay");
|
||||
// A lease arriving within the delay cancels the close: the same host,
|
||||
// still there well past the delay the last lease's end began.
|
||||
let mut again = serve(&home);
|
||||
std::thread::sleep(Duration::from_millis(3000));
|
||||
assert_eq!(
|
||||
home.host(),
|
||||
Some(host),
|
||||
"closed though a lease came in time"
|
||||
);
|
||||
again.child.kill().unwrap();
|
||||
again.child.wait().unwrap();
|
||||
assert!(home.wait_closed(10), "never closed after the holder died");
|
||||
// The last holder is an agent's bridge: serve lets go (Ctrl-C), the
|
||||
// host stays for the bridge past the close delay, and closes after the
|
||||
@@ -681,6 +693,14 @@ fn the_bridge_reaches_the_books_serve_holds_and_holds_them_for_the_session() {
|
||||
assert!(a.end().is_some_and(|s| s.success()));
|
||||
// The session's end leaves the host to `serve`, which still holds.
|
||||
assert!(home.host().is_some(), "serve's host went with a session");
|
||||
// And with `serve` let go too, nothing holds the books: the host closes
|
||||
// after its delay, past the last lease (a bridge as the last holder is
|
||||
// `a_command_leaves_nothing_running_and_serve_closes_after_its_last_holder`).
|
||||
drop(home.serving.lock().unwrap().take());
|
||||
assert!(
|
||||
home.wait_closed(15),
|
||||
"the books stayed open after the last lease ended"
|
||||
);
|
||||
// Unknown books: a plain error, no folder named.
|
||||
let mut a = Agent::start(&home, "nobody");
|
||||
a.initialize();
|
||||
@@ -1192,6 +1212,10 @@ fn a_restart_is_not_a_close() {
|
||||
assert!(a.call("books.get_entry", json!({"entry_id": 1})).is_ok());
|
||||
}
|
||||
|
||||
/// (What a command holds while it works with nothing running, its own
|
||||
/// server's `server.lock` and `books.lock`, and the refusal of a second
|
||||
/// program, is
|
||||
/// `a_commands_own_server_holds_the_books_while_it_works_and_others_are_refused`.)
|
||||
#[test]
|
||||
fn books_commands_hold_the_books_while_they_run() {
|
||||
let home = Home::new("cmds");
|
||||
@@ -1588,20 +1612,25 @@ fn the_host_starts_without_a_scope_when_systemd_run_fails() {
|
||||
std::fs::create_dir(&runtime).unwrap();
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
std::fs::set_permissions(&runtime, std::fs::Permissions::from_mode(0o700)).unwrap();
|
||||
// `serve` is what starts a host (WB-app-5), so it is what is driven:
|
||||
// with no scope to be made, the host starts directly and is held.
|
||||
let started = Instant::now();
|
||||
let out = home
|
||||
.cmd("books")
|
||||
.args(["create", "acme"])
|
||||
.env("XDG_RUNTIME_DIR", &runtime)
|
||||
.stdin(Stdio::null())
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(out.status.success(), "{out:?}");
|
||||
let mut serving = Running::start({
|
||||
let mut c = home.cmd("serve");
|
||||
c.env("XDG_RUNTIME_DIR", &runtime);
|
||||
c
|
||||
});
|
||||
serving.expect("The books are open", 30);
|
||||
assert!(
|
||||
started.elapsed() < Duration::from_secs(10),
|
||||
"{:?}",
|
||||
started.elapsed()
|
||||
);
|
||||
assert!(home.host().is_some(), "no host was started without a scope");
|
||||
// Let go: serve ends, the host closes after its delay.
|
||||
serving.signal(libc::SIGINT);
|
||||
assert!(serving.wait(10).is_some_and(|s| s.success()));
|
||||
assert!(home.wait_closed(15));
|
||||
}
|
||||
|
||||
/// Review round 5, findings 2 and 3: a malformed byte is answered and the
|
||||
@@ -3094,3 +3123,635 @@ fn shell_join(words: &[String]) -> String {
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ")
|
||||
}
|
||||
|
||||
// What holds the books is known and not guessed (opener.rs INVARIANT 6a,
|
||||
// ledger-mcp local.rs INVARIANT 16; WB-app-5): a server says what started
|
||||
// it and whether it is closing, and a holder or a command that finds a
|
||||
// command's server or a closing one waits for it, in words, and goes on.
|
||||
|
||||
/// A real books server, started by hand (`wanderbooks server`, its admin
|
||||
/// socket answering), whose word about itself the test chooses: what a
|
||||
/// killed host's server still finishing (`host`, closing), another
|
||||
/// command's server (`command`) or the app's closing server (`app`,
|
||||
/// closing) say in `run/server.info`.
|
||||
struct StandIn {
|
||||
child: Child,
|
||||
run: PathBuf,
|
||||
}
|
||||
|
||||
impl StandIn {
|
||||
fn start(home: &Home, says: (&str, bool)) -> Self {
|
||||
let child = Command::new(&home.exe)
|
||||
.args(["server", "--open", "none", "--root"])
|
||||
.arg(home.root())
|
||||
.env_clear()
|
||||
.env("PATH", "/usr/bin:/bin")
|
||||
.env("HOME", home.dir.path())
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.spawn()
|
||||
.unwrap();
|
||||
let run = home.run_dir();
|
||||
assert!(
|
||||
wait_for(30, || ledger_host::opener::admin_answers(&run) == Ok(true)),
|
||||
"the stand-in server never answered"
|
||||
);
|
||||
std::fs::write(
|
||||
run.join("server.info"),
|
||||
json!({"by": says.0, "pid": child.id(), "closing": says.1}).to_string(),
|
||||
)
|
||||
.unwrap();
|
||||
Self { child, run }
|
||||
}
|
||||
|
||||
/// The server ends (its input closes), and takes its word with it.
|
||||
fn finish(mut self) {
|
||||
drop(self.child.stdin.take());
|
||||
self.child.wait().unwrap();
|
||||
let _ = std::fs::remove_file(self.run.join("server.info"));
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for StandIn {
|
||||
fn drop(&mut self) {
|
||||
let _ = self.child.kill();
|
||||
let _ = self.child.wait();
|
||||
}
|
||||
}
|
||||
|
||||
/// A program run with its output in a file, so a test reads what it has
|
||||
/// said so far while it is still running.
|
||||
struct Logged {
|
||||
child: Child,
|
||||
log: PathBuf,
|
||||
}
|
||||
|
||||
impl Logged {
|
||||
fn start(mut c: Command, log: PathBuf) -> Self {
|
||||
let file = std::fs::File::create(&log).unwrap();
|
||||
let child = c
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(file.try_clone().unwrap())
|
||||
.stderr(file)
|
||||
.spawn()
|
||||
.unwrap();
|
||||
Self { child, log }
|
||||
}
|
||||
|
||||
fn said(&self) -> String {
|
||||
std::fs::read_to_string(&self.log).unwrap_or_default()
|
||||
}
|
||||
|
||||
fn says(&self, text: &str, secs: u64) -> bool {
|
||||
wait_for(secs, || self.said().contains(text))
|
||||
}
|
||||
|
||||
fn running(&mut self) -> bool {
|
||||
self.child.try_wait().unwrap().is_none()
|
||||
}
|
||||
|
||||
fn done(&mut self, secs: u64) -> Option<std::process::ExitStatus> {
|
||||
let deadline = Instant::now() + Duration::from_secs(secs);
|
||||
while Instant::now() < deadline {
|
||||
if let Some(status) = self.child.try_wait().unwrap() {
|
||||
return Some(status);
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(20));
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Logged {
|
||||
fn drop(&mut self) {
|
||||
let _ = self.child.kill();
|
||||
let _ = self.child.wait();
|
||||
}
|
||||
}
|
||||
|
||||
/// What a holder or command says while it waits for a command's server,
|
||||
/// and for a server that is closing.
|
||||
const WAITS_FOR_COMMAND: &str = "command has the books open for a moment: waiting for it";
|
||||
const WAITS_FOR_CLOSE: &str = "is closing the books (a last backup is being made): waiting";
|
||||
|
||||
/// A `serve` started while a killed host's server is still finishing (its
|
||||
/// admin socket still answering, no host holding the root) was told "the
|
||||
/// app has these books open" and exited. It waits, in words, for the
|
||||
/// server to be gone, and then holds the books.
|
||||
#[test]
|
||||
fn serve_waits_in_words_for_a_server_that_is_closing_and_then_holds_the_books() {
|
||||
let home = Home::new("serve-closing");
|
||||
home.create("acme");
|
||||
let orphan = StandIn::start(&home, ("host", true));
|
||||
let mut serving = Logged::start(home.cmd("serve"), home.dir.path().join("serve.log"));
|
||||
assert!(serving.says(WAITS_FOR_CLOSE, 20), "{}", serving.said());
|
||||
assert!(serving.running(), "serve gave up: {}", serving.said());
|
||||
assert!(!serving.said().contains("has these books open"));
|
||||
assert!(home.host().is_none(), "a host started beside a closing one");
|
||||
orphan.finish();
|
||||
assert!(
|
||||
serving.says("The books are open", 40),
|
||||
"serve never took the books: {}",
|
||||
serving.said()
|
||||
);
|
||||
assert!(home.host().is_some());
|
||||
}
|
||||
|
||||
/// A command, `close` and `doctor` that find the app's server closing say
|
||||
/// so, and a command goes on once it is gone; none says "the app has them".
|
||||
#[test]
|
||||
fn a_command_and_the_words_of_close_and_doctor_know_a_server_is_closing() {
|
||||
let home = Home::new("cmd-closing");
|
||||
home.create("acme");
|
||||
let closing = StandIn::start(&home, ("app", true));
|
||||
let doctor = home.cmd("doctor").stdin(Stdio::null()).output().unwrap();
|
||||
let said = String::from_utf8_lossy(&doctor.stdout).into_owned();
|
||||
assert!(
|
||||
said.contains("The books are closing") && !said.contains("(the app)"),
|
||||
"{said}"
|
||||
);
|
||||
let close = home.cmd("close").stdin(Stdio::null()).output().unwrap();
|
||||
let said = String::from_utf8_lossy(&close.stdout).into_owned();
|
||||
assert!(
|
||||
said.contains("The books are closing") && !said.contains("the app"),
|
||||
"{said}"
|
||||
);
|
||||
let mut list = Logged::start(
|
||||
{
|
||||
let mut c = home.cmd("token");
|
||||
c.arg("list");
|
||||
c
|
||||
},
|
||||
home.dir.path().join("token.log"),
|
||||
);
|
||||
assert!(list.says(WAITS_FOR_CLOSE, 20), "{}", list.said());
|
||||
assert!(list.running(), "{}", list.said());
|
||||
closing.finish();
|
||||
assert!(
|
||||
list.done(40).is_some_and(|s| s.success()),
|
||||
"{}",
|
||||
list.said()
|
||||
);
|
||||
assert!(!list.said().contains("has these books open"));
|
||||
}
|
||||
|
||||
/// A command that finds another command's server is not the app's: it
|
||||
/// waits for that server to finish, in words, bounded by `open_wait`, and
|
||||
/// does its own work after. It neither attaches to it (a company that the
|
||||
/// other has not open was "not open: open it in Wanderbooks") nor closes
|
||||
/// it under the other.
|
||||
#[test]
|
||||
fn a_command_waits_for_another_commands_server_and_does_not_attach_to_it() {
|
||||
let home = Home::new("cmd-cmd");
|
||||
home.create("acme");
|
||||
let other = StandIn::start(&home, ("command", false));
|
||||
// Out of patience: the wait ends at its bound, in words, and the
|
||||
// other's server is untouched.
|
||||
let short = home
|
||||
.cmd("books")
|
||||
.args([
|
||||
"back-up",
|
||||
"acme",
|
||||
"--test-knobs",
|
||||
&format!("{KNOBS},open_wait=1500"),
|
||||
])
|
||||
.stdin(Stdio::null())
|
||||
.output()
|
||||
.unwrap();
|
||||
let err = String::from_utf8_lossy(&short.stderr).into_owned();
|
||||
assert_eq!(short.status.code(), Some(1), "{err}");
|
||||
assert!(
|
||||
err.contains(WAITS_FOR_COMMAND) && err.contains("still has the books open"),
|
||||
"{err}"
|
||||
);
|
||||
assert!(!err.contains("isn't open"), "attached to it: {err}");
|
||||
assert!(ledger_host::opener::server_lock_held(&home.run_dir()));
|
||||
// Patient: it waits, and goes on when the other is done.
|
||||
let mut list = Logged::start(
|
||||
{
|
||||
let mut c = home.cmd("token");
|
||||
c.arg("list");
|
||||
c
|
||||
},
|
||||
home.dir.path().join("token.log"),
|
||||
);
|
||||
assert!(list.says(WAITS_FOR_COMMAND, 20), "{}", list.said());
|
||||
assert!(list.running(), "{}", list.said());
|
||||
other.finish();
|
||||
assert!(
|
||||
list.done(40).is_some_and(|s| s.success()),
|
||||
"{}",
|
||||
list.said()
|
||||
);
|
||||
assert!(!ledger_host::opener::server_lock_held(&home.run_dir()));
|
||||
}
|
||||
|
||||
/// A second `wanderbooks serve` started while a command's server is up
|
||||
/// was told "the app has these books open". It waits for the command, in
|
||||
/// words, and holds the books after.
|
||||
#[test]
|
||||
fn a_serve_waits_for_a_commands_server_and_then_holds_the_books() {
|
||||
let home = Home::new("serve-cmd");
|
||||
home.create("acme");
|
||||
let other = StandIn::start(&home, ("command", false));
|
||||
let mut serving = Logged::start(home.cmd("serve"), home.dir.path().join("serve.log"));
|
||||
assert!(serving.says(WAITS_FOR_COMMAND, 20), "{}", serving.said());
|
||||
assert!(serving.running(), "{}", serving.said());
|
||||
assert!(!serving.said().contains("(the app)"));
|
||||
other.finish();
|
||||
assert!(serving.says("The books are open", 40), "{}", serving.said());
|
||||
}
|
||||
|
||||
/// The server says what started it and when it closes, in
|
||||
/// `run/server.info`, and removes it as it lets go (local.rs INVARIANT 16).
|
||||
#[test]
|
||||
fn a_server_says_what_started_it_and_when_it_closes() {
|
||||
let home = Home::new("info");
|
||||
let run = home.run_dir();
|
||||
let mut server = Command::new(&home.exe)
|
||||
.args([
|
||||
"server",
|
||||
"--open",
|
||||
"none",
|
||||
"--started-by",
|
||||
"command",
|
||||
"--root",
|
||||
])
|
||||
.arg(home.root())
|
||||
.env_clear()
|
||||
.env("PATH", "/usr/bin:/bin")
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.spawn()
|
||||
.unwrap();
|
||||
let pid = server.id();
|
||||
assert!(wait_for(30, || {
|
||||
ledger_mcp::local::read_server_info(&run).is_some()
|
||||
&& ledger_host::opener::admin_answers(&run) == Ok(true)
|
||||
}));
|
||||
let info = ledger_mcp::local::read_server_info(&run).unwrap();
|
||||
assert_eq!(
|
||||
(info.by, info.pid, info.closing),
|
||||
(ledger_mcp::local::StartedBy::Command, pid, false)
|
||||
);
|
||||
assert_eq!(
|
||||
ledger_host::opener::what_holds(&run),
|
||||
Ok(ledger_host::opener::Holder::Command)
|
||||
);
|
||||
// Told to close: it says so while it is still closing (its door stays
|
||||
// open for a moment after the last request), and then it is gone, and
|
||||
// its word with it.
|
||||
drop(server.stdin.take());
|
||||
assert!(
|
||||
wait_for(20, || {
|
||||
ledger_mcp::local::read_server_info(&run).is_some_and(|i| i.closing)
|
||||
}),
|
||||
"it never said it was closing"
|
||||
);
|
||||
server.wait().unwrap();
|
||||
assert!(!run.join("server.info").exists(), "its word was left");
|
||||
}
|
||||
|
||||
/// While a command works, its own server holds the books' locks, says it
|
||||
/// is a command's, and every other program is turned away or waits: the
|
||||
/// app's start is refused, and `serve` waits (WB-app-5).
|
||||
#[test]
|
||||
fn a_commands_own_server_holds_the_books_while_it_works_and_others_are_refused() {
|
||||
use ledger_host::app_server::{ANOTHER_HAS_THEM, AppServer};
|
||||
let home = Home::new("cmd-holds");
|
||||
let run = home.run_dir();
|
||||
std::fs::write(run.join("timer-hold"), "create:start").unwrap();
|
||||
let mut create = Logged::start(
|
||||
{
|
||||
let mut c = home.cmd("books");
|
||||
c.args([
|
||||
"create",
|
||||
"zed",
|
||||
"--test-knobs",
|
||||
&format!("{KNOBS},timer_hold=1"),
|
||||
]);
|
||||
c
|
||||
},
|
||||
home.dir.path().join("create.log"),
|
||||
);
|
||||
assert!(
|
||||
wait_for(30, || {
|
||||
std::fs::read_to_string(run.join("timer-held"))
|
||||
.is_ok_and(|held| held.lines().any(|l| l == "create:start"))
|
||||
}),
|
||||
"the command's server never reached its work: {}",
|
||||
create.said()
|
||||
);
|
||||
// It holds the books, and says whose they are.
|
||||
assert!(ledger_host::opener::server_lock_held(&run));
|
||||
assert!(!ledger_host::opener::host_lock_held(&run));
|
||||
let info = ledger_mcp::local::read_server_info(&run).expect("it says what it is");
|
||||
assert_eq!(info.by, ledger_mcp::local::StartedBy::Command);
|
||||
assert!(!info.closing);
|
||||
assert!(home.pids("server").contains(&info.pid));
|
||||
let books_lock = std::fs::OpenOptions::new()
|
||||
.write(true)
|
||||
.open(run.join("books.lock"))
|
||||
.expect("the books lock exists");
|
||||
assert!(
|
||||
matches!(
|
||||
books_lock.try_lock(),
|
||||
Err(std::fs::TryLockError::WouldBlock)
|
||||
),
|
||||
"the books' lock is not held while the command works"
|
||||
);
|
||||
// The app's start is refused, and nothing is touched by it.
|
||||
let dir = home.dir.path().to_path_buf();
|
||||
let refused = AppServer::start_in(&home.exe, Some(&home.root()), &[], move |c| {
|
||||
common::clean_env(c, &dir);
|
||||
})
|
||||
.err()
|
||||
.expect("a start beside a command's server");
|
||||
assert_eq!(refused, ANOTHER_HAS_THEM);
|
||||
// `serve` waits for it, in words, and does not start a host beside it.
|
||||
let serving = Logged::start(home.cmd("serve"), home.dir.path().join("serve.log"));
|
||||
assert!(serving.says(WAITS_FOR_COMMAND, 20), "{}", serving.said());
|
||||
assert!(home.host().is_none());
|
||||
// The work goes on when it is let go, and succeeds.
|
||||
std::fs::remove_file(run.join("timer-hold")).unwrap();
|
||||
assert!(
|
||||
create.done(40).is_some_and(|s| s.success()),
|
||||
"{}",
|
||||
create.said()
|
||||
);
|
||||
assert!(home.root().join("books/zed").is_dir());
|
||||
assert!(serving.says("The books are open", 40), "{}", serving.said());
|
||||
}
|
||||
|
||||
/// A command whose server is slow to take the books, while the app starts
|
||||
/// in between its look and its server's start, joins the app: it neither
|
||||
/// fails with advice to close what is not its to close, nor waits silently
|
||||
/// for the full time for a server that has lost; one bounded wait, in
|
||||
/// words. The app's server is left as it was.
|
||||
#[test]
|
||||
fn a_command_racing_the_apps_start_joins_the_app() {
|
||||
use ledger_host::app_server::AppServer;
|
||||
let home = Home::new("cmd-race");
|
||||
home.create("acme");
|
||||
let run = home.run_dir();
|
||||
let mut list = Logged::start(
|
||||
{
|
||||
let mut c = home.cmd("token");
|
||||
c.args([
|
||||
"list",
|
||||
"--test-knobs",
|
||||
&format!("{KNOBS},server_delay=4000"),
|
||||
]);
|
||||
c
|
||||
},
|
||||
home.dir.path().join("token.log"),
|
||||
);
|
||||
// Its own server is started (and delaying its take of the lock).
|
||||
assert!(
|
||||
wait_for(30, || !home.pids("server").is_empty()),
|
||||
"the command never started its server: {}",
|
||||
list.said()
|
||||
);
|
||||
// The app starts now, and gets the books first.
|
||||
let dir = home.dir.path().to_path_buf();
|
||||
let app = AppServer::start_in(&home.exe, Some(&home.root()), &[], move |c| {
|
||||
common::clean_env(c, &dir);
|
||||
})
|
||||
.unwrap();
|
||||
app.wait_ready(Duration::from_secs(60)).unwrap();
|
||||
let began = Instant::now();
|
||||
assert!(
|
||||
list.done(60).is_some_and(|s| s.success()),
|
||||
"{}",
|
||||
list.said()
|
||||
);
|
||||
assert!(
|
||||
list.said()
|
||||
.contains("Another program started the books just now"),
|
||||
"{}",
|
||||
list.said()
|
||||
);
|
||||
assert!(!list.said().contains("Close it"), "{}", list.said());
|
||||
assert!(
|
||||
began.elapsed() < Duration::from_secs(30),
|
||||
"it waited for its own server: {:?}",
|
||||
began.elapsed()
|
||||
);
|
||||
// The app's server is the one left, and still the app's.
|
||||
assert_eq!(app.exited(), None);
|
||||
assert!(ledger_host::opener::server_lock_held(&run));
|
||||
assert!(wait_for(20, || home.pids("server").len() == 1));
|
||||
assert_eq!(
|
||||
ledger_mcp::local::read_server_info(&run).map(|i| i.by),
|
||||
Some(ledger_mcp::local::StartedBy::App)
|
||||
);
|
||||
assert_eq!(
|
||||
app.stop(Duration::from_secs(60)),
|
||||
ledger_host::app_server::Stopped::Closed
|
||||
);
|
||||
}
|
||||
|
||||
/// Two commands at once with nothing running: each is done, neither is
|
||||
/// told to close anything, neither loses its work, and nothing is left.
|
||||
#[test]
|
||||
fn two_commands_at_once_with_nothing_running_both_do_their_work() {
|
||||
let home = Home::new("two-cmds");
|
||||
let make = |id: &str| {
|
||||
Logged::start(
|
||||
{
|
||||
let mut c = home.cmd("books");
|
||||
c.args(["create", id]);
|
||||
c
|
||||
},
|
||||
home.dir.path().join(format!("{id}.log")),
|
||||
)
|
||||
};
|
||||
let (mut a, mut b) = (make("alpha"), make("beta"));
|
||||
for (who, run) in [("alpha", &mut a), ("beta", &mut b)] {
|
||||
assert!(
|
||||
run.done(60).is_some_and(|s| s.success()),
|
||||
"{who}: {}",
|
||||
run.said()
|
||||
);
|
||||
assert!(!run.said().contains("Close it"), "{who}: {}", run.said());
|
||||
assert!(
|
||||
!run.said().contains("has these books open"),
|
||||
"{who}: {}",
|
||||
run.said()
|
||||
);
|
||||
}
|
||||
let listed = home.books(&["list"]);
|
||||
let text = String::from_utf8_lossy(&listed.stdout).into_owned();
|
||||
for id in ["alpha", "beta"] {
|
||||
assert!(text.contains(&format!("{id} not open")), "{text}");
|
||||
}
|
||||
assert!(!ledger_host::opener::server_lock_held(&home.run_dir()));
|
||||
assert!(home.host().is_none());
|
||||
}
|
||||
|
||||
/// A bridge that began under a `serve` host, when that host is gone and
|
||||
/// the app has started in its place, follows what runs as a bridge that
|
||||
/// began without a host does: when the app is closed, the assistant is
|
||||
/// told "Wanderbooks isn't running", and not left to a timeout.
|
||||
#[test]
|
||||
fn a_bridge_that_held_a_host_follows_the_app_that_takes_its_place() {
|
||||
use ledger_host::app_server::AppServer;
|
||||
let home = Home::new("bridge-app");
|
||||
home.create("acme");
|
||||
home.keep_host();
|
||||
let mut bridge = Command::new(&home.exe);
|
||||
bridge
|
||||
.args(["mcp", "--book", "acme", "--root"])
|
||||
.arg(home.root())
|
||||
.args(["--test-knobs", &format!("{KNOBS},backoff=3000")])
|
||||
.env_clear()
|
||||
.env("PATH", "/usr/bin:/bin")
|
||||
.env("HOME", home.dir.path());
|
||||
let mut a = Agent::from(bridge);
|
||||
a.initialize();
|
||||
assert!(a.call("books.start", json!({})).is_ok());
|
||||
// `serve` and its host go (killed: nothing closes in order), and the
|
||||
// app takes the books while the bridge waits to try again.
|
||||
drop(home.serving.lock().unwrap().take());
|
||||
for pid in home.pids("host") {
|
||||
// SAFETY: a host of this test's own root.
|
||||
unsafe { libc::kill(pid as i32, libc::SIGKILL) };
|
||||
}
|
||||
assert!(wait_for(30, || {
|
||||
!ledger_host::opener::host_lock_held(&home.run_dir())
|
||||
&& !ledger_host::opener::server_lock_held(&home.run_dir())
|
||||
}));
|
||||
let dir = home.dir.path().to_path_buf();
|
||||
let app = AppServer::start_in(
|
||||
&home.exe,
|
||||
Some(&home.root()),
|
||||
&["acme".to_string()],
|
||||
move |c| common::clean_env(c, &dir),
|
||||
)
|
||||
.unwrap();
|
||||
app.wait_ready(Duration::from_secs(60)).unwrap();
|
||||
// The bridge reaches the app's company.
|
||||
assert!(
|
||||
wait_for(30, || a.call("books.start", json!({})).is_ok()),
|
||||
"the bridge never reached the app"
|
||||
);
|
||||
// The app closes: the next call says what is so.
|
||||
assert_eq!(
|
||||
app.stop(Duration::from_secs(60)),
|
||||
ledger_host::app_server::Stopped::Closed
|
||||
);
|
||||
let said = a.call("books.start", json!({})).unwrap_err();
|
||||
assert!(said.starts_with("Wanderbooks isn't running"), "{said}");
|
||||
}
|
||||
|
||||
/// A command's own server is started clean, as a host is (start.rs
|
||||
/// INVARIANT 1, local.rs run): it may outlive its command (still closing
|
||||
/// after 90 s), so it holds nothing of the invoker's: not its environment,
|
||||
/// not a pipe it handed down, not its folder.
|
||||
#[test]
|
||||
fn a_commands_own_server_holds_none_of_the_invokers_environment_or_pipes() {
|
||||
use std::os::unix::process::CommandExt;
|
||||
let home = Home::new("cmd-clean");
|
||||
let run = home.run_dir();
|
||||
std::fs::write(run.join("timer-hold"), "create:start").unwrap();
|
||||
// A pipe the invoker holds and hands down on descriptor 9 (left open
|
||||
// across exec, as a careless launcher leaves one).
|
||||
let mut fds = [0i32; 2];
|
||||
// SAFETY: pipe fills two descriptors.
|
||||
assert_eq!(unsafe { libc::pipe(fds.as_mut_ptr()) }, 0);
|
||||
let link = |fd: i32| {
|
||||
std::fs::read_link(format!("/proc/self/fd/{fd}"))
|
||||
.unwrap()
|
||||
.to_string_lossy()
|
||||
.into_owned()
|
||||
};
|
||||
let (pipe_read, pipe_write) = (link(fds[0]), link(fds[1]));
|
||||
let mut c = home.cmd("books");
|
||||
c.args([
|
||||
"create",
|
||||
"zed",
|
||||
"--test-knobs",
|
||||
&format!("{KNOBS},timer_hold=1"),
|
||||
])
|
||||
.env("WB_HOSTILE", "secret");
|
||||
let (read_end, write_end) = (fds[0], fds[1]);
|
||||
// SAFETY: dup2 and close are async-signal-safe and touch only the
|
||||
// child's own descriptors.
|
||||
unsafe {
|
||||
c.pre_exec(move || {
|
||||
if libc::dup2(write_end, 9) < 0 {
|
||||
return Err(std::io::Error::last_os_error());
|
||||
}
|
||||
libc::close(read_end);
|
||||
libc::close(write_end);
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
let mut create = Logged::start(c, home.dir.path().join("create.log"));
|
||||
assert!(
|
||||
wait_for(30, || {
|
||||
std::fs::read_to_string(run.join("timer-held"))
|
||||
.is_ok_and(|held| held.lines().any(|l| l == "create:start"))
|
||||
}),
|
||||
"the command's server never reached its work: {}",
|
||||
create.said()
|
||||
);
|
||||
let info = ledger_mcp::local::read_server_info(&run).expect("it says what it is");
|
||||
let proc = PathBuf::from(format!("/proc/{}", info.pid));
|
||||
let environ = std::fs::read(proc.join("environ")).unwrap();
|
||||
let environ = String::from_utf8_lossy(&environ);
|
||||
assert!(!environ.contains("WB_HOSTILE"), "{environ:?}");
|
||||
assert!(environ.contains("PATH=/usr/local/bin:/usr/bin:/bin"));
|
||||
for entry in std::fs::read_dir(proc.join("fd")).unwrap().flatten() {
|
||||
let target = std::fs::read_link(entry.path())
|
||||
.unwrap_or_default()
|
||||
.to_string_lossy()
|
||||
.into_owned();
|
||||
assert!(
|
||||
target != pipe_read && target != pipe_write,
|
||||
"the invoker's pipe is held by the server: fd {:?} -> {target}",
|
||||
entry.file_name()
|
||||
);
|
||||
}
|
||||
assert_eq!(
|
||||
std::fs::read_link(proc.join("cwd")).unwrap(),
|
||||
Path::new("/")
|
||||
);
|
||||
// The invoker's own pipe is still its own (the command holds it).
|
||||
assert!(Path::new("/proc/self/fd").join(fds[0].to_string()).exists());
|
||||
std::fs::remove_file(run.join("timer-hold")).unwrap();
|
||||
assert!(
|
||||
create.done(40).is_some_and(|s| s.success()),
|
||||
"{}",
|
||||
create.said()
|
||||
);
|
||||
// SAFETY: closing the test's own descriptors.
|
||||
unsafe {
|
||||
libc::close(fds[0]);
|
||||
libc::close(fds[1]);
|
||||
}
|
||||
}
|
||||
|
||||
/// The app's notice can ask the host that holds the books to close them,
|
||||
/// with the lease protocol an older host speaks too: what `wanderbooks
|
||||
/// close` does, for a person with no command line (commands.rs
|
||||
/// `close_the_host`).
|
||||
#[test]
|
||||
fn the_app_can_ask_a_host_that_holds_the_books_to_close_them() {
|
||||
let home = Home::new("close-host");
|
||||
home.create("acme");
|
||||
let root = home.root();
|
||||
assert!(!ledger_host::commands::a_host_holds(Some(&root)));
|
||||
assert_eq!(
|
||||
ledger_host::commands::close_the_host(Some(&root)).as_deref(),
|
||||
Ok("No program has the books open now.")
|
||||
);
|
||||
home.keep_host();
|
||||
assert!(ledger_host::commands::a_host_holds(Some(&root)));
|
||||
let said = ledger_host::commands::close_the_host(Some(&root)).unwrap();
|
||||
assert_eq!(said, "The other program let go of the books.");
|
||||
assert!(home.wait_closed(20), "the host was left");
|
||||
assert!(!ledger_host::commands::a_host_holds(Some(&root)));
|
||||
}
|
||||
Reference in new issue
Block a user