1
//! A script's tag write-back racing an edit of the same transaction.
2

            
3
use super::*;
4
use crate::command::CmdError;
5
use crate::error::ServerError;
6
use crate::script::TransactionState;
7
use crate::script::load_transaction_state;
8
use crate::script_tags::write_one;
9

            
10
/// A transaction tagged `trip=japan`, loaded, with the scripts having deleted
11
/// that tag; also the revision it was loaded at.
12
9
async fn trip_dropped(fx: &Fixture) -> anyhow::Result<(Uuid, TransactionState, String)> {
13
9
    let tx_id = fx.create_tx(None).await?;
14
9
    UpdateTransaction::new()
15
9
        .user_id(fx.user.id)
16
9
        .transaction_id(tx_id)
17
9
        .tags(tag_map(&[("trip", "japan")]))
18
9
        .run()
19
9
        .await?;
20
9
    let mut state = load_transaction_state(fx.user.id, tx_id)
21
9
        .await?
22
        .ok_or_else(|| anyhow::anyhow!("not loaded"))?;
23
9
    let revision = state
24
9
        .loaded_revision
25
9
        .clone()
26
        .ok_or_else(|| anyhow::anyhow!("no revision"))?;
27
9
    let trip = state.transaction_tags.remove(0);
28
9
    state.deleted_transaction_tags.insert(trip.id);
29
9
    Ok((tx_id, state, revision))
30
9
}
31

            
32
/// An edit holding the row lock past its revision check stalls the
33
/// write-back, which then sees the edit's revision and writes nothing, so the
34
/// tag the edit keeps stays linked.
35
#[local_db_sqlx_test]
36
async fn test_script_detach_waits_for_an_edit_holding_the_row(
37
    pool: PgPool,
38
) -> Result<(), anyhow::Error> {
39
    use sqlx::Connection;
40
    let fx = fixture().await?;
41
    let (tx_id, state, _) = trip_dropped(&fx).await?;
42
    let mut conn = fx.user.get_connection().await?;
43
    let mut edit = conn.begin().await?;
44
    sqlx::query_file!("sql/select/transactions/by_id_locked.sql", tx_id)
45
        .fetch_one(&mut *edit)
46
        .await?;
47
    let write_back = write_one(fx.user, tx_id, &state);
48
1
    let finish_edit = async {
49
1
        wait_for_blocked(&fx, 1).await?;
50
1
        let during = fx.tx_tags(tx_id).await?;
51
1
        crate::tag::user::links::set_transaction_tag(&mut edit, tx_id, &tag("zone", "z")).await?;
52
1
        edit.commit().await?;
53
1
        anyhow::Ok(during)
54
1
    };
55
    let (written, during) = tokio::join!(write_back, finish_edit);
56
    assert_eq!(during?, vec![pair("trip", "japan")]);
57
    match written {
58
        Err(ServerError::Script(message)) => assert_eq!(message, crate::logical::STALE_REVISION),
59
        other => anyhow::bail!("the write-back must be refused: {other:?}"),
60
    }
61
    assert_eq!(
62
        fx.tx_tags(tx_id).await?,
63
        vec![pair("trip", "japan"), pair("zone", "z")]
64
    );
65
}
66

            
67
/// A third connection holds the row lock while the write-back and the edit
68
/// start, both from the same load. Exactly one may commit: the other must see
69
/// the changed revision, so the deleted tag is never re-linked by the edit.
70
#[local_db_sqlx_test]
71
async fn test_script_detach_racing_an_edit_is_never_lost(
72
    pool: PgPool,
73
) -> Result<(), anyhow::Error> {
74
    use sqlx::Connection;
75
    let fx = fixture().await?;
76
    for _ in 0..ROUNDS {
77
        let (tx_id, state, revision) = trip_dropped(&fx).await?;
78
        let mut conn = fx.user.get_connection().await?;
79
        let mut blocker = conn.begin().await?;
80
        sqlx::query_file!("sql/select/transactions/by_id_locked.sql", tx_id)
81
            .fetch_one(&mut *blocker)
82
            .await?;
83
        let edit = UpdateTransaction::new()
84
            .user_id(fx.user.id)
85
            .transaction_id(tx_id)
86
            .tags(tag_map(&[("trip", "japan"), ("zone", "z")]))
87
            .expected_revision(revision)
88
            .run();
89
        let write_back = write_one(fx.user, tx_id, &state);
90
        let release = release_after(wait_for_blocked(&fx, 2), blocker);
91
        let (edited, written, released) = tokio::join!(edit, write_back, release);
92
        released?;
93

            
94
        let tags = fx.tx_tags(tx_id).await?;
95
        match (edited, written) {
96
            (Ok(_), Err(ServerError::Script(message))) => {
97
                assert_eq!(message, crate::logical::STALE_REVISION);
98
                assert_eq!(tags, vec![pair("trip", "japan"), pair("zone", "z")]);
99
            }
100
            (Err(CmdError::Args(message)), Ok(())) => {
101
                assert_eq!(message, crate::logical::STALE_REVISION);
102
                assert!(tags.is_empty(), "{tags:?}");
103
            }
104
            other => anyhow::bail!("exactly one of the edit and the write-back commits: {other:?}"),
105
        }
106
    }
107
}