diff --git a/crates/voelin-store/tests/keystore.rs b/crates/voelin-store/tests/keystore.rs index 6182183..cb1b595 100644 --- a/crates/voelin-store/tests/keystore.rs +++ b/crates/voelin-store/tests/keystore.rs @@ -1745,8 +1745,18 @@ fn a_reader_that_blocks_the_checkpoint_costs_one_checkpoint_and_starves_no_write let rows: i64 = reader.query_row("SELECT count(*) FROM identities", [], |r| r.get(0)).unwrap(); assert_eq!(rows, 4); + /// Stops the writer when dropped, so that a failed assertion of the test does + /// not leave the scope waiting for it. + struct StopsTheWriter<'a>(&'a AtomicBool); + impl Drop for StopsTheWriter<'_> { + fn drop(&mut self) { + self.0.store(true, Ordering::Relaxed); + } + } let stop = AtomicBool::new(false); - let (done, elapsed, writes, errors, lines) = std::thread::scope(|scope| { + let written = AtomicUsize::new(0); + let (done, elapsed, errors, lines) = std::thread::scope(|scope| { + let _stops = StopsTheWriter(&stop); let writer = scope.spawn(|| { // Its own connection, set up as the app's are (`Store::open`: the log // and `synchronous = NORMAL`, so that a commit does not wait for the @@ -1755,30 +1765,47 @@ fn a_reader_that_blocks_the_checkpoint_costs_one_checkpoint_and_starves_no_write db.pragma_update(None, "synchronous", "NORMAL").unwrap(); db.busy_timeout(Duration::from_secs(60)).unwrap(); let sql = "INSERT OR REPLACE INTO settings (key, value) VALUES ('probe', ?1)"; - let (mut writes, mut errors) = (0, Vec::new()); + let mut errors = Vec::new(); while !stop.load(Ordering::Relaxed) { - match db.execute(sql, [writes.to_string()]) { - Ok(_) => writes += 1, + let began = Instant::now(); + match db.execute(sql, [written.load(Ordering::Relaxed).to_string()]) { + Ok(_) => { + written.fetch_add(1, Ordering::Relaxed); + } Err(error) => errors.push(error.to_string()), } - std::thread::sleep(Duration::from_millis(5)); + // It pauses ten times as long as the write took, and 5 ms at least. + // Under disk pressure a commit takes 100 ms and more, and a writer + // that paused 5 ms after each would hold the writer lock nearly all + // the time and keep the sign-out, whose busy timeout is the default + // five seconds, from getting it (`DatabaseBusy`). + let wake = Instant::now() + (began.elapsed() * 10).max(Duration::from_millis(5)); + while Instant::now() < wake && !stop.load(Ordering::Relaxed) { + std::thread::sleep(Duration::from_millis(1)); + } } - (writes, errors) + errors }); + // The sign-out starts when the writer is up and has written, not when + // the thread was spawned: a thread of a busy machine can start late. + let deadline = Instant::now() + Duration::from_secs(60); + while written.load(Ordering::Relaxed) == 0 { + assert!(Instant::now() < deadline, "the writer never ran"); + std::thread::sleep(Duration::from_millis(1)); + } let started = Instant::now(); let (done, lines) = support::at_trace(|| { store.sign_out(&binding().account, DeviceKeyChoice::Forget(key().uid())) }); let elapsed = started.elapsed(); stop.store(true, Ordering::Relaxed); - let (writes, errors) = writer.join().unwrap(); - (done.unwrap(), elapsed, writes, errors, lines) + let errors = writer.join().unwrap(); + (done.unwrap(), elapsed, errors, lines) }); // Four removals: the two copies, the device key the user forgot, the record. assert_eq!(done.removed.len(), 4, "{done:?}"); assert!(store.list().unwrap().is_empty()); - assert!(writes > 0, "the writer never ran"); assert!(errors.is_empty(), "a concurrent writer failed: {errors:?}"); // One checkpoint for the four removals: each one that is blocked says so. let blocked = support::blocked_checkpoints(&lines);