use super::*;
use crate::{
project::{VerifyOptions, selection::Selection},
types::RequestStatus,
};
use serde_json::json;
const LOCKED: &str = "database is locked";
#[tokio::test]
async fn queued_requests_reach_their_verdicts_while_control_reads_fail() {
let root = crate::test_os::tempdir();
let repo = root.path().join("repo");
let state = root.path().join("state");
std::fs::create_dir(&repo).unwrap();
let evals: Vec<_> = (0..4)
.map(|n| {
json!({
"id": format!("check-{n}"), "title": "Check",
"profile": {"kind": "runtime", "command": crate::test_os::true_program(), "args": []},
"payload": {"instruction": "Check."}
})
})
.collect();
crate::test_declaration::write(
&repo,
repo.join("index.artf"),
json!({"name": "app", "fingerprint": false, "evals": evals}).to_string(),
)
.unwrap();
let options = VerifyOptions {
jobs: Some(1),
..VerifyOptions::default()
};
let (id, accepted) = crate::project::verify::reserve(
&repo,
Some(&state),
&Selection::All,
&options,
None,
None,
&super::super::Origin::Cli,
)
.await
.unwrap();
assert!(accepted);
let run = store::read_run_record(&state, &id).await.unwrap();
let receipts = Receipts::open(&state, &repo).await.unwrap();
let mut changes = crate::changes::Subscription::new(&state).await;
let mut failed = 0_u32;
let watched = execute_watched(
&id,
run,
&receipts,
until_cancelling(
async || {
failed += 1;
Err(LOCKED.to_owned())
},
async || {
changes.next_reconciled().await;
},
),
)
.await;
changes.close().await.unwrap();
receipts.close().await.unwrap();
watched.unwrap();
assert!(failed >= 1, "the control was never read");
let view = store::read_run(&state, &id).await.unwrap();
assert_eq!(view.run.status(), crate::types::RunStatus::Green);
assert_eq!(view.requests.len(), 4);
for request in &view.requests {
assert_eq!(request.status(), RequestStatus::Green, "{request:?}");
}
root.close().unwrap();
}
#[tokio::test]
async fn a_cancellation_read_after_failed_reads_still_ends_the_watch() {
let mut reads = vec![
Ok(true),
Ok(false),
Err(LOCKED.to_owned()),
Err(LOCKED.to_owned()),
];
let mut waits = 0_u32;
until_cancelling(
async || reads.pop().expect("the watch ends at the cancellation"),
async || waits += 1,
)
.await;
assert!(reads.is_empty());
assert_eq!(waits, 3);
}