use chisel::Chisel;
use chisel::{ChiselError, DrainInsertion, Options};
#[test]
fn set_cache_max_bytes_returns_transaction_in_progress_mid_txn() {
let mut db = Chisel::open_in_memory().unwrap();
db.begin().unwrap();
let err = db.set_cache_max_bytes(16 * 1024 * 1024).unwrap_err();
assert!(matches!(err, ChiselError::TransactionInProgress));
db.rollback().unwrap();
}
#[test]
fn set_cache_max_bytes_succeeds_between_transactions() {
let mut db = Chisel::open_in_memory().unwrap();
db.set_cache_max_bytes(16 * 1024 * 1024).unwrap();
db.begin().unwrap();
let _h = db.allocate(b"x").unwrap();
db.commit().unwrap();
}
#[test]
fn set_spillway_max_bytes_to_zero_succeeds_between_transactions() {
let opts = Options::default()
.cache_max_bytes(32 * 8192)
.spillway_max_bytes(64 * 8192 * 1024);
let mut db = Chisel::open_in_memory_with_options(opts).unwrap();
db.begin().unwrap();
db.allocate(b"seed").unwrap();
db.commit().unwrap();
db.set_spillway_max_bytes(0).unwrap();
db.begin().unwrap();
let mut got_cache_full = false;
for _ in 0..200 {
match db.allocate(&vec![0xABu8; 4096]) {
Ok(_) => {}
Err(ChiselError::CacheFull { .. }) => {
got_cache_full = true;
break;
}
Err(e) => panic!("unexpected error: {e:?}"),
}
}
db.rollback().unwrap();
assert!(
got_cache_full,
"spillway=0 must produce CacheFull when cache overflows, not silently spill"
);
}
#[test]
fn set_drain_insertion_returns_transaction_in_progress_mid_txn() {
let mut db = Chisel::open_in_memory().unwrap();
db.begin().unwrap();
let err = db.set_drain_insertion(DrainInsertion::Mru).unwrap_err();
assert!(matches!(err, ChiselError::TransactionInProgress));
db.rollback().unwrap();
}