1
use super::{MAX_ELAB_WORK, SharedBudget};
2

            
3
#[test]
4
1
fn charges_accumulate_and_stay_within_budget() {
5
1
    let budget = SharedBudget::new();
6
1
    budget.charge(10).expect("well within budget");
7
1
    budget.charge(20).expect("still within budget");
8
1
    assert_eq!(budget.charged(), 30);
9
1
}
10

            
11
#[test]
12
1
fn charge_exceeding_the_ceiling_is_a_structured_error() {
13
1
    let budget = SharedBudget::new();
14
1
    let err = budget
15
1
        .charge(MAX_ELAB_WORK + 1)
16
1
        .expect_err("a single charge past the ceiling must fail");
17
1
    assert!(matches!(err, crate::error::Error::Resource(_)));
18
1
    assert!(format!("{err}").contains("compile-time work budget exceeded"));
19
1
}
20

            
21
#[test]
22
1
fn charge_is_kept_even_once_it_exhausts_the_budget() {
23
1
    let budget = SharedBudget::new();
24
1
    budget
25
1
        .charge(MAX_ELAB_WORK)
26
1
        .expect("exactly at the ceiling");
27
1
    let err = budget.charge(1).expect_err("one more unit must fail");
28
1
    assert!(matches!(err, crate::error::Error::Resource(_)));
29
    // The failed charge is not refunded: the counter still reflects it.
30
1
    assert_eq!(budget.charged(), MAX_ELAB_WORK + 1);
31
1
}
32

            
33
#[test]
34
1
fn shared_budget_charges_are_visible_across_clones() {
35
1
    let budget = SharedBudget::new();
36
1
    let probe_clone = budget.clone();
37
1
    probe_clone.charge(500).expect("probe charge succeeds");
38
1
    drop(probe_clone);
39
    // The clone's charge survives even though the clone itself was
40
    // discarded — a probe's work is never refunded.
41
1
    assert_eq!(budget.charged(), 500);
42
1
}
43

            
44
#[test]
45
1
fn shared_budget_exhausts_from_repeated_small_charges() {
46
1
    let budget = SharedBudget::new();
47
1
    let mut last = Ok(());
48
1
    for _ in 0..(MAX_ELAB_WORK + 10) {
49
1048577
        last = budget.charge(1);
50
1048577
        if last.is_err() {
51
1
            break;
52
1048576
        }
53
    }
54
1
    assert!(
55
1
        last.is_err(),
56
        "repeated charging must eventually exhaust the budget"
57
    );
58
1
}
59

            
60
#[test]
61
1
fn concurrent_charges_from_multiple_threads_never_lose_a_unit() {
62
    // The compare-exchange loop must serialize every charge exactly once,
63
    // even under genuine cross-thread contention — not just the
64
    // single-threaded compiler use this budget actually sees.
65
    use std::sync::Arc;
66
    use std::thread;
67

            
68
1
    let budget = Arc::new(SharedBudget::new());
69
1
    let handles: Vec<_> = (0..8)
70
8
        .map(|_| {
71
8
            let budget = Arc::clone(&budget);
72
8
            thread::spawn(move || {
73
8
                for _ in 0..1000 {
74
8000
                    let _ = budget.charge(1);
75
                }
76
8
            })
77
8
        })
78
        .collect();
79
1
    for handle in handles {
80
8
        handle.join().expect("charging thread must not panic");
81
    }
82
1
    assert_eq!(budget.charged(), 8000);
83
1
}