Lines
100 %
Functions
Branches
use super::super::transaction_edit_tests::wait_for_blocked_on;
use super::*;
use crate::account_state::STALE_ACCOUNT;
use sqlx::Connection;
#[local_db_sqlx_test]
async fn test_a_change_committed_while_the_edit_waits_makes_it_stale(
pool: PgPool,
) -> Result<(), anyhow::Error> {
let fx = fixture().await?;
let account = fx.account("Cash", None).await?;
let loaded = fx.revision(account).await?;
let mut conn = fx.user.get_connection().await?;
let mut blocker = conn.begin().await?;
lock_hierarchy(&mut blocker, fx.user.id).await?;
let edit = fx.edit(account, "Other", None, vec![], loaded);
let change = async {
wait_for_blocked_on(&mut blocker, 1).await?;
fx.set_tag(account, "type", "asset").await?;
blocker.commit().await?;
anyhow::Ok(())
};
let (edited, changed) = tokio::join!(edit, change);
changed?;
assert!(
matches!(&edited, Err(CmdError::Args(m)) if m == STALE_ACCOUNT),
"{edited:?}"
);
assert_eq!(
pairs(&fx.stored(account).await?),
[("name", "Cash"), ("type", "asset")]
}
async fn test_opposing_edits_leave_no_cycle(pool: PgPool) -> Result<(), anyhow::Error> {
let a = fx.account("A", None).await?;
let b = fx.account("B", None).await?;
let (rev_a, rev_b) = (fx.revision(a).await?, fx.revision(b).await?);
let a_under_b = fx.edit(a, "A", Some(b), vec![], rev_a);
let b_under_a = fx.edit(b, "B", Some(a), vec![], rev_b);
let release = async {
let waited = wait_for_blocked_on(&mut blocker, 2).await;
waited
let (first, second, released) = tokio::join!(a_under_b, b_under_a, release);
released?;
[first.is_ok(), second.is_ok()]
.iter()
.filter(|ok| **ok)
.count(),
1
let parents = [fx.stored(a).await?.parent, fx.stored(b).await?.parent];
parents.iter().filter(|p| p.is_some()).count(),
1,
"{parents:?}"
async fn test_a_parent_deleted_while_the_edit_waits_is_parent_gone(
let parent = fx.account("Parent", None).await?;
let edit = fx.edit(account, "Cash", Some(parent), vec![], loaded);
let delete = async {
sqlx::query("DELETE FROM account_tags WHERE account_id = $1")
.bind(parent)
.execute(&mut *blocker)
.await?;
sqlx::query("DELETE FROM accounts WHERE id = $1")
let (edited, deleted) = tokio::join!(edit, delete);
deleted?;
matches!(&edited, Err(CmdError::ParentGone(id)) if *id == parent),
assert_eq!(fx.stored(account).await?.parent, None);
/// The tag set changes between the pre-lock read and the account row lock:
/// the attempt retries and the next one sees the changed revision.
async fn test_a_tag_linked_while_the_edit_waits_for_the_row_retries_into_stale(
sqlx::query("SELECT id FROM accounts WHERE id = $1 FOR NO KEY UPDATE")
.bind(account)
.fetch_one(&mut *blocker)
let edit = fx.edit(account, "Cash", None, vec![], loaded);
let link = async {
let tag_id = Uuid::new_v4();
sqlx::query("INSERT INTO tags (id, tag_name, tag_value) VALUES ($1, 'late', 'x')")
.bind(tag_id)
sqlx::query("INSERT INTO account_tags (account_id, tag_id) VALUES ($1, $2)")
let (edited, linked) = tokio::join!(edit, link);
linked?;
[("late", "x"), ("name", "Cash")]