1
use super::super::transaction_edit_tests::wait_for_blocked_on;
2
use super::*;
3
use sqlx::Connection;
4

            
5
#[local_db_sqlx_test]
6
async fn test_opposing_reparents_leave_no_cycle(pool: PgPool) -> Result<(), anyhow::Error> {
7
    let fx = fixture().await?;
8
    let a = fx.account("A", None).await?;
9
    let b = fx.account("B", None).await?;
10
    let mut conn = fx.user.get_connection().await?;
11
    let mut blocker = conn.begin().await?;
12
    lock_hierarchy(&mut blocker, fx.user.id).await?;
13

            
14
    let a_under_b = fx.reparent(a, Some(b));
15
    let b_under_a = fx.reparent(b, Some(a));
16
1
    let release = async {
17
1
        let waited = wait_for_blocked_on(&mut blocker, 2).await;
18
1
        blocker.commit().await?;
19
1
        waited
20
1
    };
21
    let (first, second, released) = tokio::join!(a_under_b, b_under_a, release);
22
    released?;
23

            
24
    assert_eq!(
25
        [first.is_ok(), second.is_ok()]
26
            .iter()
27
2
            .filter(|ok| **ok)
28
            .count(),
29
        1
30
    );
31
    assert!(is_refused(&first) || is_refused(&second));
32
    let (a_parent, b_parent) = (fx.parent_of(a).await?, fx.parent_of(b).await?);
33
    assert!(
34
        matches!(
35
            (a_parent, b_parent),
36
            (Some(Some(_)), Some(None)) | (Some(None), Some(Some(_)))
37
        ),
38
        "{a_parent:?} {b_parent:?}"
39
    );
40
}
41

            
42
/// Either order is consistent: the merge first deletes `from`, so the move is
43
/// refused as a missing account; the move first makes the merge a cycle.
44
#[local_db_sqlx_test]
45
async fn test_a_merge_and_a_reparent_through_it_are_serialized(
46
    pool: PgPool,
47
) -> Result<(), anyhow::Error> {
48
    let fx = fixture().await?;
49
    let from = fx.account("From", None).await?;
50
    let into = fx.account("Into", None).await?;
51
    let mut conn = fx.user.get_connection().await?;
52
    let mut blocker = conn.begin().await?;
53
    lock_hierarchy(&mut blocker, fx.user.id).await?;
54

            
55
    let merge = fx.merge(into, from);
56
    let reparent = fx.reparent(into, Some(from));
57
1
    let release = async {
58
1
        let waited = wait_for_blocked_on(&mut blocker, 2).await;
59
1
        blocker.commit().await?;
60
1
        waited
61
1
    };
62
    let (merged, moved, released) = tokio::join!(merge, reparent, release);
63
    released?;
64

            
65
    let from_gone = fx.parent_of(from).await?.is_none();
66
    match (merged.is_ok(), moved.is_ok()) {
67
        (true, false) => assert!(from_gone && fx.parent_of(into).await? == Some(None)),
68
        (false, true) => assert!(
69
            is_refused(&merged) && fx.parent_of(into).await? == Some(Some(from)),
70
            "{merged:?}"
71
        ),
72
        other => anyhow::bail!("not serialized: {other:?} {merged:?} {moved:?}"),
73
    }
74
}
75

            
76
/// A reparent behind a stalled hierarchy holder gives up at the lock timeout.
77
#[local_db_sqlx_test]
78
async fn test_a_reparent_behind_a_held_hierarchy_lock_times_out(
79
    pool: PgPool,
80
) -> Result<(), anyhow::Error> {
81
    let fx = fixture().await?;
82
    let a = fx.account("A", None).await?;
83
    let b = fx.account("B", None).await?;
84
    let mut conn = fx.user.get_connection().await?;
85
    let mut blocker = conn.begin().await?;
86
    lock_hierarchy(&mut blocker, fx.user.id).await?;
87

            
88
    let reparent = fx.reparent(a, Some(b));
89
1
    let release = async { wait_for_blocked_on(&mut blocker, 1).await };
90
    let (moved, released) = tokio::join!(reparent, release);
91
    released?;
92
    let err = moved.expect_err("the reparent must time out");
93
    assert!(err.busy().is_some(), "{err:?}");
94
    blocker.commit().await?;
95
    assert_eq!(fx.parent_of(a).await?, Some(None));
96
}