diff --git a/pkgs/id/src/world/actor.rs b/pkgs/id/src/world/actor.rs index c27352dde2..e344fdeed0 100644 --- a/pkgs/id/src/world/actor.rs +++ b/pkgs/id/src/world/actor.rs @@ -1398,12 +1398,8 @@ impl WorldActor { // existing schedule standing, in the journal as in // memory (#807). let replacing = self.core.schedules.contains_key(&id); - self.core.check_schedule_replacing( - &id, - at_ms, - self.core.clock().now_ms(), - replacing, - )?; + self.core + .check_schedule_replacing(&id, at_ms, self.core.clock().now_ms())?; if replacing { // Journal the cancel before the replacement set: // replay refuses two pending sets for one id. diff --git a/pkgs/id/src/world/core.rs b/pkgs/id/src/world/core.rs index 258eaf1c7b..55882880c4 100644 --- a/pkgs/id/src/world/core.rs +++ b/pkgs/id/src/world/core.rs @@ -736,16 +736,22 @@ impl WorldCore { /// A malformed id, a duplicate pending id, a full schedule, or a time past /// the horizon. pub(crate) fn check_schedule(&self, id: &str, at_ms: u64, now_ms: u64) -> Result<()> { - self.check_schedule_replacing(id, at_ms, now_ms, false) + self.check_schedule_inner(id, at_ms, now_ms, false) } /// Like [`Self::check_schedule`], but for a set that replaces the pending - /// schedule with the same id: the entry being replaced counts as already - /// cancelled, so it is neither a duplicate nor a use of the capacity. + /// schedule with the same id. Whether this is a replacement is derived + /// from this core's schedule map: the entry being replaced counts as + /// already cancelled, so it is neither a duplicate nor a use of capacity. /// Callers validate with this BEFORE persisting the replacement's /// cancellation, so a refused replacement (say, past the horizon) leaves /// the existing schedule standing (#807). - pub(crate) fn check_schedule_replacing( + pub(crate) fn check_schedule_replacing(&self, id: &str, at_ms: u64, now_ms: u64) -> Result<()> { + let replacing = self.schedules.contains_key(id); + self.check_schedule_inner(id, at_ms, now_ms, replacing) + } + + fn check_schedule_inner( &self, id: &str, at_ms: u64, diff --git a/pkgs/id/src/world/tests.rs b/pkgs/id/src/world/tests.rs index c13fd32edb..13bb9f0486 100644 --- a/pkgs/id/src/world/tests.rs +++ b/pkgs/id/src/world/tests.rs @@ -3781,12 +3781,11 @@ mod schedule_tests { let mut core = world(8); core.schedules.insert("taken".to_owned(), now + 5); assert!( - core.check_schedule_replacing("taken", now + 9, now, true) - .is_ok(), + core.check_schedule_replacing("taken", now + 9, now).is_ok(), "replacing a pending id is not a duplicate" ); let error = core - .check_schedule_replacing("taken", now + horizon + 1, now, true) + .check_schedule_replacing("taken", now + horizon + 1, now) .unwrap_err(); assert!(error.to_string().contains("days past"), "{error}"); assert_eq!( @@ -3800,14 +3799,36 @@ mod schedule_tests { full.schedules.insert(format!("s{index}"), now); } assert!( - full.check_schedule_replacing("s0", now + 9, now, true) - .is_ok(), + full.check_schedule_replacing("s0", now + 9, now).is_ok(), "a replacement at capacity fits: it frees its own slot" ); let error = full.check_schedule("one-more", now, now).unwrap_err(); assert!(error.to_string().contains("pending schedules"), "{error}"); } + #[test] + fn replacement_path_cannot_admit_a_new_id_past_the_schedule_cap() { + let now = 1_000_000_000_000u64; + let mut full = world(8); + for index in 0..crate::world_caps::MAX_PENDING_SCHEDULES { + full.schedules.insert(format!("s{index}"), now); + } + + let error = full + .check_schedule_replacing("new", now + 9, now) + .unwrap_err(); + assert!(error.to_string().contains("pending schedules"), "{error}"); + assert!(!full.schedules.contains_key("new")); + } + + #[test] + fn replacement_path_on_an_empty_schedule_map_does_not_underflow() { + let now = 1_000_000_000_000u64; + let core = world(8); + + assert!(core.check_schedule_replacing("new", now + 9, now).is_ok()); + } + #[tokio::test] async fn an_ungranted_schedule_is_refused_and_counted() { let (mut actor, _clock, token, journal) = scheduling_actor("sched-refused", &[], false);