diff --git a/src/library_import/tests/publisher.rs b/src/library_import/tests/publisher.rs index 46329ae..afcf80f 100644 --- a/src/library_import/tests/publisher.rs +++ b/src/library_import/tests/publisher.rs @@ -339,6 +339,26 @@ async fn assert_consistent(pool: &PgPool, playlist_id: i64) { "register and playlist row of {content_id}" ); } + // Every library track in the playlist has a present register, whatever + // the register's `local_track_id` says. + let tracks: Vec<(i64, Option)> = sqlx::query_as( + "SELECT pt.track_id, r.present + FROM furumusic__playlist_track pt + JOIN furumusic__track t ON t.id = pt.track_id + JOIN furumusic__federation_content_id_cache c ON c.media_file_id = t.audio_file_id + LEFT JOIN furumusic__fed_state_playlist_item r + ON r.user_id = $1 AND r.playlist_id = $3 AND r.content_id = c.content_id + WHERE pt.playlist_id = $2", + ) + .bind(USER) + .bind(playlist_id) + .bind(devices::local_playlist_sync_id_for(USER, playlist_id)) + .fetch_all(pool) + .await + .unwrap(); + for (track_id, present) in tracks { + assert_eq!(present, Some(true), "register of playlist track {track_id}"); + } } /// A task started inside a publication transaction, awaited after it. @@ -1249,6 +1269,72 @@ async fn library_import_registers_and_playlist_rows_stay_consistent_under_concur fixture.drop().await; } +/// An applied operation changes the register and the playlist row in one +/// transaction: while its playlist row waits for a lock, the register still +/// shows the old state to everyone else. Writing the register on its own +/// first would show the new one here. +#[tokio::test] +#[ignore = "requires FURU_LIBRARY_IMPORT_TEST_DATABASE_URL pointing to a disposable PostgreSQL database"] +async fn library_import_sync_register_and_playlist_row_commit_together() { + let (_serial, fixture) = setup().await; + let pool = &fixture.pool; + library(pool, 1).await; + trust_remote(pool).await; + let import_id = matched_import(pool, "atomic", &[("Atomic", vec![Entry::Local(1)])]).await; + publish(pool, 881).await; + let road = target(pool, import_id, 0).await.unwrap(); + assert!(register(pool, road, 1).await.unwrap().0); + let base = now_ms() + 1_000_000; + + // A removal waits for the playlist row. + let mut blocker = pool.begin().await.unwrap(); + sqlx::query( + "SELECT 1 FROM furumusic__playlist_track WHERE playlist_id = $1 AND track_id = 1001 FOR UPDATE", + ) + .bind(road) + .execute(&mut *blocker) + .await + .unwrap(); + let task_pool = pool.clone(); + let removal = tokio::spawn(async move { + apply(&task_pool, remote_op(REMOTE, 1, base, removed(road, 1))).await; + }); + wait_for_lock(pool).await; + assert!(register(pool, road, 1).await.unwrap().0); + assert_eq!(rows(pool, road).await, [(1001, 0)]); + blocker.rollback().await.unwrap(); + removal.await.unwrap(); + assert!(!register(pool, road, 1).await.unwrap().0); + assert!(rows(pool, road).await.is_empty()); + assert_consistent(pool, road).await; + + // An addition waits for the playlist table. + let mut blocker = pool.begin().await.unwrap(); + sqlx::query("LOCK TABLE furumusic__playlist_track IN SHARE MODE") + .execute(&mut *blocker) + .await + .unwrap(); + let task_pool = pool.clone(); + let addition = tokio::spawn(async move { + apply( + &task_pool, + remote_op(REMOTE, 2, base + 1, added(road, 1, 4)), + ) + .await; + }); + wait_for_lock(pool).await; + assert!(!register(pool, road, 1).await.unwrap().0); + assert!(rows(pool, road).await.is_empty()); + blocker.rollback().await.unwrap(); + addition.await.unwrap(); + let (present, position, _) = register(pool, road, 1).await.unwrap(); + assert!(present); + assert_eq!(position, 4); + assert_eq!(rows(pool, road).await, [(1001, 4)]); + assert_consistent(pool, road).await; + fixture.drop().await; +} + /// Library cleanup deletes playlist rows and then detaches registers, the /// opposite order of an applied operation. When PostgreSQL breaks the /// deadlock by aborting the operation, it is applied again.