use zerocopy::FromBytes;
use crate::db::{DbStats, LogPosition, ReadCursor, Replicated};
use crate::error::Error;
use crate::sector::LogId;
use crate::spool::cursor::{
CURSOR_RECORD_SIZE, CURSOR_SECTORS, CursorKind, CursorRegion, SlotAddress, SpoolCursorWire,
};
fn batch_is_newer(a: u32, b: u32) -> bool {
a != b && a.wrapping_sub(b) < 1 << 31
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct SpoolBudget {
pub max_entries: u32,
}
impl Default for SpoolBudget {
fn default() -> Self {
Self { max_entries: 64 }
}
}
impl SpoolBudget {
pub const fn new(max_entries: u32) -> Self {
Self { max_entries }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Attached {
pub resume_from: LogPosition,
pub batch_id: u32,
pub in_flight_entries: u32,
pub recovered_entries: u32,
pub fresh: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct SpoolProgress {
pub entries_copied: u32,
pub committed_upto: LogPosition,
pub caught_up: bool,
pub gap: bool,
pub volume_erased: u32,
}
impl SpoolProgress {
pub(crate) fn idle(at: LogPosition, gap: bool) -> Self {
Self {
entries_copied: 0,
committed_upto: at,
caught_up: true,
gap,
volume_erased: 0,
}
}
pub(crate) fn absorb(&mut self, later: &Self) {
self.entries_copied += later.entries_copied;
self.committed_upto = later.committed_upto;
self.caught_up = later.caught_up;
self.gap |= later.gap;
self.volume_erased += later.volume_erased;
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct EraseTally {
start: u32,
cursor: u32,
}
impl EraseTally {
pub(crate) fn new(volume_erased_so_far: u32) -> Self {
Self {
start: volume_erased_so_far,
cursor: 0,
}
}
pub(crate) fn cursor_erase(&mut self) {
self.cursor += 1;
}
pub(crate) fn total(&self, volume_erased_now: u32) -> u32 {
self.cursor + volume_erased_now.saturating_sub(self.start)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct VolumeFill {
pub used_sectors: u16,
pub free_sectors: u16,
pub total_sectors: u16,
pub entries: u32,
}
impl VolumeFill {
pub fn from_stats(stats: &DbStats) -> Self {
Self {
used_sectors: stats.used_sectors,
free_sectors: stats.free_sectors,
total_sectors: stats.used_sectors.saturating_add(stats.free_sectors),
entries: stats.total_entries,
}
}
pub fn used_permille(&self) -> u16 {
if self.total_sectors == 0 {
return 0;
}
((self.used_sectors as u32 * 1000) / self.total_sectors as u32) as u16
}
pub fn will_overwrite(&self) -> bool {
self.free_sectors == 0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct CursorScan {
pub resume: LogPosition,
pub last_batch_id: u32,
pub in_flight: u32,
pub in_flight_dest: LogPosition,
pub active: u8,
pub next_slot: u32,
pub found: bool,
}
impl Default for CursorScan {
fn default() -> Self {
Self {
resume: LogPosition::BEGINNING,
last_batch_id: 0,
in_flight: 0,
in_flight_dest: LogPosition::BEGINNING,
active: 0,
next_slot: 0,
found: false,
}
}
}
impl CursorScan {
pub(crate) fn landed_probe(&self) -> Option<(LogPosition, u32)> {
(self.in_flight > 0).then_some((self.in_flight_dest, self.in_flight + 1))
}
pub(crate) fn accept_landed(&self, run: RunFingerprint, cursor: &ReadCursor) -> RunFingerprint {
if cursor.gap || run.entries() > self.in_flight {
return RunFingerprint::new();
}
run
}
}
#[derive(Debug)]
pub(crate) struct SpoolFailure {
pub error: Error,
pub from_source: bool,
}
impl From<Error> for SpoolFailure {
fn from(error: Error) -> Self {
Self {
error,
from_source: false,
}
}
}
impl SpoolFailure {
pub fn from_source<T>(result: Result<T, Error>) -> Result<T, Self> {
result.map_err(|error| Self {
error,
from_source: true,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct RunFingerprint {
entries: u32,
first: (u16, u64),
last: (u16, u64),
}
impl RunFingerprint {
pub fn new() -> Self {
Self::default()
}
pub fn push(&mut self, sensor_id: u16, timestamp: u64) {
if self.entries == 0 {
self.first = (sensor_id, timestamp);
}
self.last = (sensor_id, timestamp);
self.entries += 1;
}
pub fn entries(&self) -> u32 {
self.entries
}
pub fn is_empty(&self) -> bool {
self.entries == 0
}
}
#[derive(Debug, Clone, Copy)]
struct NewestBatch {
batch_id: u32,
committed: bool,
entries: u32,
dest: LogPosition,
sector: u8,
}
#[derive(Debug, Clone, Copy)]
pub struct CursorFold {
scan: CursorScan,
fill: [u32; CURSOR_SECTORS as usize],
newest_batch: Option<NewestBatch>,
}
impl Default for CursorFold {
fn default() -> Self {
Self::new()
}
}
impl CursorFold {
pub fn new() -> Self {
Self {
scan: CursorScan::default(),
fill: [0; CURSOR_SECTORS as usize],
newest_batch: None,
}
}
pub fn occupied(&mut self, sector: u8, slot: u32) {
let sector_idx = (sector % CURSOR_SECTORS) as usize;
self.fill[sector_idx] = self.fill[sector_idx].max(slot + 1);
}
pub fn record(&mut self, sector: u8, slot: u32, record: &SpoolCursorWire) {
self.occupied(sector, slot);
let candidate = record.resume_point();
if candidate > self.scan.resume {
self.scan.resume = candidate;
}
let batch_id = record.batch_id.get();
let is_commit = record.kind() == Some(CursorKind::Commit);
let seen = NewestBatch {
batch_id,
committed: is_commit,
entries: record.entry_count.get(),
dest: record.dest(),
sector: sector % CURSOR_SECTORS,
};
self.newest_batch = Some(match self.newest_batch {
Some(newest) if batch_id == newest.batch_id => NewestBatch {
committed: newest.committed || is_commit,
..seen
},
Some(newest) if !batch_is_newer(batch_id, newest.batch_id) => newest,
_ => seen,
});
self.scan.found = true;
}
pub fn feed(
&mut self,
at: SlotAddress,
buf: &[u8; CURSOR_RECORD_SIZE],
expected: u32,
) -> Result<(), Error> {
if buf.iter().all(|b| *b == 0xFF) {
return Ok(());
}
self.occupied(at.sector, at.slot);
let record = SpoolCursorWire::read_from_bytes(buf).map_err(|_| Error::CorruptCursor)?;
if !record.is_valid() {
return Ok(());
}
let found = record.source_log_id.get();
if expected != 0 && found != 0 && found != expected {
return Err(Error::ForeignVolume { expected, found });
}
self.record(at.sector, at.slot, &record);
Ok(())
}
pub fn finish(mut self) -> CursorScan {
if let Some(newest) = self.newest_batch {
self.scan.last_batch_id = newest.batch_id;
self.scan.active = newest.sector;
if !newest.committed {
self.scan.in_flight = newest.entries;
self.scan.in_flight_dest = newest.dest;
}
}
self.scan.next_slot = self.fill[self.scan.active as usize];
self.scan
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct SlotPlan {
pub sector: u8,
pub slot: u32,
pub erase_first: bool,
}
pub fn plan_slots(region: &CursorRegion, active: u8, next_slot: u32) -> SlotPlan {
if next_slot + 2 <= region.slots_per_sector() {
SlotPlan {
sector: active % CURSOR_SECTORS,
slot: next_slot,
erase_first: false,
}
} else {
SlotPlan {
sector: (active + 1) % CURSOR_SECTORS,
slot: 0,
erase_first: true,
}
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct Batch {
slots: SlotPlan,
batch_id: u32,
source_log: Option<LogId>,
source_epoch: u32,
from: LogPosition,
dest: LogPosition,
budget: SpoolBudget,
}
impl Batch {
pub(crate) fn sector(&self) -> u8 {
self.slots.sector
}
pub(crate) fn intent_slot(&self) -> u32 {
self.slots.slot
}
pub(crate) fn commit_slot(&self) -> u32 {
self.slots.slot + 1
}
pub(crate) fn erase(&self, region: &CursorRegion) -> Option<(u32, u32)> {
self.slots
.erase_first
.then(|| region.sector_range(self.slots.sector))
}
pub(crate) fn intent(&self) -> SpoolCursorWire {
SpoolCursorWire::new(
CursorKind::Intent,
self.source_log,
self.source_epoch,
self.batch_id,
self.from,
self.from,
self.budget.max_entries,
self.dest,
0,
)
}
pub(crate) fn commit(
&self,
upto: LogPosition,
copied: Replicated,
last_timestamp: u64,
) -> SpoolCursorWire {
SpoolCursorWire::new(
CursorKind::Commit,
self.source_log,
self.source_epoch,
self.batch_id,
self.from,
upto,
copied.entries,
self.dest,
last_timestamp,
)
}
}
#[derive(Debug, Clone, Copy, Default)]
pub(crate) struct BatchSector(Option<u32>);
impl BatchSector {
pub(crate) fn new() -> Self {
Self::default()
}
pub(crate) fn admits(&mut self, sequence: u32) -> bool {
match self.0 {
Some(seq) => seq == sequence,
None => {
self.0 = Some(sequence);
true
}
}
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct SpoolerState {
source_log: Option<LogId>,
region: CursorRegion,
active: u8,
next_slot: u32,
last_batch_id: u32,
resume: LogPosition,
pending: RunFingerprint,
}
impl SpoolerState {
pub(crate) fn new(
source_log: Option<LogId>,
cursor_base: u32,
sector_size: u32,
page_size: u32,
) -> Result<Self, Error> {
Ok(Self {
source_log,
region: CursorRegion::new(cursor_base, sector_size, page_size)?,
active: 0,
next_slot: 0,
last_batch_id: 0,
resume: LogPosition::BEGINNING,
pending: RunFingerprint::new(),
})
}
pub(crate) fn source_log(&self) -> Option<LogId> {
self.source_log
}
pub(crate) fn expected_log(&self) -> u32 {
self.source_log.map_or(0, LogId::get)
}
pub(crate) fn region(&self) -> &CursorRegion {
&self.region
}
pub(crate) fn resume(&self) -> LogPosition {
self.resume
}
pub(crate) fn pending_reconciliation(&self) -> u32 {
self.pending.entries()
}
pub(crate) fn adopt_scan(&mut self, scan: CursorScan, landed: RunFingerprint) -> Attached {
self.active = scan.active;
self.next_slot = scan.next_slot;
self.last_batch_id = scan.last_batch_id;
self.resume = scan.resume;
self.pending = landed;
Attached {
resume_from: scan.resume,
batch_id: scan.last_batch_id,
in_flight_entries: scan.in_flight,
recovered_entries: landed.entries(),
fresh: !scan.found,
}
}
pub(crate) fn reset(&mut self) {
self.resume = LogPosition::BEGINNING;
self.pending = RunFingerprint::new();
}
pub(crate) fn take_pending(&mut self) -> RunFingerprint {
core::mem::take(&mut self.pending)
}
pub(crate) fn accept_skip(
&mut self,
landed: &RunFingerprint,
found: &RunFingerprint,
cursor: &ReadCursor,
) -> bool {
if cursor.gap || found != landed {
return false;
}
self.resume = cursor.next;
true
}
pub(crate) fn plan_batch(
&self,
source_epoch: u32,
dest: LogPosition,
budget: SpoolBudget,
) -> Batch {
Batch {
slots: plan_slots(&self.region, self.active, self.next_slot),
batch_id: self.last_batch_id.wrapping_add(1),
source_log: self.source_log,
source_epoch,
from: self.resume,
dest,
budget,
}
}
pub(crate) fn commit_batch(&mut self, batch: &Batch, upto: LogPosition) {
self.active = batch.slots.sector;
self.next_slot = batch.commit_slot() + 1;
self.last_batch_id = batch.batch_id;
self.resume = upto;
}
}
#[cfg(test)]
mod tests {
use super::*;
fn region() -> CursorRegion {
CursorRegion::new(0, 4096, 256).unwrap()
}
fn pos(sequence: u32, offset: u32) -> LogPosition {
LogPosition { sequence, offset }
}
fn dest_of(batch_id: u32) -> LogPosition {
pos(100u32.wrapping_add(batch_id), 28)
}
fn intent(batch_id: u32, from: LogPosition, budget: u32) -> SpoolCursorWire {
SpoolCursorWire::new(
CursorKind::Intent,
LogId::new(1),
1,
batch_id,
from,
from,
budget,
dest_of(batch_id),
0,
)
}
fn commit(batch_id: u32, from: LogPosition, upto: LogPosition, n: u32) -> SpoolCursorWire {
SpoolCursorWire::new(
CursorKind::Commit,
LogId::new(1),
1,
batch_id,
from,
upto,
n,
dest_of(batch_id),
0,
)
}
#[test]
fn an_empty_region_resumes_from_the_beginning() {
let scan = CursorFold::new().finish();
assert!(!scan.found);
assert_eq!(scan.resume, LogPosition::BEGINNING);
assert_eq!(scan.next_slot, 0);
assert_eq!(scan.in_flight, 0);
}
#[test]
fn a_completed_batch_resumes_after_it() {
let mut fold = CursorFold::new();
fold.record(0, 0, &intent(1, pos(1, 28), 64));
fold.record(0, 1, &commit(1, pos(1, 28), pos(1, 900), 40));
let scan = fold.finish();
assert_eq!(scan.resume, pos(1, 900), "the commit is authoritative");
assert_eq!(scan.last_batch_id, 1);
assert_eq!(scan.next_slot, 2);
assert_eq!(scan.in_flight, 0, "the batch finished");
}
#[test]
fn an_unfinished_batch_reports_where_on_the_replica_it_landed() {
let mut fold = CursorFold::new();
fold.record(0, 0, &intent(1, pos(1, 28), 64));
fold.record(0, 1, &commit(1, pos(1, 28), pos(1, 900), 40));
fold.record(0, 2, &intent(2, pos(1, 900), 64));
let scan = fold.finish();
assert_eq!(
scan.in_flight_dest,
dest_of(2),
"reconciling batch 2 means reading the replica from where it began"
);
}
#[test]
fn a_finished_batch_reports_no_replica_position() {
let mut fold = CursorFold::new();
fold.record(0, 0, &intent(1, pos(1, 28), 64));
fold.record(0, 1, &commit(1, pos(1, 28), pos(1, 900), 40));
let scan = fold.finish();
assert_eq!(scan.in_flight, 0);
assert_eq!(
scan.in_flight_dest,
LogPosition::BEGINNING,
"nothing to reconcile, so nothing to read"
);
}
#[test]
fn an_intent_and_its_commit_agree_on_the_replica_position() {
let mut intent_only = CursorFold::new();
intent_only.record(0, 0, &intent(3, pos(2, 28), 64));
let mut both = CursorFold::new();
both.record(0, 0, &intent(3, pos(2, 28), 64));
both.record(0, 1, &commit(3, pos(2, 28), pos(2, 500), 20));
assert_eq!(intent_only.finish().in_flight_dest, dest_of(3));
assert_eq!(both.finish().in_flight, 0);
}
#[test]
fn a_run_fingerprint_distinguishes_length_and_bounds() {
let mut a = RunFingerprint::new();
assert!(a.is_empty());
for (sensor, ts) in [(1u16, 10u64), (1, 11), (2, 12)] {
a.push(sensor, ts);
}
assert_eq!(a.entries(), 3);
assert!(!a.is_empty());
let mut same = RunFingerprint::new();
for (sensor, ts) in [(1u16, 10u64), (1, 11), (2, 12)] {
same.push(sensor, ts);
}
assert_eq!(a, same);
let mut shorter = RunFingerprint::new();
for (sensor, ts) in [(1u16, 10u64), (1, 11)] {
shorter.push(sensor, ts);
}
assert_ne!(a, shorter);
let mut shifted = RunFingerprint::new();
for (sensor, ts) in [(1u16, 11u64), (2, 12), (2, 13)] {
shifted.push(sensor, ts);
}
assert_ne!(a, shifted, "a run starting one entry late must not match");
let mut other_end = RunFingerprint::new();
for (sensor, ts) in [(1u16, 10u64), (1, 11), (2, 99)] {
other_end.push(sensor, ts);
}
assert_ne!(a, other_end);
}
#[test]
fn a_torn_commit_falls_back_to_the_intent_start() {
let mut fold = CursorFold::new();
fold.record(0, 0, &intent(1, pos(1, 28), 64));
fold.record(0, 1, &commit(1, pos(1, 28), pos(1, 900), 40));
fold.record(0, 2, &intent(2, pos(1, 900), 64));
let scan = fold.finish();
assert_eq!(
scan.resume,
pos(1, 900),
"resume where the unfinished batch began, so nothing is skipped"
);
assert_eq!(scan.last_batch_id, 2);
assert_eq!(
scan.in_flight, 64,
"the unfinished batch may have copied up to its budget"
);
}
#[test]
fn a_lost_commit_is_recovered_from_the_next_intent() {
let mut fold = CursorFold::new();
fold.record(0, 0, &intent(1, pos(1, 28), 64));
fold.record(0, 2, &intent(2, pos(1, 900), 64));
fold.record(0, 3, &commit(2, pos(1, 900), pos(2, 300), 30));
let scan = fold.finish();
assert_eq!(scan.resume, pos(2, 300));
assert_eq!(scan.in_flight, 0);
}
#[test]
fn record_order_does_not_matter() {
let records = [
(0u8, 0u32, intent(1, pos(1, 28), 64)),
(0, 1, commit(1, pos(1, 28), pos(1, 900), 40)),
(0, 2, intent(2, pos(1, 900), 64)),
(0, 3, commit(2, pos(1, 900), pos(2, 300), 30)),
];
let mut forward = CursorFold::new();
for (s, i, r) in records.iter() {
forward.record(*s, *i, r);
}
let mut backward = CursorFold::new();
for (s, i, r) in records.iter().rev() {
backward.record(*s, *i, r);
}
assert_eq!(forward.finish(), backward.finish());
}
#[test]
fn the_newest_batch_wins_across_cursor_sectors() {
let mut fold = CursorFold::new();
fold.record(0, 14, &commit(9, pos(4, 28), pos(4, 900), 10));
fold.record(1, 0, &intent(10, pos(4, 900), 64));
fold.record(1, 1, &commit(10, pos(4, 900), pos(5, 100), 20));
let scan = fold.finish();
assert_eq!(scan.active, 1, "writing continues in the newer sector");
assert_eq!(scan.next_slot, 2);
assert_eq!(scan.resume, pos(5, 100));
assert_eq!(scan.last_batch_id, 10);
}
#[test]
fn a_torn_slot_still_consumes_its_place() {
let mut fold = CursorFold::new();
fold.record(0, 0, &intent(1, pos(1, 28), 64));
fold.record(0, 1, &commit(1, pos(1, 28), pos(1, 900), 40));
fold.record(0, 2, &intent(2, pos(1, 900), 64));
fold.occupied(0, 3); let scan = fold.finish();
assert_eq!(scan.next_slot, 4, "the damaged slot is not reusable");
assert_eq!(scan.resume, pos(1, 900), "and its batch replays");
assert_eq!(scan.in_flight, 64);
}
#[test]
fn a_region_holding_only_a_torn_slot_skips_past_it() {
let mut fold = CursorFold::new();
fold.occupied(0, 0);
let scan = fold.finish();
assert!(!scan.found, "no valid record was read");
assert_eq!(scan.resume, LogPosition::BEGINNING);
assert_eq!(scan.next_slot, 1, "but slot 0 is spent");
}
#[test]
fn batch_ordering_is_serial_not_numeric() {
assert!(batch_is_newer(2, 1));
assert!(!batch_is_newer(1, 2));
assert!(!batch_is_newer(7, 7), "a batch is not newer than itself");
assert!(batch_is_newer(0, u32::MAX));
assert!(batch_is_newer(3, u32::MAX - 3));
assert!(!batch_is_newer(u32::MAX, 0));
}
#[test]
fn the_newest_batch_survives_the_counter_wrapping() {
let mut fold = CursorFold::new();
fold.record(0, 0, &intent(u32::MAX - 1, pos(1, 28), 64));
fold.record(0, 1, &commit(u32::MAX - 1, pos(1, 28), pos(1, 500), 20));
fold.record(0, 2, &intent(u32::MAX, pos(1, 500), 64));
fold.record(0, 3, &commit(u32::MAX, pos(1, 500), pos(1, 900), 20));
fold.record(1, 0, &intent(0, pos(1, 900), 64));
fold.record(1, 1, &commit(0, pos(1, 900), pos(2, 300), 20));
fold.record(1, 2, &intent(1, pos(2, 300), 64));
fold.record(1, 3, &commit(1, pos(2, 300), pos(2, 700), 20));
let scan = fold.finish();
assert_eq!(
scan.active, 1,
"writing continues where the newest batch is"
);
assert_eq!(scan.next_slot, 4);
assert_eq!(scan.last_batch_id, 1);
assert_eq!(scan.resume, pos(2, 700));
}
#[test]
fn an_unfinished_batch_is_still_detected_across_the_wrap() {
let mut fold = CursorFold::new();
fold.record(0, 0, &intent(u32::MAX, pos(1, 28), 64));
fold.record(0, 1, &commit(u32::MAX, pos(1, 28), pos(1, 900), 20));
fold.record(1, 0, &intent(0, pos(1, 900), 64));
let scan = fold.finish();
assert_eq!(scan.in_flight, 64, "batch 0 is the newest, and unfinished");
assert_eq!(scan.in_flight_dest, dest_of(0));
assert_eq!(scan.resume, pos(1, 900), "the interrupted batch replays");
assert_eq!(scan.active, 1);
}
#[test]
fn a_wrapped_region_folds_the_same_in_any_order() {
let records = [
(0u8, 2u32, intent(u32::MAX, pos(1, 500), 64)),
(0, 3, commit(u32::MAX, pos(1, 500), pos(1, 900), 20)),
(1, 0, intent(0, pos(1, 900), 64)),
(1, 1, commit(0, pos(1, 900), pos(2, 300), 20)),
];
let mut forward = CursorFold::new();
for (s, i, r) in records.iter() {
forward.record(*s, *i, r);
}
let mut backward = CursorFold::new();
for (s, i, r) in records.iter().rev() {
backward.record(*s, *i, r);
}
assert_eq!(forward.finish(), backward.finish());
}
#[test]
fn volume_fill_reports_headroom_and_the_last_moment_to_swap() {
let fill = VolumeFill::from_stats(&DbStats {
total_entries: 4200,
total_samples: 4200,
used_sectors: 6,
free_sectors: 2,
reserved_sectors: 1,
min_erase_count: 1,
max_erase_count: 1,
total_erase_count: 8,
});
assert_eq!(fill.total_sectors, 8);
assert_eq!(fill.entries, 4200);
assert_eq!(fill.used_permille(), 750);
assert!(!fill.will_overwrite(), "two sectors of headroom remain");
let full = VolumeFill::from_stats(&DbStats {
used_sectors: 8,
free_sectors: 0,
..DbStats {
total_entries: 0,
total_samples: 0,
used_sectors: 0,
free_sectors: 0,
reserved_sectors: 0,
min_erase_count: 0,
max_erase_count: 0,
total_erase_count: 0,
}
});
assert_eq!(full.used_permille(), 1000);
assert!(
full.will_overwrite(),
"the next rotation reclaims a sector holding data"
);
assert_eq!(VolumeFill::default().used_permille(), 0);
}
#[test]
fn a_batch_is_confined_to_the_first_source_sector_it_sees() {
let mut batch = BatchSector::new();
assert!(batch.admits(7));
assert!(batch.admits(7));
assert!(!batch.admits(8));
assert!(!batch.admits(8));
assert!(batch.admits(7), "the batch still belongs to sector 7");
let mut from_zero = BatchSector::new();
assert!(from_zero.admits(0));
assert!(!from_zero.admits(1));
}
#[test]
fn slots_are_allocated_in_pairs_then_wrap_to_the_other_sector() {
let region = region();
assert_eq!(region.slots_per_sector(), 16);
let plan = plan_slots(®ion, 0, 0);
assert_eq!(
plan,
SlotPlan {
sector: 0,
slot: 0,
erase_first: false
}
);
let plan = plan_slots(®ion, 0, 14);
assert_eq!(
plan,
SlotPlan {
sector: 0,
slot: 14,
erase_first: false
}
);
let plan = plan_slots(®ion, 0, 15);
assert_eq!(
plan,
SlotPlan {
sector: 1,
slot: 0,
erase_first: true
}
);
let plan = plan_slots(®ion, 1, 16);
assert_eq!(
plan,
SlotPlan {
sector: 0,
slot: 0,
erase_first: true
}
);
}
}