feat(system): repair running timers and the stopwatch after a reboot
Closes M2's known blind spot. On the first pass of a new boot, every RUNNING timer has its monotonic anchor rewritten from its wall-clock fallback — one already past its end becomes EXPIRED, one with no fallback becomes PAUSED at its banked remaining. The stopwatch keeps no wall-clock fallback at all, so it is paused at what it had banked rather than being allowed to invent a segment it never ran. The gate is persisted, so the repair runs once per boot and survives a process death in between. ARCHITECTURE §5 booked the stopwatch half of this to M7; it belongs here with the timer half, and the roadmap is amended to say so. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
57605f22f6
commit
92483cac4b
9 files changed
+367
No files matched your search
+65
@@ -222,4 +222,69 @@ class StopwatchRepositoryTest {
|
||||
)
|
||||
assertThat(repository.laps().first().map { it.index }).containsExactly(1)
|
||||
}
|
||||
|
||||
// --- M3: the post-reboot repair ---
|
||||
|
||||
@Test
|
||||
fun `pausing after a reboot banks what was accumulated and invents no segment`(@TempDir tempDir: Path) =
|
||||
runTest {
|
||||
val repository = repository(tempDir)
|
||||
repository.start()
|
||||
clock.reboot(20.seconds)
|
||||
|
||||
repository.pauseAfterReboot()
|
||||
|
||||
val run = repository.run().first()
|
||||
assertThat(run.state).isEqualTo(StopwatchState.PAUSED)
|
||||
assertThat(run.accumulated).isEqualTo(Duration.ZERO)
|
||||
assertThat(run.startedAtElapsedRealtime).isNull()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `pausing after a reboot leaves a paused or idle run untouched`(@TempDir tempDir: Path) = runTest {
|
||||
val repository = repository(tempDir)
|
||||
repository.start()
|
||||
clock.value = 1_030.seconds
|
||||
repository.pause()
|
||||
val paused = repository.run().first()
|
||||
|
||||
repository.pauseAfterReboot()
|
||||
|
||||
assertThat(repository.run().first()).isEqualTo(paused)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an idle run is not written at all by the reboot repair`(@TempDir tempDir: Path) = runTest {
|
||||
val dataStore = PreferenceDataStoreFactory.create(
|
||||
scope = CoroutineScope(UnconfinedTestDispatcher(testScheduler) + Job()),
|
||||
produceFile = { tempDir.resolve("stopwatch_idle_test.preferences_pb").toFile() },
|
||||
)
|
||||
val repository = StopwatchRepositoryImpl(dao, StopwatchStateStore(PrefStore(dataStore)), clock)
|
||||
|
||||
repository.pauseAfterReboot()
|
||||
|
||||
assertThat(dataStore.data.first().asMap()).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the record written by the reboot repair still holds no wall-clock value`(@TempDir tempDir: Path) =
|
||||
runTest {
|
||||
val dataStore = PreferenceDataStoreFactory.create(
|
||||
scope = CoroutineScope(UnconfinedTestDispatcher(testScheduler) + Job()),
|
||||
produceFile = { tempDir.resolve("stopwatch_keys_test.preferences_pb").toFile() },
|
||||
)
|
||||
val repository = StopwatchRepositoryImpl(dao, StopwatchStateStore(PrefStore(dataStore)), clock)
|
||||
repository.start()
|
||||
clock.reboot(20.seconds)
|
||||
|
||||
repository.pauseAfterReboot()
|
||||
|
||||
val snapshot = dataStore.data.first().asMap()
|
||||
assertThat(snapshot.values.filterIsInstance<Long>().filter { it >= 1_000_000_000_000L }).isEmpty()
|
||||
assertThat(snapshot.keys.map { it.name }).containsExactly(
|
||||
"stopwatch_state",
|
||||
"stopwatch_accumulated_millis",
|
||||
"stopwatch_last_lap_cumulative_millis",
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -299,4 +299,92 @@ class TimerRepositoryTest {
|
||||
assertThat(repository.timers().first()).isEqualTo(before)
|
||||
assertThat(repository.find(999L)).isNull()
|
||||
}
|
||||
|
||||
// --- M3: the post-reboot repair ---
|
||||
|
||||
@Test
|
||||
fun `the repair re-anchors a running timer from its wall-clock fallback`() = runTest {
|
||||
val id = startedTimer()
|
||||
elapsed.reboot(50.seconds)
|
||||
wallClock.advance(2.minutes)
|
||||
|
||||
repository.repairAfterReboot()
|
||||
|
||||
val timer = repository.find(id)!!
|
||||
assertThat(timer.state).isEqualTo(TimerState.RUNNING)
|
||||
assertThat(timer.remaining).isEqualTo(3.minutes)
|
||||
assertThat(timer.endsAtElapsedRealtime).isEqualTo(50.seconds + 3.minutes)
|
||||
assertThat(timer.startedAtElapsedRealtime).isEqualTo(50.seconds)
|
||||
assertThat(timer.endsAtWallClock).isEqualTo(T0 + 5.minutes)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a running timer whose wall-clock end has passed is expired by the repair`() = runTest {
|
||||
val id = startedTimer()
|
||||
elapsed.reboot(50.seconds)
|
||||
wallClock.advance(10.minutes)
|
||||
|
||||
repository.repairAfterReboot()
|
||||
|
||||
val timer = repository.find(id)!!
|
||||
assertThat(timer.state).isEqualTo(TimerState.EXPIRED)
|
||||
assertThat(timer.remaining).isEqualTo(Duration.ZERO)
|
||||
assertThat(
|
||||
listOf(timer.startedAtElapsedRealtime, timer.endsAtElapsedRealtime, timer.endsAtWallClock),
|
||||
).containsExactly(null, null, null)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the repair leaves every timer that was not running exactly as it was`() = runTest {
|
||||
val paused = repository.create(TimerDraft(5.minutes, "paused"))
|
||||
repository.start(paused)
|
||||
elapsed.value = 1_100.seconds
|
||||
repository.pause(paused)
|
||||
repository.create(TimerDraft(3.minutes, "idle"))
|
||||
val expired = repository.create(TimerDraft(1.minutes, "expired"))
|
||||
repository.markExpired(expired)
|
||||
elapsed.reboot(50.seconds)
|
||||
wallClock.advance(2.minutes)
|
||||
val before = dao.stored
|
||||
|
||||
repository.repairAfterReboot()
|
||||
|
||||
assertThat(dao.stored).isEqualTo(before)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a running timer with no wall-clock fallback is paused at what it banked`() = runTest {
|
||||
val id = startedTimer()
|
||||
dao.update(dao.findById(id)!!.copy(endsAtWallClockMillis = null))
|
||||
elapsed.reboot(50.seconds)
|
||||
|
||||
repository.repairAfterReboot()
|
||||
|
||||
val timer = repository.find(id)!!
|
||||
assertThat(timer.state).isEqualTo(TimerState.PAUSED)
|
||||
assertThat(timer.remaining).isAtLeast(Duration.ZERO)
|
||||
assertThat(timer.remaining).isEqualTo(5.minutes)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the repair closes the blind spot a reboot opens under a running timer`() = runTest {
|
||||
val id = repository.create(TimerDraft(2.hours, "Roast"))
|
||||
repository.start(id)
|
||||
|
||||
// A new boot whose uptime has already climbed past the stored start anchor,
|
||||
// and half an hour of wall-clock time gone with it.
|
||||
elapsed.reboot(5_000.seconds)
|
||||
wallClock.advance(30.minutes)
|
||||
|
||||
val beforeRepair = repository.find(id)!!.snapshotAt(elapsed.elapsedRealtime(), wallClock.now())
|
||||
assertThat(beforeRepair.isRunning).isTrue()
|
||||
assertThat(beforeRepair.anchorIsStale).isFalse()
|
||||
assertThat(beforeRepair.remaining).isNotEqualTo(90.minutes)
|
||||
|
||||
repository.repairAfterReboot()
|
||||
|
||||
val repaired = repository.find(id)!!.snapshotAt(elapsed.elapsedRealtime(), wallClock.now())
|
||||
assertThat(repaired.remaining - 90.minutes).isLessThan(1.seconds)
|
||||
assertThat(90.minutes - repaired.remaining).isLessThan(1.seconds)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
package de.jeanlucmakiola.clockula.system
|
||||
|
||||
import androidx.datastore.preferences.core.PreferenceDataStoreFactory
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import de.jeanlucmakiola.clockula.data.prefs.BootStateStore
|
||||
import de.jeanlucmakiola.clockula.data.prefs.SystemPrefs
|
||||
import de.jeanlucmakiola.clockula.domain.time.BootId
|
||||
import de.jeanlucmakiola.clockula.testing.FakeBootIdProvider
|
||||
import de.jeanlucmakiola.clockula.testing.RecordingStopwatchRepository
|
||||
import de.jeanlucmakiola.clockula.testing.RecordingTimerRepository
|
||||
import de.jeanlucmakiola.clockula.testing.T0
|
||||
import de.jeanlucmakiola.floret.prefs.PrefStore
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.test.TestScope
|
||||
import kotlinx.coroutines.test.UnconfinedTestDispatcher
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.io.TempDir
|
||||
import java.io.File
|
||||
import java.nio.file.Path
|
||||
import kotlin.time.Duration.Companion.hours
|
||||
|
||||
/**
|
||||
* The gate that runs the reboot repair exactly once per boot, over a real
|
||||
* DataStore file so "survives process death" is a real assertion.
|
||||
*/
|
||||
class RebootRepairTest {
|
||||
|
||||
private val bootIds = FakeBootIdProvider(BootId(bootCount = 7, approximateBootInstant = T0))
|
||||
private val timers = RecordingTimerRepository()
|
||||
private val stopwatch = RecordingStopwatchRepository()
|
||||
|
||||
private fun TestScope.bootStateStore(file: File, job: Job = Job()) =
|
||||
BootStateStore(PrefStore(prefStore(file, job)))
|
||||
|
||||
private fun TestScope.prefStore(file: File, job: Job) = PreferenceDataStoreFactory.create(
|
||||
scope = CoroutineScope(UnconfinedTestDispatcher(testScheduler) + job),
|
||||
produceFile = { file },
|
||||
)
|
||||
|
||||
private fun repair(bootState: BootStateStore) = RebootRepair(bootIds, bootState, timers, stopwatch)
|
||||
|
||||
private fun Path.prefsFile(): File = resolve("clockula_prefs_test.preferences_pb").toFile()
|
||||
|
||||
@Test
|
||||
fun `the very first run ever counts as a new boot and repairs`(@TempDir dir: Path) = runTest {
|
||||
val bootState = bootStateStore(dir.prefsFile())
|
||||
|
||||
val repaired = repair(bootState).repairIfRebooted()
|
||||
|
||||
assertThat(repaired).isTrue()
|
||||
assertThat(timers.repairCalls).isEqualTo(1)
|
||||
assertThat(stopwatch.pauseCalls).isEqualTo(1)
|
||||
assertThat(bootState.lastBootId()).isEqualTo(bootIds.bootId)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the same boot repairs nothing`(@TempDir dir: Path) = runTest {
|
||||
val bootState = bootStateStore(dir.prefsFile())
|
||||
bootState.setLastBootId(bootIds.bootId)
|
||||
|
||||
val repaired = repair(bootState).repairIfRebooted()
|
||||
|
||||
assertThat(repaired).isFalse()
|
||||
assertThat(timers.repairCalls).isEqualTo(0)
|
||||
assertThat(stopwatch.pauseCalls).isEqualTo(0)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a different boot repairs and stores the new id`(@TempDir dir: Path) = runTest {
|
||||
val bootState = bootStateStore(dir.prefsFile())
|
||||
bootState.setLastBootId(BootId(bootCount = 6, approximateBootInstant = T0 - 5.hours))
|
||||
|
||||
val repaired = repair(bootState).repairIfRebooted()
|
||||
|
||||
assertThat(repaired).isTrue()
|
||||
assertThat(timers.repairCalls).isEqualTo(1)
|
||||
assertThat(stopwatch.pauseCalls).isEqualTo(1)
|
||||
assertThat(bootState.lastBootId()).isEqualTo(bootIds.bootId)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `calling twice in one boot repairs once`(@TempDir dir: Path) = runTest {
|
||||
val repair = repair(bootStateStore(dir.prefsFile()))
|
||||
|
||||
val first = repair.repairIfRebooted()
|
||||
val second = repair.repairIfRebooted()
|
||||
|
||||
assertThat(listOf(first, second)).containsExactly(true, false).inOrder()
|
||||
assertThat(timers.repairCalls).isEqualTo(1)
|
||||
assertThat(stopwatch.pauseCalls).isEqualTo(1)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a malformed stored id reads as no known boot and is replaced`(@TempDir dir: Path) = runTest {
|
||||
val file = dir.prefsFile()
|
||||
val store = PrefStore(prefStore(file, Job()))
|
||||
store.set(SystemPrefs.lastBootId, "not-a-boot-id")
|
||||
val bootState = BootStateStore(store)
|
||||
|
||||
val repaired = repair(bootState).repairIfRebooted()
|
||||
|
||||
assertThat(repaired).isTrue()
|
||||
assertThat(bootState.lastBootId()).isEqualTo(bootIds.bootId)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the gate survives process death, so the repair does not run twice per boot`(@TempDir dir: Path) =
|
||||
runTest {
|
||||
val file = dir.prefsFile()
|
||||
val firstProcess = Job()
|
||||
repair(bootStateStore(file, firstProcess)).repairIfRebooted()
|
||||
firstProcess.cancel()
|
||||
|
||||
val afterRestart = bootStateStore(file, Job())
|
||||
|
||||
assertThat(afterRestart.lastBootId()).isEqualTo(bootIds.bootId)
|
||||
assertThat(repair(afterRestart).repairIfRebooted()).isFalse()
|
||||
assertThat(timers.repairCalls).isEqualTo(1)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user