use std::io;
use proptest::prelude::*;
use rstest::rstest;
use super::{
super::evaluate_memory_pressure_with_retained,
ExpectedAction,
TestResult,
action_matches,
budgets,
custom_budgets,
state_with_buffered_bytes,
};
use crate::app::MemoryBudgets;
#[rstest]
#[case::below_the_soft_limit((0, 0, 79), ExpectedAction::Continue)]
#[case::at_the_soft_limit((0, 0, 80), ExpectedAction::Pause)]
#[case::at_the_hard_limit((0, 0, 100), ExpectedAction::Pause)]
#[case::past_the_hard_limit((0, 0, 101), ExpectedAction::Abort)]
#[case::mixed_retained_total_at_the_soft_limit((40, 20, 20), ExpectedAction::Pause)]
#[case::mixed_retained_total_past_the_hard_limit((40, 20, 41), ExpectedAction::Abort)]
fn retained_bytes_contribute_to_pressure(
#[case] bytes: (usize, usize, usize),
#[case] expected: ExpectedAction,
budgets: TestResult<MemoryBudgets>,
) -> TestResult {
let (assembly_bytes, fragment_bytes, retained_bytes) = bytes;
let state = state_with_buffered_bytes(assembly_bytes)?;
let action = evaluate_memory_pressure_with_retained(
(assembly_bytes != 0).then_some(&state),
Some(budgets?),
fragment_bytes,
retained_bytes,
);
if !action_matches(&action, &expected.to_representative()) {
return Err(io::Error::other(format!(
concat!(
"assembly={}, fragment={}, retained={}: ",
"unexpected pressure action {:?}"
),
assembly_bytes, fragment_bytes, retained_bytes, action
))
.into());
}
Ok(())
}
proptest! {
#[test]
fn pressure_depends_on_the_retained_total_not_its_partition(
assembly in 0usize..=60,
fragment in 0usize..=60,
parked in 0usize..=60,
) {
let budgets = custom_budgets(1024, 100, 100).expect("budgets are non-zero");
let total = assembly.saturating_add(fragment).saturating_add(parked);
let state = state_with_buffered_bytes(assembly).expect("assembly state builds");
let consolidated = state_with_buffered_bytes(total).expect("assembly state builds");
let partitioned = evaluate_memory_pressure_with_retained(
(assembly != 0).then_some(&state),
Some(budgets),
fragment,
parked,
);
let whole = evaluate_memory_pressure_with_retained(
(total != 0).then_some(&consolidated),
Some(budgets),
0,
0,
);
prop_assert_eq!(partitioned, whole);
}
}