use std::sync::Mutex;
use std::time::Duration;
use serde::{Deserialize, Serialize};
use super::fixture::{DAY, HOUR, MINUTE, SECOND, fixture};
use super::{Concurrency, Filter, Queue, State};
use crate::{ErrorKind, Result};
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
struct Note {
text: String,
}
fn note(text: &str) -> Note {
Note { text: text.to_owned() }
}
fn ran(queue: &Queue<Note>) -> Vec<String> {
let seen = Mutex::new(Vec::new());
queue
.concurrency(1)
.run_due(|note: Note, _run| -> Result<()> {
seen.lock().unwrap().push(note.text);
Ok(())
})
.unwrap();
seen.into_inner().unwrap()
}
#[test]
fn a_job_runs_at_its_time_and_not_before() {
let f = fixture();
let notes = f.store.queue::<Note>("notes").open().unwrap();
assert!(notes.delay(HOUR).add(¬e("later")).unwrap());
assert!(notes.add(¬e("now")).unwrap());
assert_eq!(ran(¬es), ["now"]);
f.clock.advance(HOUR - SECOND);
assert!(ran(¬es).is_empty());
f.clock.advance(SECOND);
assert_eq!(ran(¬es), ["later"]);
assert!(ran(¬es).is_empty(), "a done job leaves the queue");
}
#[test]
fn an_added_job_survives_a_reopen() {
let mut f = fixture();
let notes = f.store.queue::<Note>("notes").open().unwrap();
notes.add(¬e("kept")).unwrap();
f.reopen();
let notes = f.store.queue::<Note>("notes").open().unwrap();
assert_eq!(ran(¬es), ["kept"]);
}
#[test]
fn jobs_run_in_the_order_of_their_time_equal_times_in_the_order_added() {
let f = fixture();
let notes = f.store.queue::<Note>("notes").open().unwrap();
notes.delay(2 * SECOND).add(¬e("third")).unwrap();
notes.delay(SECOND).add(¬e("first")).unwrap();
notes.delay(SECOND).add(¬e("second")).unwrap();
f.clock.advance(2 * SECOND);
assert_eq!(ran(¬es), ["first", "second", "third"]);
}
#[test]
fn add_adds_only_to_an_id_that_is_free() {
let f = fixture();
let notes = f.store.queue::<Note>("notes").attempts(1).open().unwrap();
assert!(notes.id("a").delay(HOUR).add(¬e("first")).unwrap());
assert!(!notes.id("a").add(¬e("second")).unwrap(), "a scheduled job keeps its id");
assert_eq!(notes.get("a").unwrap().unwrap().value, note("first"), "and its value");
f.clock.advance(HOUR);
assert_eq!(ran(¬es), ["first"]);
assert!(notes.id("a").add(¬e("again")).unwrap(), "a done job leaves its id free");
let failing = notes.concurrency(1).run_due(|_: Note, _run| -> Result<(), String> { Err("down".into()) });
assert_eq!(failing.unwrap(), 1);
assert_eq!(notes.get("a").unwrap().unwrap().state, State::Failed);
assert!(!notes.id("a").add(¬e("after the failure")).unwrap(), "a failed job is kept to be looked at");
}
#[test]
fn add_of_an_id_whose_job_runs_adds_nothing() {
let f = fixture();
let notes = f.store.queue::<Note>("notes").open().unwrap();
notes.id("a").add(¬e("running")).unwrap();
let added = Mutex::new(None);
notes
.run_due(|_: Note, _run| -> Result<()> {
*added.lock().unwrap() = Some(notes.id("a").add(¬e("while it runs"))?);
Ok(())
})
.unwrap();
assert_eq!(*added.lock().unwrap(), Some(false));
assert!(ran(¬es).is_empty());
}
#[test]
fn dedupe_keeps_a_done_id_taken_until_its_span_passes() {
let f = fixture();
let pushes = f.store.queue::<Note>("pushes").dedupe(HOUR).open().unwrap();
pushes.id("m1:u1").add(¬e("push")).unwrap();
assert_eq!(ran(&pushes), ["push"]);
assert!(!pushes.id("m1:u1").add(¬e("push")).unwrap(), "an id runs once in the hour");
let done = pushes.get("m1:u1").unwrap().unwrap();
assert_eq!((done.state, done.value), (State::Done, note("push")));
assert!(done.last_run.is_some());
f.clock.advance(HOUR);
assert_eq!(super::maintain(&f.store).unwrap().done, 1);
assert!(pushes.id("m1:u1").add(¬e("push")).unwrap());
}
#[test]
fn set_makes_the_ids_job_this_value_at_this_time_whatever_it_was() {
let f = fixture();
let checks = f.store.queue::<Note>("checks").attempts(1).open().unwrap();
checks.id("backup").delay(25 * HOUR).set(¬e("ping 1")).unwrap();
f.clock.advance(24 * HOUR);
checks.id("backup").delay(25 * HOUR).set(¬e("ping 2")).unwrap();
f.clock.advance(24 * HOUR);
assert!(ran(&checks).is_empty(), "each ping moved the alarm on");
f.clock.advance(HOUR);
assert_eq!(ran(&checks), ["ping 2"]);
checks.set("failing", ¬e("one")).unwrap();
checks.run_due(|_: Note, _run| -> Result<(), &str> { Err("down") }).unwrap();
assert_eq!(checks.get("failing").unwrap().unwrap().state, State::Failed);
checks.set("failing", ¬e("two")).unwrap();
let again = checks.get("failing").unwrap().unwrap();
assert_eq!(
(again.state, again.attempt, again.value),
(State::Waiting, 0, note("two")),
"a failed job starts again"
);
}
#[test]
fn set_of_a_running_job_runs_it_once_more_with_its_value() {
let f = fixture();
let indexing = f.store.queue::<Note>("indexing").open().unwrap();
indexing.set("note:7", ¬e("draft")).unwrap();
let seen = Mutex::new(Vec::new());
let index = |note: Note, _run: &super::Run| -> Result<()> {
if note.text == "draft" {
indexing.set("note:7", ¬e_of("edited"))?;
indexing.set("note:7", ¬e_of("edited twice"))?;
}
seen.lock().unwrap().push(note.text);
Ok(())
};
indexing.run_due(index).unwrap();
assert_eq!(*seen.lock().unwrap(), ["draft", "edited twice"], "two sets while it ran ask one run more");
}
fn note_of(text: &str) -> Note {
note(text)
}
#[test]
fn update_changes_only_a_job_that_has_not_started() {
let f = fixture();
let later = f.store.queue::<Note>("send-later").open().unwrap();
assert!(!later.update("missing", ¬e("x")).unwrap(), "there was no job");
later.id("m1").delay(HOUR).add(¬e("hello")).unwrap();
assert!(later.id("m1").delay(2 * HOUR).update(¬e("hello, edited")).unwrap());
let job = later.get("m1").unwrap().unwrap();
assert_eq!((job.value.text.as_str(), job.at), ("hello, edited", f.now() + 2 * HOUR));
assert!(later.update("m1", ¬e("text alone")).unwrap(), "an update without a time keeps it");
assert_eq!(later.get("m1").unwrap().unwrap().at, f.now() + 2 * HOUR);
f.clock.advance(2 * HOUR);
let too_late = Mutex::new(None);
later
.run_due(|_: Note, _run| -> Result<()> {
*too_late.lock().unwrap() = Some(later.update("m1", ¬e("while it is sent"))?);
Ok(())
})
.unwrap();
assert_eq!(*too_late.lock().unwrap(), Some(false));
assert!(!later.update("m1", ¬e("after it was sent")).unwrap());
}
#[test]
fn an_update_of_a_job_waiting_for_its_retry_drops_the_steps_kept_for_its_old_value() {
let f = fixture();
let orders = f.store.queue::<Note>("orders").attempts(3).open().unwrap();
orders.id("order:7").add(¬e("10")).unwrap();
let charged = Mutex::new(Vec::new());
let charge = |order: Note, run: &super::Run| -> Result<()> {
let kept: String = run.step("charge", || -> Result<String> { Ok(order.text.clone()) })?;
charged.lock().unwrap().push(kept);
match order.text.as_str() {
"10" => Err(crate::Error::invalid("the receipt did not save")),
_ => Ok(()),
}
};
orders.run_due(charge).unwrap();
assert!(orders.id("order:7").update(¬e("20")).unwrap(), "a job waiting for its retry is not running");
f.clock.advance(HOUR);
orders.run_due(charge).unwrap();
assert_eq!(*charged.lock().unwrap(), ["10", "20"], "the step ran again for the new value");
assert!(orders.get("order:7").unwrap().is_none(), "and the job is done");
}
#[test]
fn cancel_says_whether_there_was_a_job_and_tells_a_running_handler_to_stop() {
let f = fixture();
let later = f.store.queue::<Note>("send-later").attempts(1).open().unwrap();
assert!(!later.cancel("missing").unwrap());
later.id("m1").delay(HOUR).add(¬e("scheduled")).unwrap();
assert!(later.cancel("m1").unwrap());
assert!(later.get("m1").unwrap().is_none());
later.id("m2").add(¬e("running")).unwrap();
let stopped = Mutex::new(false);
later
.run_due(|_: Note, run| -> Result<()> {
assert!(later.cancel("m2")?);
*stopped.lock().unwrap() = run.stopped();
Err(crate::Error::invalid("what a cancelled handler returns settles nothing"))
})
.unwrap();
assert!(*stopped.lock().unwrap());
assert!(later.get("m2").unwrap().is_none(), "the cancel took it, not a retry");
later.id("m3").add(¬e("failing")).unwrap();
later.run_due(|_: Note, _run| -> Result<(), &str> { Err("down") }).unwrap();
assert!(later.cancel("m3").unwrap(), "a failed job is cancelled too");
assert!(later.get("m3").unwrap().is_none());
}
fn texts_of(queue: &Queue<Note>) -> Vec<String> {
queue.all(Filter::default()).map(|job| job.unwrap().value.text).collect()
}
fn listed(queue: &Queue<Note>, filter: Filter, text: &str) -> super::Job<Note> {
queue.all(filter).map(Result::unwrap).find(|job| job.value.text == text).unwrap()
}
#[test]
fn cancel_takes_a_listed_job_without_an_id_by_its_row_and_no_other() {
let f = fixture();
let emails = f.store.queue::<Note>("emails").attempts(1).open().unwrap();
for text in ["typo", "same time", "sent"] {
emails.delay(HOUR).add(¬e(text)).unwrap();
}
let typo = listed(&emails, Filter::default().state(State::Scheduled), "typo");
assert_eq!(typo.id, None);
assert!(emails.cancel(&typo).unwrap());
assert!(!emails.cancel(&typo).unwrap(), "cancelled already");
assert_eq!(texts_of(&emails), ["same time", "sent"], "a job due at the same time stays");
emails.add(¬e("bounced")).unwrap();
emails.run_due(|_: Note, _run| -> Result<(), &str> { Err("no such mailbox") }).unwrap();
let bounced = listed(&emails, Filter::default().state(State::Failed), "bounced");
assert!(emails.cancel(&bounced).unwrap(), "a failed job by the row it failed into");
assert_eq!(texts_of(&emails), ["same time", "sent"]);
emails.id("welcome").delay(HOUR).add(¬e("welcome")).unwrap();
let welcome = listed(&emails, Filter::default(), "welcome");
assert!(emails.cancel(&welcome).unwrap(), "a job with an id by its id");
assert!(emails.get("welcome").unwrap().is_none());
}
#[test]
fn a_listed_job_that_moved_since_is_in_no_row_and_its_cancel_takes_nothing() {
let f = fixture();
let emails = f.store.queue::<Note>("emails").attempts(3).open().unwrap();
emails.add(¬e("flaky")).unwrap();
let flaky = listed(&emails, Filter::default(), "flaky");
emails.run_due(|_: Note, _run| -> Result<(), &str> { Err("timeout") }).unwrap();
assert!(!emails.cancel(&flaky).unwrap(), "retried into another row");
assert_eq!(texts_of(&emails), ["flaky"], "and still there");
let retried = listed(&emails, Filter::default(), "flaky");
assert_eq!(retried.attempt, 1);
assert!(emails.cancel(&retried).unwrap(), "the row its retry lies in");
assert!(texts_of(&emails).is_empty());
}
#[test]
fn a_cancel_by_row_stops_a_running_job_and_takes_a_parked_one() {
let f = fixture();
let pushes = f.store.queue::<Note>("pushes").concurrency(Concurrency::total(2).group(1)).open().unwrap();
pushes.group("ada").add(¬e("first")).unwrap();
pushes.group("ada").add(¬e("second")).unwrap();
let (stopped, parked) = (Mutex::new(false), Mutex::new(None));
pushes
.run_due(|note: Note, run| -> Result<()> {
if note.text == "first" {
let second = listed(&pushes, Filter::default().state(State::Waiting), "second");
*parked.lock().unwrap() = Some(pushes.cancel(&second)?);
let first = listed(&pushes, Filter::default().state(State::Running), "first");
assert!(pushes.cancel(&first)?);
*stopped.lock().unwrap() = run.stopped();
}
Ok(())
})
.unwrap();
assert_eq!(*parked.lock().unwrap(), Some(true), "parked while its group runs, found at its time");
assert!(*stopped.lock().unwrap(), "the running one's handler is told to stop");
assert!(texts_of(&pushes).is_empty());
}
#[test]
fn a_transaction_cancels_a_listed_job_once_it_commits() {
let f = fixture();
let pushes = f.store.queue::<Note>("pushes").open().unwrap();
pushes.delay(HOUR).add(¬e("kept")).unwrap();
let kept = listed(&pushes, Filter::default(), "kept");
let rolled_back = f.store.tx(|tx| -> Result<()> {
assert!(tx.with(&pushes).cancel(&kept)?);
Err(crate::Error::invalid("rolled back"))
});
assert!(rolled_back.is_err());
assert_eq!(texts_of(&pushes), ["kept"], "a cancel that did not commit took nothing");
assert!(f.store.tx(|tx| tx.with(&pushes).cancel(&kept)).unwrap());
assert!(texts_of(&pushes).is_empty());
}
#[test]
fn counts_say_how_many_jobs_are_in_each_state() {
let f = fixture();
let emails = f.store.queue::<Note>("emails").attempts(1).open().unwrap();
assert_eq!(emails.counts().unwrap(), super::Counts::default());
emails.add(¬e("bounced")).unwrap();
emails.run_due(|_: Note, _run| -> Result<(), &str> { Err("no such mailbox") }).unwrap();
for text in ["running", "waiting"] {
emails.add(¬e(text)).unwrap();
}
emails.id("later").delay(HOUR).add(¬e("later")).unwrap();
emails.delay(DAY).add(¬e("tomorrow")).unwrap();
let counted = Mutex::new(None);
emails
.concurrency(1)
.run_due(|note: Note, _run| -> Result<()> {
if note.text == "running" {
*counted.lock().unwrap() = Some(emails.counts()?);
}
Ok(())
})
.unwrap();
let counts = counted.into_inner().unwrap().unwrap();
assert_eq!(counts, super::Counts { scheduled: 2, waiting: 1, running: 1, failed: 1 });
let counts = emails.counts().unwrap();
assert_eq!(counts, super::Counts { scheduled: 2, waiting: 0, running: 0, failed: 1 }, "a done job left");
f.clock.advance(HOUR);
assert_eq!(emails.counts().unwrap(), super::Counts { scheduled: 1, waiting: 1, running: 0, failed: 1 });
}
#[test]
fn get_says_where_a_job_is_and_how_many_run_before_it() {
let f = fixture();
let videos = f.store.queue::<Note>("videos").attempts(1).open().unwrap();
for at in 0..3 {
videos.add(¬e(&format!("before {at}"))).unwrap();
}
videos.id("mine").add(¬e("mine")).unwrap();
videos.id("later").delay(HOUR).add(¬e("later")).unwrap();
let mine = videos.get("mine").unwrap().unwrap();
assert_eq!((mine.state, mine.ahead), (State::Waiting, 3));
assert_eq!(videos.get("later").unwrap().unwrap().state, State::Scheduled);
let running = Mutex::new(None);
videos
.concurrency(1)
.run_due(|note: Note, run| -> Result<()> {
if note.text == "mine" {
run.set_progress(&0.4)?;
*running.lock().unwrap() = videos.get("mine")?;
return Err(crate::Error::invalid("transcoding failed"));
}
Ok(())
})
.unwrap();
let running = running.into_inner().unwrap().unwrap();
assert_eq!((running.state, running.attempt, running.progress), (State::Running, 1, Some(serde_json::json!(0.4))));
let failed = videos.get("mine").unwrap().unwrap();
assert_eq!(failed.state, State::Failed);
assert!(failed.error.unwrap().contains("transcoding failed"));
assert!(failed.last_run.is_some());
}
#[test]
fn list_reads_the_ids_under_a_prefix_a_page_at_a_time() {
let f = fixture();
let later = f.store.queue::<Note>("send-later").open().unwrap();
for id in ["chat:42:1", "chat:42:2", "chat:42:3", "chat:420:1", "chat:7:1"] {
later.id(id).delay(HOUR).add(¬e(id)).unwrap();
}
let first = later.list(&Filter::prefix("chat:42:").limit(2)).unwrap();
let ids: Vec<_> = first.jobs.iter().map(|job| job.id.clone().unwrap()).collect();
assert_eq!(ids, ["chat:42:1", "chat:42:2"]);
let rest = later.list(&Filter::prefix("chat:42:").limit(2).after(first.next.unwrap())).unwrap();
assert_eq!(rest.jobs.len(), 1);
assert_eq!(rest.next, None);
let all: Vec<_> = later.all(Filter::prefix("chat:4")).map(|job| job.unwrap().id.unwrap()).collect();
assert_eq!(all.len(), 4, "a prefix is text: chat:4 meets chat 420 too");
}
#[test]
fn a_page_with_no_prefix_lists_the_jobs_without_ids_after_those_with() {
let f = fixture();
let pushes = f.store.queue::<Note>("pushes").attempts(1).open().unwrap();
pushes.add(¬e("bounced")).unwrap();
pushes.id("gone").add(¬e("gone")).unwrap();
pushes.run_due(|_: Note, _run| -> Result<(), &str> { Err("no device") }).unwrap();
f.clock.advance(SECOND);
pushes.add(¬e("first")).unwrap();
pushes.id("b").add(¬e("b")).unwrap();
pushes.add(¬e("second")).unwrap();
pushes.id("a").delay(HOUR).add(¬e("a")).unwrap();
pushes.delay(HOUR).add(¬e("later")).unwrap();
let texts = |filter: Filter| -> Vec<String> { pushes.all(filter).map(|job| job.unwrap().value.text).collect() };
let every = ["a", "b", "gone", "first", "second", "later", "bounced"];
assert_eq!(texts(Filter::default()), every, "ids first, then the rest as they run, then the failed");
assert_eq!(texts(Filter::default().limit(1)), every, "a page at a time across the walks");
assert_eq!(texts(Filter::default().state(State::Waiting)), ["b", "first", "second"]);
assert_eq!(texts(Filter::default().state(State::Scheduled)), ["a", "later"]);
assert_eq!(texts(Filter::default().state(State::Failed)), ["gone", "bounced"], "the last failed first");
assert_eq!(texts(Filter::prefix("g")), ["gone"], "a job without an id starts with no prefix");
let first = pushes.list(&Filter::default().limit(4)).unwrap();
assert_eq!(first.jobs.last().unwrap().id, None);
let rest = pushes.list(&Filter::default().after(first.next.clone().unwrap())).unwrap();
let rest: Vec<_> = rest.jobs.iter().map(|job| job.value.text.as_str()).collect();
assert_eq!(rest, ["second", "later", "bounced"]);
let error = pushes.list(&Filter::prefix("chat:").after(first.next.unwrap())).unwrap_err();
assert_eq!(error.kind(), ErrorKind::Invalid, "a prefix walks ids alone: {error}");
let error = pushes.list(&Filter::default().after("chat:42")).unwrap_err();
assert_eq!(error.kind(), ErrorKind::Invalid, "an id is no cursor: {error}");
}
#[test]
fn a_page_of_one_state_reads_on_past_the_others_until_it_holds_its_limit() {
let f = fixture();
let notes = f.store.queue::<Note>("notes").open().unwrap();
let waiting = f.store.tx(|tx| -> Result<()> {
for at in 0..150 {
tx.with(¬es).id(format!("now:{at:03}")).add(¬e("now"))?;
tx.with(¬es).add(¬e("now"))?;
}
Ok(())
});
waiting.unwrap();
notes.id("~later").delay(HOUR).add(¬e("later 0")).unwrap();
for at in 1..4 {
notes.delay(HOUR).add(¬e(&format!("later {at}"))).unwrap();
}
let scheduled = Filter::default().state(State::Scheduled).limit(3);
let texts =
|page: &super::Page<Note>| -> Vec<String> { page.jobs.iter().map(|job| job.value.text.clone()).collect() };
let first = notes.list(&scheduled).unwrap();
assert_eq!(texts(&first), ["later 0", "later 1", "later 2"], "past 300 waiting jobs, a page holds its limit");
let rest = notes.list(&scheduled.clone().after(first.next.expect("a full page has a next"))).unwrap();
assert_eq!(texts(&rest), ["later 3"]);
assert_eq!(rest.next, None, "the last page has no next");
let walked: Vec<String> = notes.all(scheduled.limit(1)).map(|job| job.unwrap().value.text).collect();
assert_eq!(walked, ["later 0", "later 1", "later 2", "later 3"], "each met once");
}
#[test]
fn the_failed_jobs_list_the_last_failed_first() {
let f = fixture();
let emails = f.store.queue::<Note>("emails").attempts(1).open().unwrap();
for text in ["a", "b", "c"] {
emails.add(¬e(text)).unwrap();
emails.run_due(|_: Note, _run| -> Result<(), &str> { Err("bounced") }).unwrap();
f.clock.advance(SECOND);
}
let failed = emails.list(&Filter::default().state(State::Failed)).unwrap();
let texts: Vec<_> = failed.jobs.iter().map(|job| job.value.text.as_str()).collect();
assert_eq!(texts, ["c", "b", "a"]);
}
#[test]
fn a_page_that_meets_a_value_that_no_longer_reads_names_its_job() {
#[derive(Debug, Serialize, Deserialize)]
struct Renamed {
body: String,
}
let f = fixture();
f.store.queue::<Note>("notes").attempts(1).open().unwrap().id("old").add(¬e("from before")).unwrap();
let renamed = f.store.queue::<Renamed>("notes").attempts(1).open().unwrap();
renamed.run_due(|_: Renamed, _run| -> Result<()> { Ok(()) }).unwrap();
let error = renamed.list(&Filter::default().state(State::Failed)).unwrap_err();
assert_eq!(error.kind(), ErrorKind::Corrupt);
assert!(error.to_string().contains("id \"old\""), "the id a program cancels, or reads as JSON: {error}");
let raw = f.store.queue::<serde_json::Value>("notes").attempts(1).open().unwrap();
let failed = raw.list(&Filter::default().state(State::Failed)).unwrap();
assert_eq!(failed.jobs[0].value, serde_json::json!({ "text": "from before" }), "kept as it was");
}
#[test]
fn a_walk_meets_a_job_whose_value_no_longer_reads_as_its_error_and_goes_on() {
#[derive(Debug, Serialize, Deserialize)]
struct Renamed {
body: String,
}
let f = fixture();
let raw = f.store.queue::<serde_json::Value>("notes").open().unwrap();
raw.id("a").delay(HOUR).add(&serde_json::json!({ "body": "first" })).unwrap();
raw.delay(HOUR).add(&serde_json::json!({ "text": "an older shape" })).unwrap();
raw.delay(HOUR).add(&serde_json::json!({ "body": "last" })).unwrap();
let renamed = f.store.queue::<Renamed>("notes").open().unwrap();
let walked: Vec<Result<String>> = renamed.all(Filter::default()).map(|job| job.map(|job| job.value.body)).collect();
assert_eq!(walked.len(), 3, "the walk goes on past it: {walked:?}");
assert_eq!((walked[0].as_deref().ok(), walked[2].as_deref().ok()), (Some("first"), Some("last")));
let error = walked[1].as_ref().unwrap_err();
assert_eq!(error.kind(), ErrorKind::Corrupt);
assert!(error.to_string().contains("without an id, due at unix ms"), "named by where it lies: {error}");
let failed = renamed.list(&Filter::default()).unwrap_err();
assert!(failed.to_string().contains("without an id, due at unix ms"), "a page names it the same: {failed}");
}
#[test]
fn a_value_comes_back_as_the_json_it_went_in() {
let f = fixture();
let notes = f.store.queue::<Note>("notes").open().unwrap();
let long = note(&"x".repeat(2000));
notes.id("long").add(&long).unwrap();
assert_eq!(notes.get("long").unwrap().unwrap().value, long, "a value past 512 bytes lives in a row of its own");
assert_eq!(ran(¬es), [long.text]);
let raw = f.store.queue::<serde_json::Value>("notes").open().unwrap();
raw.id("plain").add(&serde_json::json!({ "text": "from another language" })).unwrap();
assert_eq!(notes.get("plain").unwrap().unwrap().value, note("from another language"));
}
#[test]
fn a_value_that_no_longer_reads_fails_its_job_not_its_queue() {
let f = fixture();
let raw = f.store.queue::<serde_json::Value>("notes").open().unwrap();
raw.id("old").add(&serde_json::json!({ "body": "an older shape" })).unwrap();
raw.add(&serde_json::json!({ "text": "fine" })).unwrap();
let notes = f.store.queue::<Note>("notes").open().unwrap();
assert_eq!(ran(¬es), ["fine"]);
let failed = raw.get("old").unwrap().unwrap();
assert_eq!(failed.state, State::Failed);
assert!(failed.error.unwrap().contains("no longer reads"));
}
#[test]
fn a_queue_past_max_waiting_refuses_the_next_job() {
let f = fixture();
let notes = f.store.queue::<Note>("notes").max_waiting(2).open().unwrap();
notes.add(¬e("1")).unwrap();
notes.add(¬e("2")).unwrap();
let error = notes.add(¬e("3")).unwrap_err();
assert_eq!(error.kind(), ErrorKind::Limit, "{error}");
assert!(error.to_string().starts_with("jobs queue notes"), "{error}");
}
#[test]
fn a_call_that_cannot_be_kept_is_invalid_and_says_why() {
let f = fixture();
let notes = f.store.queue::<Note>("notes").open().unwrap();
let cases = [
notes.id("").add(¬e("x")),
notes.at(f.now()).delay(HOUR).add(¬e("x")),
notes.delay(SECOND).every(MINUTE).add(¬e("x")),
notes.id("a").cron("0 9 * * *", "").add(¬e("x")),
notes.id("a").cron("0 9 * * *", "Mars/Olympus").add(¬e("x")),
];
for case in cases {
assert_eq!(case.unwrap_err().kind(), ErrorKind::Invalid);
}
let error = notes.id("a").cron("0 9 * * *", "").add(¬e("x")).unwrap_err();
assert_eq!(
error.to_string(),
"jobs queue notes: id \"a\": cron \"0 9 * * *\": a cron needs a time zone, \"UTC\" among them: invalid"
);
}
#[test]
fn a_name_keeps_its_kind_and_one_process_its_options() {
let f = fixture();
f.store.queue::<Note>("notes").open().unwrap();
let error = f.store.queue::<Note>("notes").attempts(3).open().unwrap_err();
assert_eq!(error.kind(), ErrorKind::Invalid);
let error = f.store.schedule("notes").every(HOUR).open().unwrap_err();
assert_eq!(error.kind(), ErrorKind::Invalid, "{error}");
let error = f.store.queue::<Note>("Notes!").open().unwrap_err();
assert_eq!(error.kind(), ErrorKind::Invalid);
}
#[test]
fn a_failed_job_is_kept_then_removed() {
let f = fixture();
let emails = f.store.queue::<Note>("emails").attempts(1).keep(DAY).open().unwrap();
emails.id("welcome").add(¬e("hi")).unwrap();
emails.run_due(|_: Note, _run| -> Result<(), &str> { Err("bounced") }).unwrap();
assert_eq!(super::maintain(&f.store).unwrap().failed, 0);
f.clock.advance(DAY);
assert_eq!(super::maintain(&f.store).unwrap().failed, 1);
assert!(emails.get("welcome").unwrap().is_none());
}
#[test]
fn an_id_a_done_job_left_behind_names_nothing_and_maintenance_drops_it() {
let f = fixture();
let notes = f.store.queue::<Note>("notes").open().unwrap();
notes.id("a").add(¬e("a")).unwrap();
assert_eq!(ran(¬es), ["a"]);
assert!(notes.get("a").unwrap().is_none());
assert_eq!(super::maintain(&f.store).unwrap().ids, 1);
assert_eq!(super::maintain(&f.store).unwrap().ids, 0);
}
#[test]
fn a_repeating_job_neither_overlaps_nor_piles_up() {
let f = fixture();
let probes = f.store.queue::<Note>("probes").open().unwrap();
probes.id("probe:7").every(HOUR).set(¬e("probe")).unwrap();
let first = probes.get("probe:7").unwrap().unwrap();
assert!(first.repeat.unwrap().starts_with("@every 1h +"));
assert!(first.at > f.now() && first.at <= f.now() + HOUR, "its first run within the next interval");
f.clock.advance(5 * HOUR);
assert_eq!(ran(&probes), ["probe"], "a night down runs it once, not once a missed time");
let next = probes.get("probe:7").unwrap().unwrap();
assert!(next.at > f.now() && next.at <= f.now() + HOUR);
assert_eq!((next.state, next.attempt), (State::Scheduled, 0));
assert!(probes.cancel("probe:7").unwrap(), "only an id stops it");
}
#[test]
fn ids_repeating_together_run_each_at_a_phase_of_its_own() {
let f = fixture();
let probes = f.store.queue::<Note>("probes").open().unwrap();
let mut times = Vec::new();
for site in 0..20 {
let id = format!("probe:{site}");
probes.id(&id).every(Duration::from_secs(30)).set(¬e(&id)).unwrap();
times.push(probes.get(&id).unwrap().unwrap().at);
}
times.sort();
times.dedup();
assert!(times.len() > 10, "{} instants for 20 ids", times.len());
}
#[test]
fn a_cron_runs_on_its_zones_wall_clock() {
let f = fixture();
let digests = f.store.queue::<Note>("digests").open().unwrap();
digests.id("user:42").cron("0 20 * * *", "Europe/Berlin").set(¬e("digest")).unwrap();
let digest = digests.get("user:42").unwrap().unwrap();
assert_eq!(crate::unix_millis(digest.at), 1_791_568_800_000);
assert_eq!(digest.repeat.as_deref(), Some("0 20 * * * Europe/Berlin"));
}
#[test]
fn a_schedule_keeps_the_codes_repeat_each_time_it_opens() {
let mut f = fixture();
let cleanup = f.store.schedule("cleanup").cron("10 3 * * *", "UTC").open().unwrap();
let first = cleanup.get().unwrap().unwrap();
assert_eq!(first.repeat.as_deref(), Some("10 3 * * * UTC"));
f.reopen();
let cleanup = f.store.schedule("cleanup").cron("10 3 * * *", "UTC").open().unwrap();
assert_eq!(cleanup.get().unwrap().unwrap().at, first.at, "the same repeat keeps its time");
f.reopen();
let cleanup = f.store.schedule("cleanup").cron("30 4 * * *", "UTC").open().unwrap();
let changed = cleanup.get().unwrap().unwrap();
assert_eq!(changed.repeat.as_deref(), Some("30 4 * * * UTC"), "the code's repeat replaces the kept one");
assert_ne!(changed.at, first.at);
f.clock.advance(DAY);
let runs = Mutex::new(Vec::new());
let ran = cleanup.run_due(|run| -> Result<()> {
runs.lock().unwrap().push(run.at());
Ok(())
});
assert_eq!(ran.unwrap(), 1);
assert_eq!(*runs.lock().unwrap(), [changed.at], "a run knows when it was due");
}
#[test]
fn a_schedule_whose_job_failed_for_good_starts_again_under_its_one_id() {
let mut f = fixture();
let nightly = f.store.schedule("nightly").every(HOUR).open().unwrap();
let fail = "insert into _tinystore_jobs_failed (queue, id, key, at, attempt, failed, error, value, spill, grp)
select queue, id, key, at, attempt, 0, 'killed', value, spill, grp from _tinystore_jobs where key = 'nightly';
delete from _tinystore_jobs where key = 'nightly'";
let jobs = super::engine::Jobs::of(&f.store).unwrap();
let failed = |error| crate::sqlite::sql_error("a test", error);
jobs.file().transaction(|tx| tx.execute_batch(fail).map_err(failed)).unwrap();
assert_eq!(nightly.get().unwrap().unwrap().state, State::Failed);
drop((nightly, jobs));
f.reopen();
let nightly = f.store.schedule("nightly").every(HOUR).open().unwrap();
assert_eq!(nightly.get().unwrap().unwrap().state, State::Scheduled, "the schedule starts again");
let jobs = super::engine::Jobs::of(&f.store).unwrap();
let count = "select count(*) from _tinystore_jobs_failed";
let kept: i64 = jobs.file().read(|c| c.query_row(count, [], |row| row.get(0)).map_err(failed)).unwrap();
assert_eq!(kept, 0, "one job under the schedule's id, not a failed one beside it");
}
#[test]
fn a_time_outside_the_years_a_job_keeps_is_refused() {
let f = fixture();
let notes = f.store.queue::<Note>("notes").open().unwrap();
let year_10000 = std::time::UNIX_EPOCH + Duration::from_secs(253_402_300_800);
assert_eq!(notes.at(year_10000).add(¬e("x")).unwrap_err().kind(), ErrorKind::Invalid);
}
#[test]
fn a_span_longer_than_the_years_a_job_keeps_is_refused_where_it_is_given() {
let f = fixture();
let notes = f.store.queue::<Note>("notes").open().unwrap();
let error = notes.id("a").every(Duration::MAX).add(¬e("x")).unwrap_err();
assert_eq!(error.kind(), ErrorKind::Invalid, "{error}");
assert!(notes.get("a").unwrap().is_none(), "the call added nothing");
let builders = [
f.store.queue::<Note>("rated").rate(1, Duration::MAX),
f.store.queue::<Note>("grouped").group_rate(1, Duration::MAX),
f.store.queue::<Note>("patient").timeout(Duration::MAX),
];
for builder in builders {
let error = builder.open().unwrap_err();
assert_eq!(error.kind(), ErrorKind::Invalid, "{error}");
}
assert!(f.store.queue::<Note>("rated").rate(1, SECOND).open().is_ok(), "the refused open kept nothing");
}
#[test]
fn a_progress_past_4_kib_is_refused() {
let f = fixture();
let videos = f.store.queue::<Note>("videos").open().unwrap();
videos.add(¬e("v")).unwrap();
let refused = Mutex::new(None);
videos
.run_due(|_: Note, run| -> Result<()> {
*refused.lock().unwrap() = run.set_progress(&"x".repeat(5000)).err().map(|error| error.kind());
Ok(())
})
.unwrap();
assert_eq!(*refused.lock().unwrap(), Some(ErrorKind::Limit));
}
#[test]
fn a_transaction_of_the_store_adds_its_jobs_together_or_not_at_all() {
let f = fixture();
let pushes = f.store.queue::<Note>("pushes").open().unwrap();
f.store
.tx(|tx| -> Result<()> {
for member in ["ann", "bob", "eve"] {
assert!(tx.with(&pushes).id(member).add(¬e("the build is green"))?);
}
assert!(tx.with(&pushes).get("ann")?.is_some(), "a read inside sees what the transaction added");
Ok(())
})
.unwrap();
assert_eq!(ran(&pushes).len(), 3);
pushes.id("kept").add(¬e("stays")).unwrap();
let refused = f.store.tx(|tx| -> Result<()> {
tx.with(&pushes).id("zed").add(¬e("never sent"))?;
assert!(tx.with(&pushes).cancel("kept")?);
Err(crate::Error::invalid("changed its mind"))
});
assert_eq!(refused.unwrap_err().kind(), ErrorKind::Invalid);
assert!(pushes.get("zed").unwrap().is_none(), "the add rolled back");
assert!(pushes.get("kept").unwrap().is_some(), "and the cancel");
let around = f.store.tx(|_| pushes.add(¬e("outside the transaction"))).unwrap_err();
assert_eq!(around.kind(), ErrorKind::Invalid, "a call around the transaction is refused: {around}");
}
#[cfg(feature = "kv")]
#[test]
fn a_transaction_of_the_store_is_of_one_file() {
let f = fixture();
let pushes = f.store.queue::<Note>("pushes").open().unwrap();
let seen = f.store.bucket::<String>("seen").open().unwrap();
let error = f
.store
.tx(|tx| -> Result<()> {
tx.with(&pushes).add(¬e("first"))?;
tx.with(&seen).set("ann", &"yes".to_owned())
})
.unwrap_err();
assert_eq!(error.kind(), ErrorKind::Invalid, "{error}");
assert!(error.to_string().contains("kept in kv.db, outside this transaction of jobs.db"), "{error}");
assert!(ran(&pushes).is_empty(), "the job rolled back with the transaction");
let nothing: Result<()> = f.store.tx(|_| Ok(()));
nothing.unwrap();
}
fn synchronous(file: &crate::sqlite::File) -> i64 {
file.transaction(|tx| -> crate::Result<i64> {
tx.pragma_query_value(None, "synchronous", |row| row.get(0))
.map_err(|error| crate::sqlite::sql_error("synchronous", error))
})
.unwrap()
}
#[test]
fn jobs_db_commits_as_far_as_its_store_says_and_syncs_each_commit_unless_told() {
for (durability, level) in [(None, 2), (Some(crate::Durability::Os), 1)] {
let dir = tempfile::tempdir().unwrap();
let options = crate::Options { durability, background: false, ..crate::Options::default() };
let store = crate::Store::open(dir.path(), options).unwrap();
let mail = store.queue::<i64>("mail").open().unwrap();
mail.add(&1).unwrap();
assert_eq!(synchronous(super::engine::Jobs::of(&store).unwrap().file()), level, "{durability:?}");
store.close().unwrap();
}
}
#[test]
#[ignore = "a timing, run by hand"]
fn timing_a_job_from_its_add_to_its_end() {
let dir = tempfile::tempdir().unwrap();
let options = crate::Options { background: false, durability: Some(crate::Durability::Os), ..Default::default() };
let store = crate::Store::open(dir.path(), options).unwrap();
let notes = store.queue::<Note>("notes").open().unwrap();
for round in 0..3 {
let started = std::time::Instant::now();
for _ in 0..10_000 {
notes.add(¬e("a note")).unwrap();
}
let added = started.elapsed();
let started = std::time::Instant::now();
while let Some(claimed) = notes.claim().unwrap() {
claimed.settle(()).unwrap();
}
let ran = started.elapsed();
let each = |spent: Duration| spent.as_secs_f64() * 100.0;
println!("round {round}: an add {:.1} µs, a claim and its settle {:.1} µs", each(added), each(ran));
let started = std::time::Instant::now();
std::thread::scope(|callers| {
for _ in 0..16 {
callers.spawn(|| {
for _ in 0..2_000 {
notes.add(¬e("a note")).unwrap();
}
});
}
});
println!("round {round}: 16 callers at once, {:.0} adds a second", 32_000.0 / started.elapsed().as_secs_f64());
while let Some(claimed) = notes.claim().unwrap() {
claimed.settle(()).unwrap();
}
}
}