use super::content_paid_frontier;
use decdn_bao_range::{CHUNK_GROUP_BYTES, align_range, bao_encoded_size};
fn wire_to(fetch_start: u64, c: u64, total: u64) -> u64 {
if c <= fetch_start {
return 0;
}
align_range(fetch_start, c - fetch_start, total)
.map_or(u64::MAX, |r| bao_encoded_size(total, r.chunk_ranges()))
}
#[test]
fn content_paid_frontier_never_over_maps_and_is_tight() {
let group = CHUNK_GROUP_BYTES;
for total in [3 * group + 123, 6 * group, group + 1] {
for fetch_start in [0u64, group, 2 * group] {
if fetch_start >= total {
continue;
}
let full_wire = wire_to(fetch_start, total, total);
for step in 0..=40u64 {
let paid_wire = full_wire.saturating_mul(step) / 40;
let c = content_paid_frontier(fetch_start, total, paid_wire);
assert!(c >= fetch_start, "frontier must not regress below start");
assert!(c <= total, "frontier must not exceed the blob");
assert_eq!(
(c - fetch_start) % group,
if c == total {
(total - fetch_start) % group
} else {
0
},
"frontier is a group boundary (or the blob end)"
);
assert!(
wire_to(fetch_start, c, total) <= paid_wire,
"c={c} maps past the paid wire {paid_wire} (would under-pay)"
);
if c < total {
let next = (c + group).min(total);
assert!(
wire_to(fetch_start, next, total) > paid_wire,
"next group {next} still fits in {paid_wire}: not tight"
);
}
}
}
}
}
#[test]
fn summed_multi_leg_wire_over_maps_which_is_why_baselines_are_per_leg() {
let group = CHUNK_GROUP_BYTES;
let total = 12 * group;
let (frontier1, frontier2) = (3 * group, 6 * group);
let leg1_wire = wire_to(0, frontier1, total);
let leg2_wire = wire_to(frontier1, frontier2, total);
let contiguous_wire = wire_to(0, frontier2, total);
assert!(
leg1_wire + leg2_wire > contiguous_wire,
"the two legs' wire ({leg1_wire} + {leg2_wire}) must exceed the contiguous \
cost of the same span ({contiguous_wire}), else this hazard would not exist"
);
let correct = content_paid_frontier(frontier1, total, leg2_wire);
assert_eq!(
correct, frontier2,
"the per-leg call lands on the true frontier"
);
let stale = content_paid_frontier(0, total, leg1_wire + leg2_wire);
assert!(
stale >= frontier2,
"a summed budget cannot under-report the frontier: {stale} < {frontier2}"
);
let with_repaid = content_paid_frontier(0, total, 2 * leg1_wire + leg2_wire);
assert!(
with_repaid > frontier2 + group,
"a re-paid leg must visibly overshoot the true frontier by more than a \
group: got {with_repaid}, true frontier {frontier2}"
);
assert_eq!(
content_paid_frontier(frontier1, total, leg2_wire),
frontier2,
"per-leg anchoring is unaffected by an earlier leg being re-paid"
);
}
#[test]
fn content_paid_frontier_zero_paid_stays_at_start() {
let group = CHUNK_GROUP_BYTES;
assert_eq!(content_paid_frontier(0, 5 * group, 0), 0);
assert_eq!(content_paid_frontier(group, 5 * group, 0), group);
}
use bytes::Bytes;
use super::{BlobCache, MemoryBlobCache, NoCache};
#[tokio::test]
async fn no_cache_always_misses_and_discards() -> anyhow::Result<()> {
let cache = NoCache;
let hash = [7u8; 32];
cache.put(hash, 0, Bytes::from_static(b"hello")).await?;
anyhow::ensure!(
cache.get(hash, 0, 5).await?.is_none(),
"the no-op cache must never hit"
);
Ok(())
}
#[tokio::test]
async fn memory_cache_round_trips_a_hash_range() -> anyhow::Result<()> {
let cache = MemoryBlobCache::new();
let hash = [1u8; 32];
let other = [2u8; 32];
cache.put(hash, 16, Bytes::from_static(b"abcd")).await?;
anyhow::ensure!(
cache.get(hash, 16, 4).await?.as_deref() == Some(&b"abcd"[..]),
"an exact (hash, range) hit returns the stored bytes"
);
anyhow::ensure!(
cache.get(other, 16, 4).await?.is_none(),
"wrong hash misses"
);
anyhow::ensure!(
cache.get(hash, 0, 4).await?.is_none(),
"wrong offset misses"
);
anyhow::ensure!(
cache.get(hash, 16, 2).await?.is_none(),
"wrong length misses"
);
Ok(())
}