pub(crate) const WRITE_LEASE: i64 = 3_600 * 1_000_000_000;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Writers {
None,
#[cfg_attr(
not(test),
expect(
dead_code,
reason = "servers that share one database recover with it from a later step; the tests do now"
)
)]
Maybe,
}
pub(crate) fn under_way(writers: Writers, started_at: i64, now: i64) -> bool {
match writers {
Writers::None => false,
Writers::Maybe => {
(i128::from(now) - i128::from(started_at)).abs() < i128::from(WRITE_LEASE)
}
}
}
pub(crate) fn leftover(named: bool, temporary: bool, under_way: bool) -> bool {
!under_way && (temporary || !named)
}
#[cfg(test)]
mod tests {
use super::{WRITE_LEASE, Writers, leftover, under_way};
const NOW: i64 = 1_792_756_800 * 1_000_000_000;
#[test]
fn a_write_that_began_now_is_under_way() {
assert!(under_way(Writers::Maybe, NOW, NOW), "a write begun now");
}
#[test]
fn a_write_that_began_a_minute_ago_is_under_way() {
let minute = 60 * 1_000_000_000;
assert!(
under_way(Writers::Maybe, NOW - minute, NOW),
"a write begun a minute ago"
);
}
#[test]
fn a_write_one_nanosecond_inside_the_lease_is_under_way() {
assert!(
under_way(Writers::Maybe, NOW - WRITE_LEASE + 1, NOW),
"a write begun one nanosecond less than the lease ago"
);
}
#[test]
fn a_write_begun_on_a_clock_a_minute_ahead_is_under_way() {
let minute = 60 * 1_000_000_000;
assert!(
under_way(Writers::Maybe, NOW + minute, NOW),
"another process's clock a minute ahead"
);
}
#[test]
fn an_object_no_row_names_whose_write_is_under_way_is_kept() {
assert!(
!leftover(false, false, true),
"an object of a write under way is kept"
);
}
#[test]
fn a_temporary_object_whose_write_is_under_way_is_kept() {
assert!(
!leftover(false, true, true),
"a temporary name of a write under way is kept"
);
}
#[test]
fn an_object_a_row_names_is_kept() {
assert!(!leftover(true, false, false), "a named object is kept");
}
#[test]
fn an_object_a_row_names_with_a_pending_row_is_kept() {
assert!(
!leftover(true, false, true),
"a named object with a pending row is kept"
);
}
#[test]
fn a_lease_is_an_hour() {
assert_eq!(WRITE_LEASE, 3_600_000_000_000, "the lease, in nanoseconds");
}
#[test]
fn a_write_begun_at_the_epoch_is_under_way_at_the_epoch() {
assert!(
under_way(Writers::Maybe, 0, 0),
"the same time, at the epoch"
);
}
#[test]
fn a_write_before_the_epoch_is_under_way_just_after_it() {
assert!(
under_way(Writers::Maybe, -1, 1),
"two nanoseconds apart across the epoch"
);
}
#[test]
fn a_write_one_nanosecond_inside_the_lease_ahead_is_under_way() {
assert!(
under_way(Writers::Maybe, NOW + WRITE_LEASE - 1, NOW),
"a clock ahead by one nanosecond less than the lease"
);
}
#[test]
fn an_object_no_row_names_and_no_write_is_under_way_for_is_a_leftover() {
assert!(
leftover(false, false, false),
"an object with no row and no write under way"
);
}
#[test]
fn a_temporary_object_no_write_is_under_way_for_is_a_leftover() {
assert!(
leftover(false, true, false),
"a temporary name with no write under way"
);
}
#[test]
fn a_temporary_object_a_row_names_is_a_leftover() {
assert!(
leftover(true, true, false),
"a temporary name is never a version's object"
);
}
#[test]
fn a_write_that_began_two_hours_ago_is_not_under_way() {
assert!(
!under_way(Writers::Maybe, NOW - 2 * WRITE_LEASE, NOW),
"a write begun twice the lease ago"
);
}
#[test]
fn a_write_at_exactly_the_lease_is_not_under_way() {
assert!(
!under_way(Writers::Maybe, NOW - WRITE_LEASE, NOW),
"a write begun exactly the lease ago"
);
}
#[test]
fn a_write_exactly_the_lease_ahead_is_not_under_way() {
assert!(
!under_way(Writers::Maybe, NOW + WRITE_LEASE, NOW),
"a clock ahead by exactly the lease"
);
}
#[test]
fn a_write_a_day_ahead_is_not_under_way() {
assert!(
!under_way(Writers::Maybe, NOW + 24 * WRITE_LEASE, NOW),
"a clock a day ahead"
);
}
#[test]
fn with_no_other_writer_a_write_begun_now_is_not_under_way() {
assert!(
!under_way(Writers::None, NOW, NOW),
"the process holds the catalog alone"
);
}
#[test]
fn with_no_other_writer_a_write_a_minute_old_is_not_under_way() {
let minute = 60 * 1_000_000_000;
assert!(
!under_way(Writers::None, NOW - minute, NOW),
"the process holds the catalog alone"
);
}
#[test]
fn with_no_other_writer_a_write_ahead_is_not_under_way() {
assert!(
!under_way(Writers::None, NOW + 1, NOW),
"the process holds the catalog alone"
);
}
#[test]
fn a_write_at_the_lease_with_no_other_writer_is_not_under_way() {
assert!(
!under_way(Writers::None, NOW - WRITE_LEASE, NOW),
"the process holds the catalog alone"
);
}
#[test]
fn a_write_one_nanosecond_past_the_lease_is_not_under_way() {
assert!(
!under_way(Writers::Maybe, NOW - WRITE_LEASE - 1, NOW),
"a write begun one nanosecond more than the lease ago"
);
}
#[test]
fn the_earliest_start_at_the_latest_time_is_not_under_way() {
assert!(
!under_way(Writers::Maybe, i64::MIN, i64::MAX),
"the widest distance there is"
);
}
#[test]
fn the_latest_start_at_the_earliest_time_is_not_under_way() {
assert!(
!under_way(Writers::Maybe, i64::MAX, i64::MIN),
"the widest distance there is, ahead"
);
}
#[test]
fn the_latest_start_at_the_latest_time_is_under_way() {
assert!(
under_way(Writers::Maybe, i64::MAX, i64::MAX),
"the same time, at the end of the range"
);
}
#[test]
fn the_earliest_start_at_the_earliest_time_is_under_way() {
assert!(
under_way(Writers::Maybe, i64::MIN, i64::MIN),
"the same time, at the start of the range"
);
}
#[test]
fn a_start_inside_the_lease_of_the_latest_time_is_under_way() {
assert!(
under_way(Writers::Maybe, i64::MAX - WRITE_LEASE + 1, i64::MAX),
"one nanosecond inside the lease, at the end of the range"
);
}
#[test]
fn a_start_inside_the_lease_of_the_earliest_time_is_under_way() {
assert!(
under_way(Writers::Maybe, i64::MIN + WRITE_LEASE - 1, i64::MIN),
"one nanosecond inside the lease ahead, at the start of the range"
);
}
}