Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flaky RateLimiter test #4100

Merged
merged 3 commits into from
Jan 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"))
}
}
Loading