1
use super::*;
2

            
3
/// A third connection holds the row lock while both edits start, so both have
4
/// read the transaction before either writes unless the edit locks it first.
5
#[local_db_sqlx_test]
6
async fn test_concurrent_edits_do_not_lose_the_date(pool: PgPool) -> Result<(), anyhow::Error> {
7
    use sqlx::Connection;
8
    let fx = fixture().await?;
9
    let new_date = Utc.with_ymd_and_hms(2020, 1, 2, 3, 4, 5).single();
10
    let new_date = new_date.ok_or_else(|| anyhow::anyhow!("date"))?;
11

            
12
    for _ in 0..ROUNDS {
13
        let tx_id = fx.create_tx(None).await?;
14
        let mut conn = fx.user.get_connection().await?;
15
        let mut blocker = conn.begin().await?;
16
        sqlx::query_file!("sql/select/transactions/by_id_locked.sql", tx_id)
17
            .fetch_one(&mut *blocker)
18
            .await?;
19

            
20
        let set_date = UpdateTransaction::new()
21
            .user_id(fx.user.id)
22
            .transaction_id(tx_id)
23
            .post_date(new_date)
24
            .run();
25
        let set_note = UpdateTransaction::new()
26
            .user_id(fx.user.id)
27
            .transaction_id(tx_id)
28
            .note("later".to_string())
29
            .run();
30
        let release = release_after(wait_for_blocked(&fx, 2), blocker);
31
        let (a, b, r) = tokio::join!(set_date, set_note, release);
32
        a?;
33
        b?;
34
        r?;
35

            
36
        let mut conn = fx.user.get_connection().await?;
37
        let row = sqlx::query_file!("sql/select/transactions/by_id.sql", tx_id)
38
            .fetch_one(&mut *conn)
39
            .await?;
40
        assert_eq!(row.post_date, new_date);
41
        assert_eq!(fx.tx_tags(tx_id).await?, vec![pair("note", "later")]);
42
    }
43
}
44

            
45
#[local_db_sqlx_test]
46
async fn test_edit_concurrent_with_set_transaction_tag(pool: PgPool) -> Result<(), anyhow::Error> {
47
    let fx = fixture().await?;
48
    for _ in 0..ROUNDS * 4 {
49
        let tx_id = fx.create_tx(None).await?;
50
        let edit = UpdateTransaction::new()
51
            .user_id(fx.user.id)
52
            .transaction_id(tx_id)
53
            .splits(fx.splits(tx_id, 60))
54
            .tags(tag_map(&[("category", "shared"), ("zone", "z")]))
55
            .run();
56
        let set = SetTransactionTag::new()
57
            .user_id(fx.user.id)
58
            .transaction_id(tx_id)
59
            .tag_name("category".to_string())
60
            .tag_value("shared".to_string())
61
            .run();
62
        let (a, b) = tokio::join!(edit, set);
63
        a?;
64
        b?;
65
        assert!(
66
            fx.tx_tags(tx_id)
67
                .await?
68
                .contains(&pair("category", "shared"))
69
        );
70
    }
71
}
72

            
73
#[local_db_sqlx_test]
74
async fn test_create_and_edit_sharing_tags_do_not_deadlock(
75
    pool: PgPool,
76
) -> Result<(), anyhow::Error> {
77
    let fx = fixture().await?;
78
    let existing = fx.create_tx(None).await?;
79
    for _ in 0..ROUNDS * 4 {
80
        let new_id = Uuid::new_v4();
81
        let new_splits = fx.splits(new_id, 10);
82
        let ids: Vec<Uuid> = new_splits
83
            .iter()
84
64
            .filter_map(|e| match e {
85
64
                FinanceEntity::Split(s) => Some(s.id),
86
                _ => None,
87
64
            })
88
            .collect();
89
        let descending: Vec<(Uuid, Tag)> = shared_tags()
90
            .into_iter()
91
            .rev()
92
160
            .map(|(n, v)| (ids[0], tag(n, v)))
93
            .collect();
94
        let now = Utc::now();
95
        let create = CreateTransaction::new()
96
            .user_id(fx.user.id)
97
            .splits(new_splits)
98
            .id(new_id)
99
            .post_date(now)
100
            .enter_date(now)
101
            .split_tags(descending)
102
            .run();
103
        let edit = UpdateTransaction::new()
104
            .user_id(fx.user.id)
105
            .transaction_id(existing)
106
            .tags(tag_map(&shared_tags()))
107
            .run();
108
        let (a, b) = tokio::join!(create, edit);
109
        a?;
110
        b?;
111
        assert_eq!(
112
            fx.split_tag_values(ids[0]).await?.len(),
113
            shared_tags().len()
114
        );
115
    }
116
}
117

            
118
#[local_db_sqlx_test]
119
async fn test_delete_and_edit_sharing_tags_do_not_deadlock(
120
    pool: PgPool,
121
) -> Result<(), anyhow::Error> {
122
    let fx = fixture().await?;
123
    let kept = fx.create_tx(None).await?;
124
    for _ in 0..ROUNDS * 4 {
125
        let doomed = fx.create_tx(None).await?;
126
        let seeded = fx.splits(doomed, 10);
127
        let first = first_split_id(&seeded)?;
128
        UpdateTransaction::new()
129
            .user_id(fx.user.id)
130
            .transaction_id(doomed)
131
            .splits(seeded)
132
            .tags(tag_map(&shared_tags()))
133
            .split_tags(
134
                shared_tags()
135
                    .into_iter()
136
160
                    .map(|(n, v)| (first, tag(n, v)))
137
                    .collect(),
138
            )
139
            .run()
140
            .await?;
141

            
142
        let delete = DeleteTransaction::new()
143
            .user_id(fx.user.id)
144
            .transaction_id(doomed)
145
            .run();
146
        let edit = UpdateTransaction::new()
147
            .user_id(fx.user.id)
148
            .transaction_id(kept)
149
            .tags(tag_map(&shared_tags()))
150
            .run();
151
        let (a, b) = tokio::join!(delete, edit);
152
        a?;
153
        b?;
154
        assert_eq!(fx.tx_tags(kept).await?.len(), shared_tags().len());
155
    }
156
}
157

            
158
#[local_db_sqlx_test]
159
async fn test_concurrent_edits_of_other_transactions_sharing_tags(
160
    pool: PgPool,
161
) -> Result<(), anyhow::Error> {
162
    let fx = fixture().await?;
163
    let first = fx.create_tx(None).await?;
164
    let second = fx.create_tx(None).await?;
165
    let shared = [("a", "1"), ("b", "2"), ("c", "3"), ("d", "4"), ("e", "5")];
166
10
    let edit = |id| {
167
10
        UpdateTransaction::new()
168
10
            .user_id(fx.user.id)
169
10
            .transaction_id(id)
170
10
            .tags(tag_map(&shared))
171
10
            .run()
172
10
    };
173

            
174
    for _ in 0..5 {
175
        let (a, b) = tokio::join!(edit(first), edit(second));
176
        a?;
177
        b?;
178
    }
179

            
180
    assert_eq!(fx.tx_tags(first).await?.len(), shared.len());
181
    assert_eq!(fx.tx_tags(second).await?.len(), shared.len());
182
}