fix(infrared): add cutoff timestamp to failInterruptedLearning to prevent race with subsequent tasks

This commit is contained in:
2026-08-25 13:07:38 +08:00
parent a3812aae27
commit 2185e73f5e
9 changed files with 89 additions and 12 deletions
@@ -100,11 +100,12 @@ class FakeInfraredKeyRepository : InfraredKeyRepository {
override suspend fun failInterruptedLearning(
profileId: ProviderProfileId,
cutoffEpochMillis: Long,
updatedAtEpochMillis: Long,
): DomainResult<Int> = synchronized(lock) {
var changed = 0
keys.replaceAll { _, key ->
if (key.profileId == profileId && key.status == InfraredKeyStatus.LEARNING) {
if (key.profileId == profileId && key.status == InfraredKeyStatus.LEARNING && key.createdAtEpochMillis <= cutoffEpochMillis) {
changed += 1
key.copy(
status = InfraredKeyStatus.FAILED,
@@ -207,6 +207,41 @@ class FakeInfraredKeyRepositoryTest {
)
}
@Test
fun failInterruptedLearningRespectsCutoffEpochMillis() = runTest {
val repository = FakeInfraredKeyRepository()
repository.insert(
savedKey(
id = "old-learning",
sdkKey = "old-learning",
status = InfraredKeyStatus.LEARNING,
).copy(createdAtEpochMillis = 100L),
)
repository.insert(
savedKey(
id = "new-learning",
sdkKey = "new-learning",
status = InfraredKeyStatus.LEARNING,
).copy(createdAtEpochMillis = 200L),
)
val result = repository.failInterruptedLearning(
profileId = profileOne,
cutoffEpochMillis = 150L,
updatedAtEpochMillis = 150L,
)
assertEquals(DomainResult.Success(1), result)
assertEquals(
InfraredKeyStatus.FAILED,
repository.successfulKey(InfraredKeyRef(profileOne, InfraredKeyId("old-learning")))?.status,
)
assertEquals(
InfraredKeyStatus.LEARNING,
repository.successfulKey(InfraredKeyRef(profileOne, InfraredKeyId("new-learning")))?.status,
)
}
@Test
fun deviceConfigurationIsIsolatedByProfile() = runTest {
val repository = FakeInfraredDeviceConfigurationRepository()