Lines
100 %
Functions
Branches
use super::super::transaction_edit_tests::wait_for_blocked_on;
use super::*;
use sqlx::Connection;
#[local_db_sqlx_test]
async fn test_opposing_reparents_leave_no_cycle(pool: PgPool) -> Result<(), anyhow::Error> {
let fx = fixture().await?;
let a = fx.account("A", None).await?;
let b = fx.account("B", None).await?;
let mut conn = fx.user.get_connection().await?;
let mut blocker = conn.begin().await?;
lock_hierarchy(&mut blocker, fx.user.id).await?;
let a_under_b = fx.reparent(a, Some(b));
let b_under_a = fx.reparent(b, Some(a));
let release = async {
let waited = wait_for_blocked_on(&mut blocker, 2).await;
blocker.commit().await?;
waited
};
let (first, second, released) = tokio::join!(a_under_b, b_under_a, release);
released?;
assert_eq!(
[first.is_ok(), second.is_ok()]
.iter()
.filter(|ok| **ok)
.count(),
1
);
assert!(is_refused(&first) || is_refused(&second));
let (a_parent, b_parent) = (fx.parent_of(a).await?, fx.parent_of(b).await?);
assert!(
matches!(
(a_parent, b_parent),
(Some(Some(_)), Some(None)) | (Some(None), Some(Some(_)))
),
"{a_parent:?} {b_parent:?}"
}
/// Either order is consistent: the merge first deletes `from`, so the move is
/// refused as a missing account; the move first makes the merge a cycle.
async fn test_a_merge_and_a_reparent_through_it_are_serialized(
pool: PgPool,
) -> Result<(), anyhow::Error> {
let from = fx.account("From", None).await?;
let into = fx.account("Into", None).await?;
let merge = fx.merge(into, from);
let reparent = fx.reparent(into, Some(from));
let (merged, moved, released) = tokio::join!(merge, reparent, release);
let from_gone = fx.parent_of(from).await?.is_none();
match (merged.is_ok(), moved.is_ok()) {
(true, false) => assert!(from_gone && fx.parent_of(into).await? == Some(None)),
(false, true) => assert!(
is_refused(&merged) && fx.parent_of(into).await? == Some(Some(from)),
"{merged:?}"
other => anyhow::bail!("not serialized: {other:?} {merged:?} {moved:?}"),
/// A reparent behind a stalled hierarchy holder gives up at the lock timeout.
async fn test_a_reparent_behind_a_held_hierarchy_lock_times_out(
let reparent = fx.reparent(a, Some(b));
let release = async { wait_for_blocked_on(&mut blocker, 1).await };
let (moved, released) = tokio::join!(reparent, release);
let err = moved.expect_err("the reparent must time out");
assert!(err.busy().is_some(), "{err:?}");
assert_eq!(fx.parent_of(a).await?, Some(None));