Skip to main content

check

Function check 

Source
pub fn check<F>(f: F) -> Report
where F: Fn(),
Expand description

Runs f once to count allocations and then once per observed allocation, failing one allocation attempt per run.

This is equivalent to Check::new().run(f).

ยงPanics

Panics if another check is already active in the process. Use try_check to handle that case explicitly.

Examples found in repository?
examples/scenarios.rs (line 68)
67fn strict_success() {
68    let report = alloc_chaos::check(checked_packet_builder);
69
70    print_report("strict exhaustive check", &report);
71    report.assert_success();
72}
73
74fn bounded_diagnostic_run() {
75    let report = alloc_chaos::Check::new()
76        .max_failures(1)
77        .stop_on_failure(true)
78        .run(checked_packet_builder);
79
80    print_report("bounded diagnostic run", &report);
81    assert!(report.is_truncated());
82    assert!(!report.is_success());
83}
84
85fn range_diagnostic_run() {
86    let report = alloc_chaos::Check::new()
87        .failure_range(1..2)
88        .run(checked_packet_builder);
89
90    print_report("selected failure range", &report);
91    assert!(report.is_truncated());
92    assert_eq!(report.tested_failures(), 1);
93    assert!(!report.is_success());
94}
95
96fn reproduce_one_failure_and_show_metadata() {
97    let full_report = alloc_chaos::check(checked_packet_builder);
98    full_report.assert_success();
99
100    let target = full_report
101        .attempts()
102        .first()
103        .map(alloc_chaos::Attempt::target_allocation)
104        .expect("packet builder should allocate during the baseline run");
105
106    let report = alloc_chaos::Check::new()
107        .only_failure(target)
108        .run(checked_packet_builder);
109
110    print_report("single-target reproduction", &report);
111    print_allocation_metadata(&report);
112
113    assert!(report.is_truncated());
114    assert_eq!(report.tested_failures(), 1);
115    assert!(report.attempts()[0].is_success());
116    assert!(!report.is_success());
117}
More examples
Hide additional examples
examples/try_reserve.rs (lines 26-30)
25fn main() {
26    let report = alloc_chaos::check(|| {
27        if let Ok(bytes) = build_payload(4096) {
28            assert_eq!(bytes.len(), 4096)
29        }
30    });
31
32    println!("{report}");
33    report.assert_success();
34}