1
//! Tag setters behind a stalled key holder report the busy condition.
2

            
3
use super::lock_wait::hold_key;
4
use super::*;
5
use crate::command::CmdError;
6
use crate::command::transaction::SetTransactionTag;
7
use sqlx::Connection;
8

            
9
2
async fn assert_busy_behind_held_key<T: std::fmt::Debug>(
10
2
    fx: &Fixture,
11
2
    write: impl Future<Output = Result<T, CmdError>>,
12
2
) -> anyhow::Result<()> {
13
2
    let mut conn = fx.user.get_connection().await?;
14
2
    let mut holder = conn.begin().await?;
15
2
    hold_key(&mut holder, "memo", "m").await?;
16
2
    let written = write.await;
17
2
    holder.commit().await?;
18
2
    let err = written.expect_err("the setter must time out on the held key");
19
2
    assert!(err.busy().is_some(), "{err:?}");
20
2
    assert_eq!(fx.tag_rows_with_value("m").await?, 0);
21
2
    Ok(())
22
2
}
23

            
24
#[local_db_sqlx_test]
25
async fn set_split_tag_behind_a_held_key_is_busy(pool: PgPool) -> anyhow::Result<()> {
26
    let fx = fixture().await?;
27
    let tx_id = fx.create_tx(None).await?;
28
    let split = fx.split_ids(tx_id).await?[0];
29
    let set = SetSplitTag::new()
30
        .user_id(fx.user.id)
31
        .split_id(split)
32
        .tag_name("memo".to_string())
33
        .tag_value("m".to_string())
34
        .run();
35
    assert_busy_behind_held_key(&fx, set).await?;
36
}
37

            
38
#[local_db_sqlx_test]
39
async fn set_transaction_tag_behind_a_held_key_is_busy(pool: PgPool) -> anyhow::Result<()> {
40
    let fx = fixture().await?;
41
    let tx_id = fx.create_tx(None).await?;
42
    let set = SetTransactionTag::new()
43
        .user_id(fx.user.id)
44
        .transaction_id(tx_id)
45
        .tag_name("memo".to_string())
46
        .tag_value("m".to_string())
47
        .run();
48
    assert_busy_behind_held_key(&fx, set).await?;
49
}
50

            
51
2
async fn set_split_tag(
52
2
    fx: &Fixture,
53
2
    split: Uuid,
54
2
    value: &str,
55
2
) -> Result<Option<CmdResult>, CmdError> {
56
2
    SetSplitTag::new()
57
2
        .user_id(fx.user.id)
58
2
        .split_id(split)
59
2
        .tag_name("memo".to_string())
60
2
        .tag_value(value.to_string())
61
2
        .run()
62
2
        .await
63
2
}
64

            
65
#[local_db_sqlx_test]
66
async fn a_blank_split_tag_value_is_an_argument_error(pool: PgPool) -> anyhow::Result<()> {
67
    let fx = fixture().await?;
68
    let tx_id = fx.create_tx(None).await?;
69
    let split = fx.split_ids(tx_id).await?[0];
70
    let err = set_split_tag(&fx, split, "  ")
71
        .await
72
        .expect_err("blank value");
73
    assert!(matches!(err, CmdError::Args(_)), "{err:?}");
74
    assert!(err.busy().is_none());
75
}
76

            
77
#[local_db_sqlx_test]
78
async fn a_missing_split_is_not_found(pool: PgPool) -> anyhow::Result<()> {
79
    let fx = fixture().await?;
80
    let err = set_split_tag(&fx, Uuid::new_v4(), "m")
81
        .await
82
        .expect_err("missing split");
83
    assert!(matches!(err, CmdError::NotFound(_)), "{err:?}");
84
}