@@ -6,6 +6,7 @@ import java.util.concurrent.Callable
66import java.util.concurrent.CancellationException
77import java.util.concurrent.LinkedBlockingQueue
88import java.util.concurrent.ScheduledThreadPoolExecutor
9+ import java.util.concurrent.TimeUnit
910import java.util.concurrent.atomic.AtomicBoolean
1011import kotlin.test.Test
1112import kotlin.test.assertEquals
@@ -177,27 +178,6 @@ class SentryExecutorServiceTest {
177178 verify(executor).submit(any<Callable <String >>())
178179 }
179180
180- @Test
181- fun `SentryExecutorService schedule returns cancelled future when queue size exceeds limit` () {
182- val queue = mock<BlockingQueue <Runnable >>()
183- whenever(queue.size).thenReturn(272 ) // Above MAX_QUEUE_SIZE (271)
184-
185- val executor = mock<ScheduledThreadPoolExecutor > { on { getQueue() } doReturn queue }
186-
187- val options = mock<SentryOptions >()
188- val logger = mock<ILogger >()
189- whenever(options.logger).thenReturn(logger)
190-
191- val sentryExecutor = SentryExecutorService (executor, options)
192- val future = sentryExecutor.schedule({}, 1000L )
193-
194- assertTrue(future.isCancelled)
195- assertTrue(future.isDone)
196- assertFailsWith<CancellationException > { future.get() }
197- verify(executor, never()).schedule(any<Runnable >(), any(), any())
198- verify(logger).log(any<SentryLevel >(), any<String >())
199- }
200-
201181 @Test
202182 fun `SentryExecutorService schedule accepts when queue size is within limit` () {
203183 val queue = mock<BlockingQueue <Runnable >>()
@@ -225,4 +205,28 @@ class SentryExecutorServiceTest {
225205 // the queue should be empty
226206 assertEquals(0 , executor.queue.size)
227207 }
208+
209+ @Test
210+ fun `SentryExecutorService schedules any number of job` () {
211+ val executor = ScheduledThreadPoolExecutor (1 )
212+ val sentryExecutor = SentryExecutorService (executor, null )
213+ // Post 1k jobs after 1 day, to test they are all accepted
214+ repeat(1000 ) { sentryExecutor.schedule({}, TimeUnit .DAYS .toMillis(1 )) }
215+ assertEquals(1000 , executor.queue.size)
216+ }
217+
218+ @Test
219+ fun `SentryExecutorService purges cancelled jobs when limit is reached` () {
220+ val executor = ScheduledThreadPoolExecutor (1 )
221+ val sentryExecutor = SentryExecutorService (executor, null )
222+ // Post 1k jobs after 1 day, to test they are all accepted
223+ repeat(1000 ) {
224+ val future = sentryExecutor.schedule({}, TimeUnit .DAYS .toMillis(1 ))
225+ future.cancel(true )
226+ }
227+ assertEquals(1000 , executor.queue.size)
228+ // Submit should purge cancelled scheduled jobs
229+ sentryExecutor.submit {}
230+ assertEquals(1 , executor.queue.size)
231+ }
228232}
0 commit comments