use super::*;
#[tokio::test]
async fn failed_writer_initialization_releases_sqlite_before_returning_the_schema_error() {
let root = crate::test_os::tempdir();
let state = root.path().join("state");
let repo = root.path().join("repo");
std::fs::create_dir(&state).unwrap();
std::fs::create_dir(&repo).unwrap();
let database = state.join(DATABASE);
let connection = rusqlite::Connection::open(&database).unwrap();
connection
.execute_batch("CREATE TABLE unrelated(value TEXT)")
.unwrap();
drop(connection);
let error = match OwnedReceipts::open(&state, &repo).await {
Ok(_) => panic!("an unversioned nonempty database is not our schema"),
Err(error) => error,
};
assert!(
error.contains("Unsupported state schema version: 0."),
"{error}"
);
assert!(crate::test_os::database_released(&database).unwrap());
root.close().unwrap();
}
#[tokio::test]
async fn explicit_writer_close_releases_sqlite_before_return_and_closes_clones() {
let root = crate::test_os::tempdir();
let state = root.path().join("state");
let repo = root.path().join("repo");
std::fs::create_dir(&repo).unwrap();
let writer = OwnedReceipts::open(&state, &repo).await.unwrap();
let retained = Receipts::clone(&writer);
let database = state.join(DATABASE);
assert!(!crate::test_os::database_released(&database).unwrap());
Receipts::clone(&writer).close().await.unwrap();
assert!(crate::test_os::database_released(&database).unwrap());
assert_eq!(retained.state_id().await.unwrap_err(), "ConnectionClosed");
root.close().unwrap();
}
#[tokio::test]
async fn owned_writer_closes_before_temp_state_removal_on_success_and_unwind() {
for unwind in [false, true] {
let root = crate::test_os::tempdir();
let state = root.path().join("state");
let repo = root.path().join("repo");
std::fs::create_dir(&repo).unwrap();
let writer = OwnedReceipts::open(&state, &repo).await.unwrap();
assert!(writer.state_id().await.is_ok());
let retained = Receipts::clone(&writer);
if unwind {
let failed = std::panic::catch_unwind(std::panic::AssertUnwindSafe(move || {
let _writer = writer;
panic!("fixture assertion failure");
}));
assert!(failed.is_err());
} else {
drop(writer);
}
let error = retained.state_id().await.unwrap_err();
assert_eq!(error, "ConnectionClosed");
root.close().unwrap();
}
}