mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 08:39:02 +03:00
test(cli-rs): check contender failures through service startup
reports_overlapping_contender_failures_without_recruiting_replacements now kills the second child so the first child's status and stderr must survive a distinguishable failure. Keep recovers_when_its_own_unresponsive_contender_is_evicted_after_a_prior_failure for eviction and reports_a_contender_terminated_by_a_signal for exit status. Keep the deterministic clock checks for backoff boundaries. reuses_a_registered_service, waits_for_a_registered_service_to_finish_starting and reports_a_failed_registered_service_without_spawning cover the normal probe decisions through ensure.
This commit is contained in:
@@ -172,12 +172,11 @@ mod tests {
|
||||
struct Fake {
|
||||
pid: u32,
|
||||
exit: Rc<Cell<Option<i32>>>,
|
||||
released: Rc<Cell<bool>>,
|
||||
}
|
||||
|
||||
impl Fake {
|
||||
fn new(pid: u32) -> Self {
|
||||
Self { pid, exit: Rc::default(), released: Rc::default() }
|
||||
Self { pid, exit: Rc::default() }
|
||||
}
|
||||
|
||||
fn exit(&self, code: i32) {
|
||||
@@ -198,9 +197,7 @@ mod tests {
|
||||
fn failure(&self) -> Option<String> {
|
||||
self.exit.get().filter(|code| *code != 0).map(|code| format!("failure of {} ({code})", self.pid))
|
||||
}
|
||||
fn release(&self) {
|
||||
self.released.set(true);
|
||||
}
|
||||
fn release(&self) {}
|
||||
}
|
||||
|
||||
fn pool(spawn: u64, max_spawn: u64) -> Pool<Fake> {
|
||||
@@ -211,42 +208,6 @@ mod tests {
|
||||
start + Duration::from_millis(offset)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remembers_the_first_startup_failure_and_reports_it_once_no_attempt_is_left() {
|
||||
let now = Instant::now();
|
||||
let mut pool = pool(0, 30_000);
|
||||
let survivor = Fake::new(1);
|
||||
let failed = Fake::new(2);
|
||||
pool.add(survivor.clone(), now);
|
||||
pool.add(failed.clone(), now);
|
||||
failed.exit(1);
|
||||
assert_eq!(pool.reap(), None);
|
||||
assert_eq!(pool.failure(), Some("failure of 2 (1)"));
|
||||
assert!(!pool.should_recruit(false, now));
|
||||
survivor.exit(1);
|
||||
assert_eq!(pool.reap().as_deref(), Some("failure of 2 (1)"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn evicting_a_stopped_owner_forgets_its_failure_and_restarts_the_spawn_clock() {
|
||||
let now = Instant::now();
|
||||
let mut pool = pool(60_000, 60_000);
|
||||
let owner = Fake::new(1);
|
||||
let failed = Fake::new(2);
|
||||
pool.add(owner.clone(), now);
|
||||
pool.add(failed.clone(), now);
|
||||
failed.exit(1);
|
||||
assert_eq!(pool.reap(), None);
|
||||
assert!(pool.failure().is_some());
|
||||
assert!(pool.owns(1));
|
||||
pool.evict(1);
|
||||
assert!(owner.released.get());
|
||||
assert!(!pool.owns(1));
|
||||
assert_eq!(pool.failure(), None);
|
||||
assert_eq!(pool.reap(), None);
|
||||
assert!(pool.should_recruit(false, now));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_unanswered_registration_gets_one_spawn_delay_before_an_attempt_competes() {
|
||||
let now = Instant::now();
|
||||
@@ -298,19 +259,4 @@ mod tests {
|
||||
pool.service_answered();
|
||||
assert!(pool.should_recruit(false, ms(now, 250)));
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_real_contender_reports_its_exit_code_and_stderr() {
|
||||
let contender = Contender::spawn(&["sh".into(), "-c".into(), "echo denied >&2; exit 23".into()], &[]).unwrap();
|
||||
while !contender.finished() {
|
||||
std::thread::sleep(Duration::from_millis(5));
|
||||
}
|
||||
assert_eq!(contender.failure().as_deref(), Some("Server process exited with code 23\ndenied"));
|
||||
let killed = Contender::spawn(&["sh".into(), "-c".into(), "kill -9 $$".into()], &[]).unwrap();
|
||||
while !killed.finished() {
|
||||
std::thread::sleep(Duration::from_millis(5));
|
||||
}
|
||||
assert_eq!(killed.failure().as_deref(), Some("Server process terminated by SIGKILL"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -204,16 +204,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_compatible_service_is_reused_when_ready_awaited_while_starting_and_fails_when_it_failed() {
|
||||
assert_eq!(decide(&service(Some("2.0.0"), State::Ready, true), None), Decision::Reuse);
|
||||
assert_eq!(decide(&service(Some("2.0.0"), State::Waiting, true), Some(&exact("2.0.0"))), Decision::Wait);
|
||||
assert_eq!(
|
||||
decide(&service(Some("2.0.0"), State::Failed, true), None),
|
||||
Decision::Fail("Background service failed to start".to_owned())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_version_mismatch_replaces_the_service_handing_off_terminals_only_when_it_is_ready() {
|
||||
let two = exact("2.0.0");
|
||||
|
||||
@@ -566,7 +566,8 @@ async fn reports_overlapping_contender_failures_without_recruiting_replacements(
|
||||
fixture.release(starts[0], "fail").await;
|
||||
// Let ensure see the exit across two spawn windows before the survivor exits.
|
||||
tokio::time::sleep(Duration::from_millis(450)).await;
|
||||
fixture.release(starts[1], "fail").await;
|
||||
// Distinguish the second failure from the first, whose diagnostics must win.
|
||||
signal(starts[1], Signal::KILL);
|
||||
let error = pending.await.unwrap().unwrap_err().to_string();
|
||||
assert!(started.elapsed() < Duration::from_secs(3));
|
||||
assert!(error.contains("Server process exited with code 23"), "{error}");
|
||||
|
||||
Reference in New Issue
Block a user