Lines
85.71 %
Functions
80 %
Branches
100 %
use super::*;
/// A third connection holds the row lock while both edits start, so both have
/// read the transaction before either writes unless the edit locks it first.
#[local_db_sqlx_test]
async fn test_concurrent_edits_do_not_lose_the_date(pool: PgPool) -> Result<(), anyhow::Error> {
use sqlx::Connection;
let fx = fixture().await?;
let new_date = Utc.with_ymd_and_hms(2020, 1, 2, 3, 4, 5).single();
let new_date = new_date.ok_or_else(|| anyhow::anyhow!("date"))?;
for _ in 0..ROUNDS {
let tx_id = fx.create_tx(None).await?;
let mut conn = fx.user.get_connection().await?;
let mut blocker = conn.begin().await?;
sqlx::query_file!("sql/select/transactions/by_id_locked.sql", tx_id)
.fetch_one(&mut *blocker)
.await?;
let set_date = UpdateTransaction::new()
.user_id(fx.user.id)
.transaction_id(tx_id)
.post_date(new_date)
.run();
let set_note = UpdateTransaction::new()
.note("later".to_string())
let release = release_after(wait_for_blocked(&fx, 2), blocker);
let (a, b, r) = tokio::join!(set_date, set_note, release);
a?;
b?;
r?;
let row = sqlx::query_file!("sql/select/transactions/by_id.sql", tx_id)
.fetch_one(&mut *conn)
assert_eq!(row.post_date, new_date);
assert_eq!(fx.tx_tags(tx_id).await?, vec![pair("note", "later")]);
}
async fn test_edit_concurrent_with_set_transaction_tag(pool: PgPool) -> Result<(), anyhow::Error> {
for _ in 0..ROUNDS * 4 {
let edit = UpdateTransaction::new()
.splits(fx.splits(tx_id, 60))
.tags(tag_map(&[("category", "shared"), ("zone", "z")]))
let set = SetTransactionTag::new()
.tag_name("category".to_string())
.tag_value("shared".to_string())
let (a, b) = tokio::join!(edit, set);
assert!(
fx.tx_tags(tx_id)
.await?
.contains(&pair("category", "shared"))
);
async fn test_create_and_edit_sharing_tags_do_not_deadlock(
pool: PgPool,
) -> Result<(), anyhow::Error> {
let existing = fx.create_tx(None).await?;
let new_id = Uuid::new_v4();
let new_splits = fx.splits(new_id, 10);
let ids: Vec<Uuid> = new_splits
.iter()
.filter_map(|e| match e {
FinanceEntity::Split(s) => Some(s.id),
_ => None,
})
.collect();
let descending: Vec<(Uuid, Tag)> = shared_tags()
.into_iter()
.rev()
.map(|(n, v)| (ids[0], tag(n, v)))
let now = Utc::now();
let create = CreateTransaction::new()
.splits(new_splits)
.id(new_id)
.post_date(now)
.enter_date(now)
.split_tags(descending)
.transaction_id(existing)
.tags(tag_map(&shared_tags()))
let (a, b) = tokio::join!(create, edit);
assert_eq!(
fx.split_tag_values(ids[0]).await?.len(),
shared_tags().len()
async fn test_delete_and_edit_sharing_tags_do_not_deadlock(
let kept = fx.create_tx(None).await?;
let doomed = fx.create_tx(None).await?;
let seeded = fx.splits(doomed, 10);
let first = first_split_id(&seeded)?;
UpdateTransaction::new()
.transaction_id(doomed)
.splits(seeded)
.split_tags(
shared_tags()
.map(|(n, v)| (first, tag(n, v)))
.collect(),
)
.run()
let delete = DeleteTransaction::new()
.transaction_id(kept)
let (a, b) = tokio::join!(delete, edit);
assert_eq!(fx.tx_tags(kept).await?.len(), shared_tags().len());
async fn test_concurrent_edits_of_other_transactions_sharing_tags(
let first = fx.create_tx(None).await?;
let second = fx.create_tx(None).await?;
let shared = [("a", "1"), ("b", "2"), ("c", "3"), ("d", "4"), ("e", "5")];
let edit = |id| {
.transaction_id(id)
.tags(tag_map(&shared))
};
for _ in 0..5 {
let (a, b) = tokio::join!(edit(first), edit(second));
assert_eq!(fx.tx_tags(first).await?.len(), shared.len());
assert_eq!(fx.tx_tags(second).await?.len(), shared.len());