diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/appconfig/mapping/ExposureDetectionConfigMapper.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/appconfig/mapping/ExposureDetectionConfigMapper.kt index 5110334a3cbb27f5b89cf0671428fc95b3de6b32..900ce5bef4f3179a4b9cc020ab53cdf4316407ae 100644 --- a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/appconfig/mapping/ExposureDetectionConfigMapper.kt +++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/appconfig/mapping/ExposureDetectionConfigMapper.kt @@ -54,7 +54,7 @@ fun ExposureDetectionParametersAndroid?.maxExposureDetectionsPerDay(): Int = fun ExposureDetectionParametersAndroid?.minTimeBetweenExposureDetections(): Duration { val detectionsPerDay = this.maxExposureDetectionsPerDay() return if (detectionsPerDay == 0) { - Duration.standardDays(99) + Duration.standardDays(1) } else { (24 / detectionsPerDay).let { Duration.standardHours(it.toLong()) } } diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/diagnosiskeys/download/DownloadDiagnosisKeysTask.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/diagnosiskeys/download/DownloadDiagnosisKeysTask.kt index 7be1a88e9bbe32a26abafa3bf6784c0c9cf9888b..6a741421493b0958044f56fd38a3d6775b35eaf6 100644 --- a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/diagnosiskeys/download/DownloadDiagnosisKeysTask.kt +++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/diagnosiskeys/download/DownloadDiagnosisKeysTask.kt @@ -140,9 +140,23 @@ class DownloadDiagnosisKeysTask @Inject constructor( trackedDetections: Collection<TrackedExposureDetection> ): Boolean { val lastDetection = trackedDetections.maxByOrNull { it.startedAt } - val nextDetectionAt = lastDetection?.startedAt?.plus(exposureConfig.minTimeBetweenDetections) + if (lastDetection == null) { + Timber.tag(TAG).d("No previous detections exist, don't abort.") + return false + } + + if (lastDetection.startedAt.isAfter(now.plus(Duration.standardHours(1)))) { + Timber.tag(TAG).w("Last detection happened in our future? Don't abort as precaution.") + return false + } + + val nextDetectionAt = lastDetection.startedAt.plus(exposureConfig.minTimeBetweenDetections) + + Duration(now, nextDetectionAt).also { + Timber.tag(TAG).d("Next detection is available in %d min", it.standardMinutes) + } - return (nextDetectionAt != null && now.isBefore(nextDetectionAt)).also { + return (now.isBefore(nextDetectionAt)).also { if (it) Timber.tag(TAG).w("Aborting. Last detection is recent: %s (now=%s)", lastDetection, now) } } diff --git a/Corona-Warn-App/src/test/java/de/rki/coronawarnapp/appconfig/mapping/ExposureDetectionConfigMapperTest.kt b/Corona-Warn-App/src/test/java/de/rki/coronawarnapp/appconfig/mapping/ExposureDetectionConfigMapperTest.kt index f39e4f9dee0a89f0d4d073cca27b76c99cabfe1b..7a6e080ae54e1f5908871d854a9e0045b2432faf 100644 --- a/Corona-Warn-App/src/test/java/de/rki/coronawarnapp/appconfig/mapping/ExposureDetectionConfigMapperTest.kt +++ b/Corona-Warn-App/src/test/java/de/rki/coronawarnapp/appconfig/mapping/ExposureDetectionConfigMapperTest.kt @@ -21,13 +21,13 @@ class ExposureDetectionConfigMapperTest : BaseTest() { } @Test - fun `detection interval 0 defaults to almost infinite delay`() { + fun `detection interval 0 defaults to sane delay`() { val exposureDetectionParameters = ExposureDetectionParametersAndroid.newBuilder() val rawConfig = AppConfigAndroid.ApplicationConfigurationAndroid.newBuilder() .setExposureDetectionParameters(exposureDetectionParameters) .build() createInstance().map(rawConfig).apply { - minTimeBetweenDetections shouldBe Duration.standardDays(99) + minTimeBetweenDetections shouldBe Duration.standardDays(1) maxExposureDetectionsPerUTCDay shouldBe 0 } } diff --git a/Corona-Warn-App/src/test/java/de/rki/coronawarnapp/diagnosiskeys/download/DownloadDiagnosisKeysTaskTest.kt b/Corona-Warn-App/src/test/java/de/rki/coronawarnapp/diagnosiskeys/download/DownloadDiagnosisKeysTaskTest.kt index f85ce9af769bebddcbdd955c45a40b1268c79e07..c22593689bdb3fb649c31a47c0c244abc18a0b52 100644 --- a/Corona-Warn-App/src/test/java/de/rki/coronawarnapp/diagnosiskeys/download/DownloadDiagnosisKeysTaskTest.kt +++ b/Corona-Warn-App/src/test/java/de/rki/coronawarnapp/diagnosiskeys/download/DownloadDiagnosisKeysTaskTest.kt @@ -141,7 +141,7 @@ class DownloadDiagnosisKeysTaskTest : BaseTest() { } @Test - fun `execution is skipped if last detection was recent via`() = runBlockingTest { + fun `execution is skipped if last detection was recent`() = runBlockingTest { // Last detection was at T+2h every { timeStamper.nowUTC } returns Instant.EPOCH.plus(Duration.standardHours(2)) @@ -157,6 +157,18 @@ class DownloadDiagnosisKeysTaskTest : BaseTest() { } } + @Test + fun `execution is NOT skipped if last detection is in our future`() = runBlockingTest { + // Last detection was at T, i.e. our time is now T-1h, so it was in our future. + every { timeStamper.nowUTC } returns Instant.EPOCH.minus(Duration.standardHours(1).plus(1)) + + createInstance().run(DownloadDiagnosisKeysTask.Arguments()) + + coVerify { + enfClient.provideDiagnosisKeys(any(), any()) + } + } + @Test fun `wasLastDetectionPerformedRecently honors paramters from config`() = runBlockingTest { every { timeStamper.nowUTC } returns Instant.EPOCH.plus(Duration.standardHours(4))