fix(alarms): spend a pending skip only after its fire grace

The upcoming check that spends a skip dismissed in advance now wakes once the skipped occurrence's fire grace is over rather than at the occurrence itself, so it can never get ahead of a fire still in flight for it.

Fixes #16
This commit is contained in:
makiolaj committed 2026-10-22 08:13:31 +02:00
1 parent fdb725e64c
commit 5ad9bd02d6
4 files changed
+16 -17

No files matched your search

@@ -51,13 +51,13 @@ object UpcomingNotices {
* - a window opening, so its notice goes up;
* - a noticed occurrence's fire grace running out, so a notice whose fire
* came late (Doze, no exact alarms) does not outlive its alarm;
* - an armed skip's ([pendingSkips]) occurrence, so a skip dismissed in
* advance is spent on time and the list stops saying "skipping next"
* about an occurrence that has gone by. Nothing is registered to fire
* at a skipped occurrence, so there is no fire to race.
* - an armed skip's ([pendingSkips]) fire grace running out, so a skip
* dismissed in advance is spent on time and the list stops saying
* "skipping next" about an occurrence that has gone by.
*
* The closing waits out the grace rather than land on the alarm's own
* instant, so it never races that alarm's fire.
* Both of the last two wait out the grace rather than land on the
* occurrence itself: a fire for it still in flight is refused as too late
* by then, so the check can never race it.
*/
fun plan(
upcoming: List<UpcomingAlarm>,
@@ -74,8 +74,7 @@ object UpcomingNotices {
.filter { it.source == FireSource.OCCURRENCE }
.mapNotNull { entry -> entry.nextFire?.let { it - lead } }
val closings = notices.map { it.fireAt + fireGrace }
val skipsSpent = pendingSkips
val nextCheckAt = (openings + closings + skipsSpent).filter { it > now }.minOrNull()
val nextCheckAt = (openings + closings + pendingSkips.map { it + fireGrace }).filter { it > now }.minOrNull()
return UpcomingNoticePlan(notices, nextCheckAt)
}
}
@@ -71,9 +71,9 @@ class AlarmEngineUpcomingTest {
assertThat(alarm.skipNextOccurrence).isTrue()
assertThat(harness.scheduler.next?.fireAt).isEqualTo(sixThirtyTomorrow)
assertThat(harness.notifier.upcoming).isEmpty()
// Back at the skipped occurrence, to spend the skip.
assertThat(harness.scheduler.upcomingCheck).isEqualTo(sixThirty)
harness.wallClock.instant = sixThirty
// Back once the skipped occurrence's grace is over, to spend the skip.
assertThat(harness.scheduler.upcomingCheck).isEqualTo(sixThirty + AlarmRing.FIRE_GRACE)
harness.wallClock.instant = sixThirty + AlarmRing.FIRE_GRACE
harness.engine.onUpcomingWindow()
assertThat(harness.alarms.find(1L)!!.skipNextOccurrence).isFalse()
assertThat(harness.scheduler.next?.fireAt).isEqualTo(sixThirtyTomorrow)
@@ -103,7 +103,7 @@ class UpcomingNoticesTest {
)
// So the skip is spent then, not at tomorrow's window.
assertThat(plan.nextCheckAt).isEqualTo(skipped)
assertThat(plan.nextCheckAt).isEqualTo(skipped + AlarmRing.FIRE_GRACE)
}
@Test
+5 -5
View File
@@ -1239,11 +1239,11 @@ the alarm's row for whoever opens the app instead.
`rescheduleLocked`, so that is where the notices are synced and the third
slot is armed for the earliest window still to open, *after* the real
registration. The check also wakes once a noticed occurrence's fire grace has
run out (so a notice cannot outlive a fire that came late; waiting out the
grace keeps it from racing that fire) and at an armed skip's occurrence (so a
skip dismissed in advance is spent on time, and the list stops saying
"skipping next" about an occurrence already gone; nothing is registered to
fire then, so there is nothing to race). The sync is wrapped so a notification failure costs nothing,
run out (so a notice cannot outlive a fire that came late) and once an armed
skip's has (so a skip dismissed in advance is spent on time, and the list
stops saying "skipping next" about an occurrence already gone). Both wait out
the grace rather than land on the occurrence itself: a fire for it still in
flight is refused as too late by then, so the check never races it. The sync is wrapped so a notification failure costs nothing,
and on the fire path it is deferred until the ring has started, so its binder
calls never stand between the fire and the sound. The check's receiver just
calls `onUpcomingWindow()`, which is `reschedule()`.