artifactize-core 0.27.2

The library behind artifactize: Artifacts, evals, reviews, the monitor and review models, and the shared operations every interface calls.
Documentation
use super::*;
use std::process::Stdio;

#[tokio::test]
async fn cancelling_close_and_shutting_down_the_runtime_releases_sqlite_without_panicking() {
    // The test owns the probe's files and the probe: they go only after it has exited.
    let root = crate::test_os::tempdir();
    let mut command =
        crate::test_os::self_test("store::connection::tests::close_cancellation_fixture");
    command
        .env("ARTIFACTIZE_SQLITE_CLOSE_FIXTURE", root.path())
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::piped());
    let mut probe = crate::test_os::spawn_test(&mut command);
    let (status, stderr) = probe.stderr_output().await;
    assert!(
        status.success(),
        "{status:?}: {}",
        crate::platform::output_text(&stderr)
    );
    assert!(
        stderr.is_empty(),
        "a cancelled SQLite close must not panic on its worker: {}",
        crate::platform::output_text(&stderr)
    );
    root.close().unwrap();
}

/// The probe: its files are in the folder the test gives it, which owns them.
#[test]
fn close_cancellation_fixture() {
    let Some(root) = crate::platform::environment::var("ARTIFACTIZE_SQLITE_CLOSE_FIXTURE") else {
        return;
    };
    let root = std::path::PathBuf::from(root);
    let runtime = tokio::runtime::Builder::new_current_thread()
        .build()
        .unwrap();
    let connection = runtime
        .block_on(Connection::open(root.join("closing.sqlite")))
        .unwrap();
    let (release, held) = std::sync::mpsc::channel();
    runtime.block_on(async {
        let (entered, started) = tokio::sync::oneshot::channel();
        let operation = connection.call(move |_| {
            entered.send(()).unwrap();
            // A channel, not a sleep, keeps close pending until its observer is gone.
            let _ = held.recv();
            Ok::<_, rusqlite::Error>(())
        });
        tokio::pin!(operation);
        assert!(futures_util::poll!(&mut operation).is_pending());
        started.await.unwrap();
        let closing = close(connection.clone());
        tokio::pin!(closing);
        assert!(futures_util::poll!(&mut closing).is_pending());
        // Both awaiting futures are cancelled while the SQLite worker is held.
    });
    release.send(()).unwrap();
    drop(runtime);
    let runtime = tokio::runtime::Builder::new_current_thread()
        .build()
        .unwrap();
    // Wait for the actual worker's closed channel, including any worker panic hook,
    // before the parent checks its stderr and releases the temporary files.
    assert!(matches!(
        runtime.block_on(connection.call(|_| Ok::<_, rusqlite::Error>(()))),
        Err(tokio_rusqlite::Error::ConnectionClosed)
    ));
    drop(connection);
}