Lines
100 %
Functions
Branches
use super::{MAX_ELAB_WORK, SharedBudget};
#[test]
fn charges_accumulate_and_stay_within_budget() {
let budget = SharedBudget::new();
budget.charge(10).expect("well within budget");
budget.charge(20).expect("still within budget");
assert_eq!(budget.charged(), 30);
}
fn charge_exceeding_the_ceiling_is_a_structured_error() {
let err = budget
.charge(MAX_ELAB_WORK + 1)
.expect_err("a single charge past the ceiling must fail");
assert!(matches!(err, crate::error::Error::Resource(_)));
assert!(format!("{err}").contains("compile-time work budget exceeded"));
fn charge_is_kept_even_once_it_exhausts_the_budget() {
budget
.charge(MAX_ELAB_WORK)
.expect("exactly at the ceiling");
let err = budget.charge(1).expect_err("one more unit must fail");
// The failed charge is not refunded: the counter still reflects it.
assert_eq!(budget.charged(), MAX_ELAB_WORK + 1);
fn shared_budget_charges_are_visible_across_clones() {
let probe_clone = budget.clone();
probe_clone.charge(500).expect("probe charge succeeds");
drop(probe_clone);
// The clone's charge survives even though the clone itself was
// discarded — a probe's work is never refunded.
assert_eq!(budget.charged(), 500);
fn shared_budget_exhausts_from_repeated_small_charges() {
let mut last = Ok(());
for _ in 0..(MAX_ELAB_WORK + 10) {
last = budget.charge(1);
if last.is_err() {
break;
assert!(
last.is_err(),
"repeated charging must eventually exhaust the budget"
);
fn concurrent_charges_from_multiple_threads_never_lose_a_unit() {
// The compare-exchange loop must serialize every charge exactly once,
// even under genuine cross-thread contention — not just the
// single-threaded compiler use this budget actually sees.
use std::sync::Arc;
use std::thread;
let budget = Arc::new(SharedBudget::new());
let handles: Vec<_> = (0..8)
.map(|_| {
let budget = Arc::clone(&budget);
thread::spawn(move || {
for _ in 0..1000 {
let _ = budget.charge(1);
})
.collect();
for handle in handles {
handle.join().expect("charging thread must not panic");
assert_eq!(budget.charged(), 8000);