Prevent backups from being scheduled twice within the jitter window.

Fixes #13559.
This commit is contained in:
Nicholas Tinsley
2024-07-01 17:01:02 -04:00
committed by Cody Henthorne
parent b113eec940
commit aec0a9951a
4 changed files with 43 additions and 9 deletions

View File

@@ -8,8 +8,12 @@ package org.thoughtcrime.securesms.service
import org.junit.Assert
import org.junit.Test
import org.thoughtcrime.securesms.BaseUnitTest
import org.thoughtcrime.securesms.testutil.MockRandom
import java.time.Duration
import java.time.LocalDateTime
import java.util.concurrent.TimeUnit
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.minutes
class BackListenerTest : BaseUnitTest() {
@@ -44,4 +48,14 @@ class BackListenerTest : BaseUnitTest() {
val next = MessageBackupListener.getNextDailyBackupTimeFromNowWithJitter(now, 3, 0, jitterWindowSeconds)
Assert.assertEquals(8, next.dayOfMonth)
}
@Test
fun testBackupJitterWhenScheduledForMidnightButJitterMakesItRunJustBefore() {
val mockRandom = MockRandom(listOf(1.minutes.inWholeSeconds.toInt()))
val jitterWindowSeconds = 10.minutes.inWholeSeconds.toInt()
val now: LocalDateTime = LocalDateTime.of(2024, 6, 27, 23, 57, 0)
val next: LocalDateTime = MessageBackupListener.getNextDailyBackupTimeFromNowWithJitter(now, 0, 0, jitterWindowSeconds, mockRandom)
Assert.assertTrue(Duration.between(now, next).toSeconds() > (1.days.inWholeSeconds - jitterWindowSeconds))
}
}

View File

@@ -0,0 +1,18 @@
/*
* Copyright 2024 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.thoughtcrime.securesms.testutil
import java.util.LinkedList
import java.util.Random
class MockRandom(initialInts: List<Int>) : Random() {
val nextInts = LinkedList(initialInts)
override fun nextInt(): Int {
return nextInts.remove()
}
}