1
use super::*;
2
use crate::test_support::{CountingPool, ErrorMarks};
3
use scripting::nomiscript::{Compiler, Reader, SymbolTable};
4

            
5
/// Spins until the executor's epoch deadline stops it.
6
const SPIN: &str = "(do () (nil nil))";
7

            
8
1
fn compile(source: &str) -> anyhow::Result<Vec<u8>> {
9
1
    let program = Reader::parse(source).map_err(|err| anyhow::anyhow!("parse: {err}"))?;
10
1
    let mut symbols = SymbolTable::with_builtins();
11
1
    Compiler::new()
12
1
        .compile(&program, &mut symbols)
13
        .map_err(|err| anyhow::anyhow!("compile: {err}"))
14
1
}
15

            
16
2
async fn account(user: &User, name: &str) -> anyhow::Result<Uuid> {
17
2
    match CreateAccount::new()
18
2
        .name(name.to_string())
19
2
        .user_id(user.id)
20
2
        .run()
21
2
        .await?
22
    {
23
2
        Some(CmdResult::Entity(FinanceEntity::Account(account))) => Ok(account.id),
24
        other => anyhow::bail!("account: {other:?}"),
25
    }
26
2
}
27

            
28
1
fn splits(tx_id: Uuid, commodity_id: Uuid, accounts: [Uuid; 2]) -> Vec<FinanceEntity> {
29
1
    [(accounts[0], -100), (accounts[1], 100)]
30
1
        .into_iter()
31
2
        .map(|(account_id, value_num)| {
32
2
            FinanceEntity::Split(Split {
33
2
                id: Uuid::new_v4(),
34
2
                tx_id,
35
2
                account_id,
36
2
                commodity_id,
37
2
                value_num,
38
2
                value_denom: 1,
39
2
                reconcile_state: None,
40
2
                reconcile_date: None,
41
2
                lot_id: None,
42
2
            })
43
2
        })
44
        .collect()
45
1
}
46

            
47
/// The script-failure ERROR is logged once the script ends, and by then the
48
/// pool's only connection has been returned exactly once (the scripts read);
49
/// the store takes and returns it afterwards.
50
#[local_db_sqlx_test]
51
async fn test_create_with_a_slow_script_releases_the_connection_between_phases(
52
    pool: PgPool,
53
) -> anyhow::Result<()> {
54
    let user = USER.get().ok_or_else(|| anyhow::anyhow!("no user"))?;
55
    user.commit().await?;
56
    user.create_script(compile(SPIN)?, None).await?;
57
    let Some(CmdResult::String(commodity)) = CreateCommodity::new()
58
        .symbol("TST".to_string())
59
        .name("Test".to_string())
60
        .user_id(user.id)
61
        .run()
62
        .await?
63
    else {
64
        anyhow::bail!("commodity");
65
    };
66
    let commodity = Uuid::parse_str(&commodity)?;
67
    let accounts = [account(user, "A1").await?, account(user, "A2").await?];
68

            
69
    let single = CountingPool::single(&pool).await?;
70
    let _guard = install_test_pool(single.pool.clone());
71
    let errors = ErrorMarks::of(&single);
72
    let _logs = tracing::subscriber::set_default(errors.subscriber());
73
    let tx_id = Uuid::new_v4();
74
    CreateTransaction::new()
75
        .user_id(user.id)
76
        .id(tx_id)
77
        .post_date(Utc::now())
78
        .enter_date(Utc::now())
79
        .splits(splits(tx_id, commodity, accounts))
80
        .run()
81
        .await?;
82
    assert_eq!(errors.marks(), [1]);
83
    assert_eq!(single.checkouts().await?, 2);
84
}