Skip to main content

walletkit_sqlite/
test_utils.rs

1//! Shared test helpers for crates built on `walletkit-sqlite`.
2
3use std::sync::OnceLock;
4
5use crate::Connection;
6
7/// Ensures sqlite3mc's global codec registration is complete before any test
8/// body runs.
9///
10/// sqlite3mc registers its cipher implementations the first time
11/// `sqlite3_open_v2` is called. When the test binary runs all tests in
12/// parallel threads, two threads can race inside that one-time
13/// initialization and one of them sees an "unknown cipher 'chacha20'"
14/// error even though chacha20 is compiled in.
15///
16/// Calling this at the start of every test ensures exactly one thread
17/// performs the first open (all others block on the `OnceLock`) so that
18/// by the time any test-specific code runs, sqlite3mc is fully initialized.
19///
20/// # Panics
21///
22/// Panics if sqlite3mc cannot open an in-memory database.
23pub fn init_sqlite() {
24    static INIT: OnceLock<()> = OnceLock::new();
25    INIT.get_or_init(|| {
26        drop(
27            Connection::open(std::path::Path::new(":memory:"), false)
28                .expect("sqlite3mc pre-init"),
29        );
30    });
31}