test: harden claim race test

This commit is contained in:
2026-10-25 23:40:43 +01:00
parent 98ad1a8fe7
commit c31aa033cc
2 changed files with 14 additions and 6 deletions

View File

@@ -10,11 +10,15 @@ use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions};
use super::{DbEventQueueConfig, claim_batch}; use super::{DbEventQueueConfig, claim_batch};
async fn pool_on(path: &std::path::Path) -> SqlitePool { async fn pool_on(path: &std::path::Path) -> SqlitePool {
pool_with_timeout(path, 5).await
}
async fn pool_with_timeout(path: &std::path::Path, busy_secs: u64) -> SqlitePool {
let opts = SqliteConnectOptions::from_str(&format!("sqlite://{}", path.display())) let opts = SqliteConnectOptions::from_str(&format!("sqlite://{}", path.display()))
.unwrap() .unwrap()
.create_if_missing(true) .create_if_missing(true)
.journal_mode(SqliteJournalMode::Wal) .journal_mode(SqliteJournalMode::Wal)
.busy_timeout(std::time::Duration::from_secs(5)); .busy_timeout(std::time::Duration::from_secs(busy_secs));
SqlitePoolOptions::new() SqlitePoolOptions::new()
.max_connections(4) .max_connections(4)
.connect_with(opts) .connect_with(opts)
@@ -46,7 +50,7 @@ async fn claiming_while_another_connection_writes_does_not_fail() {
let path = dir.path().join("q.db"); let path = dir.path().join("q.db");
let claimer = pool_on(&path).await; let claimer = pool_on(&path).await;
crate::migrate(&claimer).await.unwrap(); crate::migrate(&claimer).await.unwrap();
let writer = pool_on(&path).await; let writer = pool_with_timeout(&path, 30).await;
insert(&claimer, 50).await; insert(&claimer, 50).await;
let writer_task = { let writer_task = {
@@ -65,10 +69,14 @@ async fn claiming_while_another_connection_writes_does_not_fail() {
let cfg = config(5); let cfg = config(5);
let mut claimed = 0; let mut claimed = 0;
while !writer_task.is_finished() { while !writer_task.is_finished() {
claimed += claim_batch(&claimer, &cfg) let batch = claim_batch(&claimer, &cfg)
.await .await
.expect("claim must not fail while another connection writes") .expect("claim must not fail while another connection writes");
.len(); if batch.is_empty() {
// an empty UPDATE still takes the write lock; don't starve the writer
tokio::task::yield_now().await;
}
claimed += batch.len();
} }
writer_task.await.unwrap(); writer_task.await.unwrap();
assert!(claimed > 0); assert!(claimed > 0);

View File

@@ -120,7 +120,7 @@ async fn merging_into_a_canonical_movie_that_does_not_exist_yet_succeeds() {
} }
#[tokio::test] #[tokio::test]
async fn merging_into_an_existing_canonical_movie_keeps_its_poster() { async fn merging_into_an_existing_canonical_movie_moves_the_external_id_and_keeps_its_poster() {
let pool = migrated_pool().await; let pool = migrated_pool().await;
let ext = ExternalMetadataId::new("tmdb:603".into()).unwrap(); let ext = ExternalMetadataId::new("tmdb:603".into()).unwrap();
let old_id = MovieId::generate(); let old_id = MovieId::generate();