Lines
92.31 %
Functions
60 %
Branches
100 %
//! A script's tag write-back racing an edit of the same transaction.
use super::*;
use crate::command::CmdError;
use crate::error::ServerError;
use crate::script::TransactionState;
use crate::script::load_transaction_state;
use crate::script_tags::write_one;
/// A transaction tagged `trip=japan`, loaded, with the scripts having deleted
/// that tag; also the revision it was loaded at.
async fn trip_dropped(fx: &Fixture) -> anyhow::Result<(Uuid, TransactionState, String)> {
let tx_id = fx.create_tx(None).await?;
UpdateTransaction::new()
.user_id(fx.user.id)
.transaction_id(tx_id)
.tags(tag_map(&[("trip", "japan")]))
.run()
.await?;
let mut state = load_transaction_state(fx.user.id, tx_id)
.await?
.ok_or_else(|| anyhow::anyhow!("not loaded"))?;
let revision = state
.loaded_revision
.clone()
.ok_or_else(|| anyhow::anyhow!("no revision"))?;
let trip = state.transaction_tags.remove(0);
state.deleted_transaction_tags.insert(trip.id);
Ok((tx_id, state, revision))
}
/// An edit holding the row lock past its revision check stalls the
/// write-back, which then sees the edit's revision and writes nothing, so the
/// tag the edit keeps stays linked.
#[local_db_sqlx_test]
async fn test_script_detach_waits_for_an_edit_holding_the_row(
pool: PgPool,
) -> Result<(), anyhow::Error> {
use sqlx::Connection;
let fx = fixture().await?;
let (tx_id, state, _) = trip_dropped(&fx).await?;
let mut conn = fx.user.get_connection().await?;
let mut edit = conn.begin().await?;
sqlx::query_file!("sql/select/transactions/by_id_locked.sql", tx_id)
.fetch_one(&mut *edit)
let write_back = write_one(fx.user, tx_id, &state);
let finish_edit = async {
wait_for_blocked(&fx, 1).await?;
let during = fx.tx_tags(tx_id).await?;
crate::tag::user::links::set_transaction_tag(&mut edit, tx_id, &tag("zone", "z")).await?;
edit.commit().await?;
anyhow::Ok(during)
};
let (written, during) = tokio::join!(write_back, finish_edit);
assert_eq!(during?, vec![pair("trip", "japan")]);
match written {
Err(ServerError::Script(message)) => assert_eq!(message, crate::logical::STALE_REVISION),
other => anyhow::bail!("the write-back must be refused: {other:?}"),
assert_eq!(
fx.tx_tags(tx_id).await?,
vec![pair("trip", "japan"), pair("zone", "z")]
);
/// A third connection holds the row lock while the write-back and the edit
/// start, both from the same load. Exactly one may commit: the other must see
/// the changed revision, so the deleted tag is never re-linked by the edit.
async fn test_script_detach_racing_an_edit_is_never_lost(
for _ in 0..ROUNDS {
let (tx_id, state, revision) = trip_dropped(&fx).await?;
let mut blocker = conn.begin().await?;
.fetch_one(&mut *blocker)
let edit = UpdateTransaction::new()
.tags(tag_map(&[("trip", "japan"), ("zone", "z")]))
.expected_revision(revision)
.run();
let release = release_after(wait_for_blocked(&fx, 2), blocker);
let (edited, written, released) = tokio::join!(edit, write_back, release);
released?;
let tags = fx.tx_tags(tx_id).await?;
match (edited, written) {
(Ok(_), Err(ServerError::Script(message))) => {
assert_eq!(message, crate::logical::STALE_REVISION);
assert_eq!(tags, vec![pair("trip", "japan"), pair("zone", "z")]);
(Err(CmdError::Args(message)), Ok(())) => {
assert!(tags.is_empty(), "{tags:?}");
other => anyhow::bail!("exactly one of the edit and the write-back commits: {other:?}"),