#[path = "common/harness.rs"]
mod harness;
use harness::TestHarness;
use macrame::prelude::*;
const T1: &str = "2026-01-01T00:00:00.000000Z";
const DEFAULT_THRESHOLD_BYTES: u64 = 1000 * 4096;
fn wal_bytes(harness: &TestHarness) -> u64 {
let mut wal = harness.db_path.clone().into_os_string();
wal.push("-wal");
std::fs::metadata(std::path::PathBuf::from(wal))
.map(|m| m.len())
.unwrap_or(0)
}
async fn write_a_lot(db: &Database) {
let filler = "x".repeat(1024);
let concepts: Vec<_> = (0..3000)
.map(|i| {
ConceptUpsert::new(format!("c{i}"), format!("Concept {i}"))
.content(filler.clone())
.valid_from(T1)
})
.collect();
db.write_concepts(concepts).await.unwrap();
}
async fn open(harness: &TestHarness, wal_autocheckpoint: WalCheckpointPolicy) -> Database {
Database::open_tuned(
&harness.db_path,
Tuning {
cadence: CadencePolicy::Disabled,
wal_autocheckpoint,
..Default::default()
},
)
.await
.unwrap()
}
#[tokio::test]
async fn the_default_still_bounds_the_wal_without_being_asked() {
let harness = TestHarness::new();
let db = open(&harness, WalCheckpointPolicy::default()).await;
write_a_lot(&db).await;
let wal = wal_bytes(&harness);
assert!(
wal < DEFAULT_THRESHOLD_BYTES * 3,
"the WAL reached {wal} bytes with the default policy — the automatic \
checkpointer is not running, which means the default changed"
);
db.close().await.unwrap();
}
#[tokio::test]
async fn a_disabled_autocheckpoint_lets_the_wal_grow() {
let harness = TestHarness::new();
let db = open(&harness, WalCheckpointPolicy::Disabled).await;
write_a_lot(&db).await;
let wal = wal_bytes(&harness);
assert!(
wal > DEFAULT_THRESHOLD_BYTES * 3,
"the WAL is only {wal} bytes with autocheckpoint disabled — something \
is still checkpointing, so the pragma did not reach the writer"
);
let report = db.checkpoint().await.unwrap();
assert!(report.is_complete());
assert_eq!(wal_bytes(&harness), 0);
db.close().await.unwrap();
}
#[tokio::test]
async fn an_explicit_threshold_bounds_the_wal_below_the_default() {
let harness = TestHarness::new();
let db = open(&harness, WalCheckpointPolicy::EveryPages(64)).await;
write_a_lot(&db).await;
let wal = wal_bytes(&harness);
assert!(
wal < DEFAULT_THRESHOLD_BYTES,
"a 64-page threshold left {wal} bytes of WAL, which is more than the \
1,000-page default would have"
);
db.close().await.unwrap();
}
#[tokio::test]
async fn a_bulk_import_with_the_checkpointer_off_reclaims_and_keeps_its_rows() {
const EDGES: usize = 3_000;
let harness = TestHarness::new();
let db = open(&harness, WalCheckpointPolicy::Disabled).await;
let concepts: Vec<_> = (0..=EDGES)
.map(|i| ConceptUpsert::new(format!("c{i}"), format!("C{i}")).valid_from(T1))
.collect();
db.write_concepts(concepts).await.unwrap();
let edges: Vec<_> = (0..EDGES)
.map(|i| {
EdgeAssertion::new(format!("c{i}"), format!("c{}", i + 1), "LINKS")
.valid_from(T1)
.valid_to("9999-12-31T23:59:59.999999Z")
})
.collect();
let written = db.bulk_import(edges).await.unwrap();
assert_eq!(written, EDGES);
let grown = wal_bytes(&harness);
assert!(
grown > DEFAULT_THRESHOLD_BYTES,
"the WAL is {grown} bytes after a {EDGES}-edge import with the \
checkpointer disabled — something is still checkpointing, so this \
test is not measuring the deferred-cost path it claims to"
);
let report = db.checkpoint().await.unwrap();
assert!(!report.busy, "the checkpoint gave up: {report:?}");
assert!(
report.checkpointed_frames > 0,
"no frames moved: {report:?}"
);
assert!(report.is_complete(), "{report:?}");
assert_eq!(wal_bytes(&harness), 0);
let conn = db.diagnostic_conn().await.unwrap();
let surviving: i64 = conn
.query("SELECT COUNT(*) FROM links_current", ())
.await
.unwrap()
.next()
.await
.unwrap()
.unwrap()
.get(0)
.unwrap();
assert_eq!(
surviving,
EDGES as i64,
"the WAL was reclaimed and {} of {EDGES} edges went with it",
EDGES as i64 - surviving
);
db.close().await.unwrap();
}
#[test]
fn a_zero_threshold_is_kept_as_the_caller_wrote_it() {
let tuning = Tuning {
wal_autocheckpoint: WalCheckpointPolicy::EveryPages(0),
..Default::default()
};
assert_eq!(
tuning.wal_autocheckpoint,
WalCheckpointPolicy::EveryPages(0),
"a computed zero was rewritten to Disabled, which hides a caller's bug"
);
assert_ne!(
WalCheckpointPolicy::EveryPages(0),
WalCheckpointPolicy::Disabled
);
}