diff --git a/crates/voelin-core/tests/common/mod.rs b/crates/voelin-core/tests/common/mod.rs new file mode 100644 index 0000000..57dd5fb --- /dev/null +++ b/crates/voelin-core/tests/common/mod.rs @@ -0,0 +1,28 @@ +//! What more than one integration test of this crate needs (a test binary +//! that uses it declares `mod common;`). + +use std::path::{Path, PathBuf}; +use std::time::Duration; + +/// 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). +pub 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"); +} diff --git a/crates/voelin-core/tests/gateway.rs b/crates/voelin-core/tests/gateway.rs index 65e37ac..a623282 100644 --- a/crates/voelin-core/tests/gateway.rs +++ b/crates/voelin-core/tests/gateway.rs @@ -24,6 +24,8 @@ use voelin_gateway_proto::{ }; use voelin_model::{ChatMessage, ChatTarget, PresenceSnapshot}; +mod common; + const LOBBY: ChatTarget = ChatTarget::Channel(1); const ME: &str = "me-uid"; @@ -529,6 +531,8 @@ async fn history_on_open_paging_and_offline_messages() { assert_eq!(texts(&stored), ["m1", "m2", "m3", "m4", "m5", "while away 1", "while away 2"]); history.flush(); drop((engine, history)); + // Dropping the engine does not close the database yet (see `closed`). + common::closed(&path).await; // After a restart, with the gateway down: the stored page, found by the // gateway's URL, which is remembered with the server's id. diff --git a/crates/voelin-core/tests/voice_features.rs b/crates/voelin-core/tests/voice_features.rs index 69f7d99..e869dea 100644 --- a/crates/voelin-core/tests/voice_features.rs +++ b/crates/voelin-core/tests/voice_features.rs @@ -3,7 +3,6 @@ //! 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; @@ -11,6 +10,10 @@ use voelin_core::settings::Settings; use voelin_core::{Command, Contact, DownloadTo, Engine, Event, History, Relation, TransferState}; use voelin_model::FileRef; +mod common; + +use common::closed; + async fn next(rx: &mut broadcast::Receiver, mut f: impl FnMut(Event) -> Option) -> T { tokio::time::timeout(Duration::from_secs(10), async { loop { @@ -23,29 +26,6 @@ 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();