use crate::github::QueuedJob;
#[derive(Debug, Default)]
pub(super) struct Listing(Shown);
#[derive(Debug, Default)]
enum Shown {
#[default]
Unknown,
Listed(Vec<QueuedJob>),
Stale(Vec<QueuedJob>),
}
impl Listing {
pub(super) fn answer(&mut self, jobs: Vec<QueuedJob>) {
self.0 = Shown::Listed(jobs);
}
pub(super) fn fail(&mut self) {
self.0 = Shown::Unknown;
}
pub(super) fn stop(&mut self) {
if let Shown::Listed(jobs) = &mut self.0 {
self.0 = Shown::Stale(std::mem::take(jobs));
}
}
#[cfg(test)]
pub(super) fn replace_jobs(&mut self, jobs: Vec<QueuedJob>) {
self.0 = match &self.0 {
Shown::Listed(_) => Shown::Listed(jobs),
Shown::Unknown | Shown::Stale(_) => Shown::Stale(jobs),
};
}
pub(super) fn listed(&self) -> Option<&[QueuedJob]> {
match &self.0 {
Shown::Listed(jobs) => Some(jobs),
Shown::Unknown | Shown::Stale(_) => None,
}
}
pub(super) fn is_listed(&self) -> bool {
self.listed().is_some()
}
pub(super) fn no_longer_shows(&self, id: u64) -> bool {
self.listed()
.is_some_and(|jobs| !jobs.iter().any(|job| job.id == id))
}
pub(super) fn jobs(&self) -> &[QueuedJob] {
match &self.0 {
Shown::Listed(jobs) | Shown::Stale(jobs) => jobs,
Shown::Unknown => &[],
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn job(id: u64) -> QueuedJob {
QueuedJob {
id,
run_id: id,
name: format!("job {id}"),
labels: Vec::new(),
created_at: 0,
run_attempt: 1,
workflow: None,
branch: None,
url: None,
}
}
fn ids(jobs: &[QueuedJob]) -> Vec<u64> {
jobs.iter().map(|job| job.id).collect()
}
#[test]
fn a_repository_never_listed_shows_no_jobs_and_is_not_listed() {
let listing = Listing::default();
assert_eq!(listing.listed().map(ids), None);
assert!(listing.jobs().is_empty(), "nothing is known yet");
assert!(
!listing.no_longer_shows(1),
"an unknown listing says nothing of job 1"
);
}
#[test]
fn an_empty_listing_is_listed_and_drops_every_job() {
let mut listing = Listing::default();
listing.answer(Vec::new());
assert_eq!(listing.listed().map(ids), Some(Vec::new()));
assert!(
listing.no_longer_shows(1),
"an empty listing no longer shows job 1"
);
}
#[test]
fn a_listing_that_fails_after_one_that_answered_forgets_its_jobs() {
let mut listing = Listing::default();
listing.answer(vec![job(1)]);
assert!(!listing.no_longer_shows(1), "the answer shows job 1");
assert!(listing.no_longer_shows(2), "the answer does not show job 2");
listing.fail();
assert_eq!(listing.listed().map(ids), None);
assert!(listing.jobs().is_empty(), "a failed listing keeps no job");
assert!(
!listing.no_longer_shows(2),
"a failed listing says nothing of job 2"
);
}
#[test]
fn a_stop_keeps_the_jobs_listed_last_but_no_longer_lists_them() {
let mut listing = Listing::default();
listing.answer(vec![job(1), job(2)]);
listing.stop();
assert_eq!(listing.listed().map(ids), None);
assert!(!listing.is_listed(), "a stopped listing is not listed");
assert_eq!(ids(listing.jobs()), [1, 2]);
assert!(
!listing.no_longer_shows(3),
"a stale listing says nothing of job 3"
);
listing.stop();
assert_eq!(ids(listing.jobs()), [1, 2], "a second stop keeps them");
}
#[test]
fn a_stop_after_a_failed_listing_shows_nothing() {
let mut listing = Listing::default();
listing.answer(vec![job(1)]);
listing.fail();
listing.stop();
assert!(
listing.jobs().is_empty(),
"nothing was known when the stop came"
);
}
#[test]
fn a_listing_after_a_stop_lists_again() {
let mut listing = Listing::default();
listing.answer(vec![job(1)]);
listing.stop();
listing.answer(vec![job(2)]);
assert_eq!(listing.listed().map(ids), Some(vec![2]));
}
#[derive(Default)]
struct Fields {
queued: Vec<QueuedJob>,
listed: bool,
}
#[derive(Debug, Clone)]
enum Step {
Answer(Vec<u64>),
Fail,
Stop,
Restart,
}
fn step(at: u64) -> Step {
match at % 7 {
0 | 1 => Step::Answer((0..at % 3).map(|id| id + at % 5).collect()),
2 => Step::Answer(Vec::new()),
3 => Step::Fail,
4 | 5 => Step::Stop,
_ => Step::Restart,
}
}
#[test]
fn a_generated_sequence_reads_as_the_two_fields_did() {
for seed in 0..64_u64 {
let mut listing = Listing::default();
let mut fields = Fields::default();
let mut state = seed.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1);
let mut steps = Vec::new();
for _ in 0..200 {
state = state
.wrapping_mul(6_364_136_223_846_793_005)
.wrapping_add(1_442_695_040_888_963_407);
let next = step(state >> 33);
steps.push(next.clone());
match next {
Step::Answer(answered) => {
let jobs: Vec<QueuedJob> = answered.into_iter().map(job).collect();
listing.answer(jobs.clone());
fields.queued = jobs;
fields.listed = true;
}
Step::Fail => {
listing.fail();
fields.queued = Vec::new();
fields.listed = false;
}
Step::Stop => {
listing.stop();
fields.listed = false;
}
Step::Restart => {
listing = Listing::default();
fields = Fields::default();
}
}
let listed = fields.listed.then(|| ids(&fields.queued));
assert_eq!(listing.listed().map(ids), listed, "after {steps:?}");
assert_eq!(ids(listing.jobs()), ids(&fields.queued), "after {steps:?}");
for id in 0..8 {
assert_eq!(
listing.no_longer_shows(id),
fields.listed && !fields.queued.iter().any(|job| job.id == id),
"job {id} after {steps:?}"
);
}
}
}
}
}