Skip to content

Commit

Permalink
Fix flaky RateLimiter test (#4100)
Browse files Browse the repository at this point in the history
* changed RateLimiterTest `close cancels the timer` to use reflection
  • Loading branch information
stefanosiano authored Jan 23, 2025
1 parent 467b52b commit 2872e8e
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions sentry/src/test/java/io/sentry/transport/RateLimiterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import io.sentry.hints.DiskFlushNotification
import io.sentry.protocol.SentryId
import io.sentry.protocol.SentryTransaction
import io.sentry.protocol.User
import io.sentry.test.getProperty
import io.sentry.test.injectForField
import io.sentry.util.HintUtils
import org.awaitility.kotlin.await
import org.mockito.kotlin.eq
Expand All @@ -38,9 +40,8 @@ import org.mockito.kotlin.verify
import org.mockito.kotlin.verifyNoMoreInteractions
import org.mockito.kotlin.whenever
import java.io.File
import java.util.Timer
import java.util.UUID
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.test.Test
import kotlin.test.assertEquals
Expand Down Expand Up @@ -363,19 +364,16 @@ class RateLimiterTest {
@Test
fun `close cancels the timer`() {
val rateLimiter = fixture.getSUT()
whenever(fixture.currentDateProvider.currentTimeMillis).thenReturn(0, 1, 2001)

val applied = CountDownLatch(1)
var activeForReplay = false
rateLimiter.addRateLimitObserver {
applied.countDown()
activeForReplay = rateLimiter.isActiveForCategory(Replay)
}
val timer = mock<Timer>()
rateLimiter.injectForField("timer", timer)

rateLimiter.updateRetryAfterLimits("1:replay:key", null, 1)
// When the rate limiter is closed
rateLimiter.close()

applied.await(2, TimeUnit.SECONDS)
assertTrue(activeForReplay)
// Then the timer is cancelled
verify(timer).cancel()

// And is removed by the rateLimiter
assertNull(rateLimiter.getProperty("timer"))
}
}

0 comments on commit 2872e8e

Please sign in to comment.