Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

test(cron): add scheduler dispatch tests

Steven Enamakele9e0ac7c6a7a
diff --git a/crates/openhuman-core/src/cron/scheduler_dispatch_tests.rs b/crates/openhuman-core/src/cron/scheduler_dispatch_tests.rs
index e3699cd72b..604e53b6a9 100644
--- a/crates/openhuman-core/src/cron/scheduler_dispatch_tests.rs
+++ b/crates/openhuman-core/src/cron/scheduler_dispatch_tests.rs
@@ -79,181 +79,6 @@ fn counting(
})
}
-/// Whether a run of `job_id` holds the run claim.
-fn job_is_running(job_id: &str) -> bool {
- crate::cron::ops::try_acquire_run(job_id).is_none()
-}
-
-async fn wait_for(mut check: impl FnMut() -> bool) -> bool {
- for _ in 0..200 {
- if check() {
- return true;
- }
- tokio::time::sleep(Duration::from_millis(25)).await;
- }
- false
-}
-
-#[tokio::test]
-async fn a_long_job_does_not_hold_up_the_next_poll() {
- let tmp = TempDir::new().unwrap();
- let config = test_config(&tmp).await;
- let security = security(&config);
- let release = Arc::new(tokio::sync::Notify::new());
- let gate = Arc::clone(&release);
- let _slow = register(
- "dispatch-slow",
- Arc::new(move |_ctx| {
- let gate = Arc::clone(&gate);
- Box::pin(async move {
- gate.notified().await;
- Ok(())
- })
- }),
- );
- let fast_calls = Arc::new(AtomicUsize::new(0));
- let _fast = register("dispatch-fast", counting(Arc::clone(&fast_calls), Ok(())));
-
- let slow = due_system_job(&config, "dispatch-slow");
- let mut dispatcher = JobDispatcher::new(
- &crate::core::runtime::DispatchCtx::ambient(),
- &config,
- &security,
- );
- let mut health = None;
- // First poll picks up only the slow job.
- tick_once(&config, &mut health, &mut dispatcher).await;
- assert!(job_is_running(&slow.id), "the slow job is running");
-
- // The next poll must not wait for it.
- let fast = due_system_job(&config, "dispatch-fast");
- tokio::time::timeout(
- Duration::from_secs(5),
- tick_once(&config, &mut health, &mut dispatcher),
- )
- .await
- .expect("a poll returns while an earlier job is still running");
- assert!(
- wait_for(|| cron::get_job(&config, &fast.id)
- .unwrap()
- .last_status
- .as_deref()
- == Some("ok"))
- .await,
- "the fast job completed while the slow one was still running"
- );
- assert!(job_is_running(&slow.id));
- assert_eq!(fast_calls.load(Ordering::SeqCst), 1);
-
- release.notify_one();
- dispatcher.drain().await;
- assert!(!job_is_running(&slow.id));
- assert_eq!(
- cron::get_job(&config, &slow.id)
- .unwrap()
- .last_status
- .as_deref(),
- Some("ok")
- );
-}
-
-#[tokio::test]
-async fn dispatch_claims_the_slot_so_a_running_job_is_not_due_again() {
- let tmp = TempDir::new().unwrap();
- let config = test_config(&tmp).await;
- let release = Arc::new(tokio::sync::Notify::new());
- let gate = Arc::clone(&release);
- let _handler = register(
- "dispatch-claim",
- Arc::new(move |_ctx| {
- let gate = Arc::clone(&gate);
- Box::pin(async move {
- gate.notified().await;
- Ok(())
- })
- }),
- );
- let job = due_system_job(&config, "dispatch-claim");
- let mut dispatcher = JobDispatcher::new(
- &crate::core::runtime::DispatchCtx::ambient(),
- &config,
- &security(&config),
- );
- dispatcher.dispatch(vec![job.clone()]).await.unwrap();
- let stored = cron::get_job(&config, &job.id).unwrap();
- assert!(stored.next_run > Utc::now(), "the running slot is claimed");
- assert!(cron::due_jobs(&config, Utc::now())
- .unwrap()
- .iter()
- .all(|due| due.id != job.id));
- release.notify_one();
- dispatcher.drain().await;
-}
-
-#[tokio::test]
-async fn a_job_already_in_flight_is_not_dispatched_again() {
- let tmp = TempDir::new().unwrap();
- let config = test_config(&tmp).await;
- let calls = Arc::new(AtomicUsize::new(0));
- let _handler = register("dispatch-dedupe", counting(Arc::clone(&calls), Ok(())));
- let job = due_system_job(&config, "dispatch-dedupe");
- let running = crate::cron::ops::try_acquire_run(&job.id).expect("the job is free");
- let mut dispatcher = JobDispatcher::new(
- &crate::core::runtime::DispatchCtx::ambient(),
- &config,
- &security(&config),
- );
- dispatcher.dispatch(vec![job.clone()]).await.unwrap();
- dispatcher.drain().await;
- drop(running);
- assert_eq!(calls.load(Ordering::SeqCst), 0);
- assert!(
- cron::list_runs(&config, &job.id, 10).unwrap().is_empty(),
- "a default job's overlap is silent"
- );
-}
-
-#[tokio::test]
-async fn a_single_flight_job_records_the_missed_slot_as_skipped() {
- let tmp = TempDir::new().unwrap();
- let config = test_config(&tmp).await;
- let calls = Arc::new(AtomicUsize::new(0));
- let _handler = register("dispatch-single", counting(Arc::clone(&calls), Ok(())));
- let job = due_system_job(&config, "dispatch-single");
- set_policy(
- &config,
- &job.id,
- JobPolicy {
- retries: None,
- single_flight: true,
- },
- )
- .unwrap();
- let job = cron::get_job(&config, &job.id).unwrap();
- let running = crate::cron::ops::try_acquire_run(&job.id).expect("the job is free");
- let mut dispatcher = JobDispatcher::new(
- &crate::core::runtime::DispatchCtx::ambient(),
- &config,
- &security(&config),
- );
- dispatcher.dispatch(vec![job.clone()]).await.unwrap();
- dispatcher.drain().await;
- drop(running);
-
- assert_eq!(
- calls.load(Ordering::SeqCst),
- 0,
- "the overlapping run is skipped"
- );
- let runs = cron::list_runs(&config, &job.id, 10).unwrap();
- assert_eq!(runs.len(), 1);
- assert_eq!(runs[0].status, "skipped");
- assert!(
- cron::get_job(&config, &job.id).unwrap().next_run > Utc::now(),
- "the skipped slot is consumed so the next poll does not record it again"
- );
-}
-
#[tokio::test]
async fn zero_retries_means_exactly_one_attempt() {
let tmp = TempDir::new().unwrap();
@@ -338,7 +163,7 @@ async fn a_system_job_handler_error_is_recorded_as_the_run_result() {
&config,
&security(&config),
);
- dispatcher.dispatch(vec![job.clone()]).await.unwrap();
+ dispatcher.tick(&config).await.unwrap();
dispatcher.drain().await;
let stored = cron::get_job(&config, &job.id).unwrap();
assert_eq!(stored.last_status.as_deref(), Some("error"));
@@ -366,15 +191,12 @@ async fn a_system_job_without_a_handler_is_still_ok_on_dispatch() {
#[test]
fn only_one_scheduler_loop_holds_the_slot() {
- let first = SchedulerSlot::acquire(LOOP_SCOPE).expect("the slot is free");
+ let first = LoopSlot::acquire().expect("the slot is free");
assert!(is_running());
- assert!(
- SchedulerSlot::acquire(LOOP_SCOPE).is_none(),
- "a second loop is refused"
- );
+ assert!(LoopSlot::acquire().is_none(), "a second loop is refused");
drop(first);
assert!(!is_running());
- let again = SchedulerSlot::acquire(LOOP_SCOPE);
+ let again = LoopSlot::acquire();
assert!(again.is_some());
}
@@ -423,7 +245,7 @@ async fn an_upgraded_job_with_a_side_table_policy_keeps_firing_with_it() {
&config,
&security(&config),
);
- dispatcher.dispatch(vec![job]).await.unwrap();
+ dispatcher.tick(&config).await.unwrap();
dispatcher.drain().await;
let after = cron::get_job(&config, shell_id).unwrap();
@@ -458,7 +280,7 @@ async fn a_flow_schedule_job_fires_through_the_trigger_sink() {
&config,
&security(&config),
);
- dispatcher.dispatch(vec![job.clone()]).await.unwrap();
+ dispatcher.tick(&config).await.unwrap();
dispatcher.drain().await;
let stored = cron::get_job(&config, &job.id).unwrap();
@@ -498,7 +320,7 @@ async fn a_flow_schedule_job_is_skipped_when_the_flows_component_is_off() {
let dispatch = crate::core::runtime::DispatchCtx::new(Arc::clone(&ctx));
CoreContext::scope(ctx, async move {
let mut dispatcher = JobDispatcher::new(&dispatch, &cfg, &security);
- dispatcher.dispatch(vec![job]).await.unwrap();
+ dispatcher.tick(&cfg).await.unwrap();
dispatcher.drain().await;
})
.await;