diff --git a/crates/voelin-core/tests/voice_features.rs b/crates/voelin-core/tests/voice_features.rs index 6b4cfb8..69f7d99 100644 --- a/crates/voelin-core/tests/voice_features.rs +++ b/crates/voelin-core/tests/voice_features.rs @@ -3,6 +3,7 @@ //! in the client database). The round trips against real servers are in //! `scripts/it-smoke.sh` (`voelinctl engine`). +use std::path::{Path, PathBuf}; use std::time::Duration; use tokio::sync::broadcast; @@ -22,6 +23,29 @@ async fn next(rx: &mut broadcast::Receiver, mut f: impl FnMut(Event) - .expect("event in time") } +/// Wait until nothing has the SQLite database at `path` open any more, as when +/// the process that had it has ended: SQLite deletes the write-ahead log +/// (`-wal`) when the last connection closes. +/// +/// A dropped `Engine` is not that yet. Its task ends later, the history writer +/// is joined there, and closing the connection checkpoints the log under an +/// exclusive lock, with fsyncs. A connection opened meanwhile waits for the +/// lock only as long as the busy timeout (5 s) and then fails with +/// `DatabaseBusy`, which a loaded disk makes happen (with every fsync taking +/// 6 s it happens every time). +async fn closed(path: &Path) { + let mut wal = path.as_os_str().to_owned(); + wal.push("-wal"); + let wal = PathBuf::from(wal); + tokio::time::timeout(Duration::from_secs(120), async { + while wal.exists() { + tokio::time::sleep(Duration::from_millis(10)).await; + } + }) + .await + .expect("the engine's database connection closed"); +} + #[tokio::test(flavor = "multi_thread")] async fn requests_without_voice_fail_with_their_ids() { let engine = Engine::start(); @@ -165,6 +189,7 @@ async fn contacts_are_kept_in_the_database() { engine.history().flush(); } // A new engine attaches the database later: the contacts come back. + closed(&path).await; let engine = Engine::start(); let mut rx = engine.subscribe(); engine.send(Command::AttachHistory(History::open(&path).unwrap()));