use crate::core::{RunId, Spend, Timestamp};
use crate::quota::{QuotaError, QuotaSettlement, QuotaStore};
use super::conformance::Report;
pub async fn check(store: &dyn QuotaStore, report: &mut Report) {
let at = Timestamp::from_unix_timestamp(1_760_000_000).expect("a valid test instant");
zero_admits_nothing(store, at, report).await;
ceiling_refuses_and_frees(store, at, report).await;
reserving_twice_takes_one_slot(store, at, report).await;
spend_accrues_per_period(store, report).await;
halt(store, report).await;
}
async fn zero_admits_nothing(store: &dyn QuotaStore, at: Timestamp, report: &mut Report) {
report.checked += 1;
let run = RunId::generate();
match store.reserve(run, Some(0), at).await {
Err(QuotaError::TooManyRuns { .. }) => {}
Err(e) => report.record(
"a ceiling of zero admits nothing",
format!("reserving under a zero ceiling failed with `{e}` rather than a refusal"),
),
Ok(()) => {
report.record(
"a ceiling of zero admits nothing",
"a run was admitted under a ceiling of zero — the value an \
operator sets to stop a tenant dead, and the one a limit \
compared inside its counting loop never sees",
);
let _ = store.release(run).await;
}
}
}
async fn ceiling_refuses_and_frees(store: &dyn QuotaStore, at: Timestamp, report: &mut Report) {
let first = RunId::generate();
let second = RunId::generate();
report.checked += 1;
if let Err(e) = store.reserve(first, Some(1), at).await {
report.record(
"a run fits under a ceiling of one",
format!("the first reservation failed: {e}"),
);
return;
}
report.checked += 1;
match store.reserve(second, Some(1), at).await {
Err(QuotaError::TooManyRuns { running, .. }) => {
report.checked += 1;
if running == 0 {
report.record(
"a refusal reports how many runs are executing",
"the refusal said zero runs are executing, which tells an \
operator asking why they are throttled precisely nothing",
);
}
}
Err(e) => report.record(
"a tenant at its ceiling is refused",
format!("failed with `{e}` rather than reporting the ceiling"),
),
Ok(()) => report.record(
"a tenant at its ceiling is refused",
"a second run was admitted past a ceiling of one, so the ceiling \
bounds nothing",
),
}
report.checked += 1;
if let Err(e) = store
.settle(&QuotaSettlement {
run: first,
epoch: 1,
period: None,
spend: Spend::default(),
release_slot: true,
})
.await
{
report.record("settling and releasing a slot", format!("{e}"));
return;
}
report.checked += 1;
match store.reserve(second, Some(1), at).await {
Ok(()) => {
let _ = store.release(second).await;
}
Err(e) => report.record(
"releasing makes room",
format!(
"the slot freed by a finished run could not be reused: {e}. A \
ceiling is back-pressure, and one that never frees is a tenant \
permanently stopped by its first burst"
),
),
}
}
async fn reserving_twice_takes_one_slot(
store: &dyn QuotaStore,
at: Timestamp,
report: &mut Report,
) {
let run = RunId::generate();
report.checked += 1;
if let Err(e) = store.reserve(run, Some(1), at).await {
report.record("reserving a run", format!("{e}"));
return;
}
report.checked += 1;
match store.reserve(run, Some(1), at).await {
Ok(()) => {}
Err(e) => report.record(
"reserving one run twice is idempotent",
format!(
"a retried admission was refused against its own slot ({e}), so \
a transient error during admission costs the tenant capacity \
until something releases a run it never really started"
),
),
}
report.checked += 1;
match store.running().await {
Ok(1) => {}
Ok(n) => report.record(
"reserving one run twice takes one slot",
format!(
"{n} slots are held for one run, so every retry permanently shrinks the ceiling"
),
),
Err(e) => report.record("counting running runs", format!("{e}")),
}
let _ = store.release(run).await;
}
async fn spend_accrues_per_period(store: &dyn QuotaStore, report: &mut Report) {
let (this, next) = ("2999-01", "2999-02");
let run = RunId::generate();
let first = QuotaSettlement {
run,
epoch: 1,
period: Some(this.to_owned()),
spend: Spend::tokens(400),
release_slot: false,
};
let second = QuotaSettlement {
run,
epoch: 2,
period: Some(this.to_owned()),
spend: Spend::tokens(600),
release_slot: false,
};
report.checked += 1;
for settlement in [&first, &second] {
if let Err(e) = store.settle(settlement).await {
report.record("settling spend", format!("{e}"));
return;
}
}
report.checked += 1;
match store.spent(this).await {
Ok(s) if s.tokens == 1_000 => {}
Ok(s) => report.record(
"accruals sum",
format!(
"two accruals of 400 and 600 totalled {} rather than 1000. \
Reading a total, adding to it and writing it back loses one of \
two concurrent updates — and what it loses is spend a tenant \
has already incurred, so the ceiling drifts upward under load",
s.tokens
),
),
Err(e) => report.record("reading spend", format!("{e}")),
}
report.checked += 1;
if let Err(e) = store.settle(&first).await {
report.record("retrying an identical settlement", format!("{e}"));
}
match store.spent(this).await {
Ok(s) if s.tokens == 1_000 => {}
Ok(s) => report.record(
"an identical settlement accrues once",
format!("retrying one pass changed the total to {} tokens", s.tokens),
),
Err(e) => report.record("reading spend after a settlement retry", format!("{e}")),
}
report.checked += 1;
let changed = QuotaSettlement {
spend: Spend::tokens(401),
..first.clone()
};
if store.settle(&changed).await.is_ok() {
report.record(
"one pass key names one exact settlement",
"the same run/epoch accepted a different spend, so a retry can rewrite the bill",
);
}
report.checked += 1;
match store.spent(next).await {
Ok(s) if s.tokens == 0 => {}
Ok(s) => report.record(
"periods are independent",
format!(
"an untouched period already reports {} tokens, so a ceiling \
would never reset and a tenant is billed forever for one month",
s.tokens
),
),
Err(e) => report.record("reading an untouched period", format!("{e}")),
}
}
async fn halt(store: &dyn QuotaStore, report: &mut Report) {
report.checked += 1;
match store.halted().await {
Ok(None) => {}
Ok(Some(reason)) => report.record(
"a fresh tenant is not halted",
format!("an untouched tenant reports itself halted for '{reason}', so a plane would refuse every run it was never told to refuse"),
),
Err(e) => report.record("reading the halt", format!("{e}")),
}
report.checked += 1;
if let Err(e) = store.set_halt(Some("incident 42")).await {
report.record("setting the halt", format!("{e}"));
}
match store.halted().await {
Ok(Some(reason)) if reason == "incident 42" => {}
Ok(other) => report.record(
"the halt survives being written",
format!(
"after halting, the store reports {other:?} — a switch that does \
not read back is one an operator believes they threw"
),
),
Err(e) => report.record("reading the halt back", format!("{e}")),
}
report.checked += 1;
if let Err(e) = store.set_halt(Some("incident 43")).await {
report.record("re-halting", format!("{e}"));
}
match store.halted().await {
Ok(Some(reason)) if reason == "incident 43" => {}
Ok(other) => report.record(
"re-halting replaces the reason",
format!("expected the newer reason, got {other:?}"),
),
Err(e) => report.record("re-reading the halt", format!("{e}")),
}
report.checked += 1;
if let Err(e) = store.set_halt(None).await {
report.record("lifting the halt", format!("{e}"));
}
match store.halted().await {
Ok(None) => {}
Ok(Some(reason)) => report.record(
"a lifted halt stays lifted",
format!(
"the tenant is still halted for '{reason}' after the stop was \
lifted, so an incident that is over never ends"
),
),
Err(e) => report.record("reading a lifted halt", format!("{e}")),
}
report.checked += 1;
if let Err(e) = store.set_halt(None).await {
report.record("lifting an unset halt", format!("{e}"));
}
}