@@ -4,27 +4,20 @@ import android.app.ActivityManager
44import android.app.ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING
55import android.app.ActivityManager.ProcessErrorStateInfo.NO_ERROR
66import android.content.Context
7- import com.google.common.truth.Truth.assertThat
87import io.sentry.time.TestMonotonicTicker
98import java.util.concurrent.CountDownLatch
10- import java.util.concurrent.ExecutionException
119import java.util.concurrent.Executors
1210import java.util.concurrent.TimeUnit
1311import java.util.concurrent.TimeUnit.MILLISECONDS
1412import kotlin.test.Test
1513import kotlin.test.assertEquals
16- import kotlin.test.assertFailsWith
17- import kotlin.test.assertFalse
1814import kotlin.test.assertNotNull
1915import kotlin.test.assertNull
2016import kotlin.test.assertTrue
2117import org.junit.Before
2218import org.mockito.kotlin.any
23- import org.mockito.kotlin.atLeastOnce
2419import org.mockito.kotlin.eq
2520import org.mockito.kotlin.mock
26- import org.mockito.kotlin.mockingDetails
27- import org.mockito.kotlin.verify
2821import org.mockito.kotlin.whenever
2922
3023class ANRWatchDogTest {
@@ -236,82 +229,6 @@ class ANRWatchDogTest {
236229 }
237230 }
238231
239- @Test
240- fun `when getProcessesInErrorState runs out of memory, keeps watching without reporting` () {
241- var anr: ApplicationNotResponding ? = null
242- val am = mock<ActivityManager >()
243- whenever(am.processesInErrorState).thenThrow(OutOfMemoryError (" large reply" ))
244- val posted = CountDownLatch (1 )
245- val sut = watchDog(am, posted) { a -> anr = a }
246- val es = Executors .newSingleThreadExecutor()
247- try {
248- val running = es.submit { sut.run () }
249- assertTrue(posted.await(10L , TimeUnit .SECONDS ))
250-
251- var waitCount = 0
252- do {
253- ticker.advance(100 , MILLISECONDS )
254- Thread .sleep(100L )
255- } while (mockingDetails(am).invocations.isEmpty() && waitCount++ < 100 )
256-
257- verify(am, atLeastOnce()).processesInErrorState
258- assertNull(anr)
259- assertFalse(running.isDone)
260- } finally {
261- sut.interrupt()
262- es.shutdown()
263- }
264- }
265-
266- @Test
267- fun `when getProcessesInErrorState throws a fatal error, the watchdog stops` () {
268- val am = mock<ActivityManager >()
269- whenever(am.processesInErrorState).thenThrow(StackOverflowError ())
270- val posted = CountDownLatch (1 )
271- val sut = watchDog(am, posted) {}
272- val es = Executors .newSingleThreadExecutor()
273- try {
274- val running = es.submit { sut.run () }
275- assertTrue(posted.await(10L , TimeUnit .SECONDS ))
276-
277- var waitCount = 0
278- do {
279- ticker.advance(100 , MILLISECONDS )
280- Thread .sleep(100L )
281- } while (! running.isDone && waitCount++ < 100 )
282-
283- val error = assertFailsWith<ExecutionException > { running.get() }
284- assertThat(error.cause).isInstanceOf(StackOverflowError ::class .java)
285- } finally {
286- sut.interrupt()
287- es.shutdown()
288- }
289- }
290-
291- private fun watchDog (
292- am : ActivityManager ,
293- posted : CountDownLatch ,
294- listener : ANRWatchDog .ANRListener ,
295- ): ANRWatchDog {
296- val handler = mock<MainLooperHandler >()
297- // the posted runnable never runs, so the main thread looks blocked
298- whenever(handler.post(any())).then { posted.countDown() }
299- whenever(handler.thread).thenReturn(mock<Thread >())
300- val context = mock<Context >()
301- whenever(context.getSystemService(eq(Context .ACTIVITY_SERVICE ))).thenReturn(am)
302- return ANRWatchDog (
303- ticker,
304- 10L ,
305- 1L ,
306- true ,
307- listener,
308- mock(),
309- handler,
310- context,
311- AndroidDebugManager (),
312- )
313- }
314-
315232 @Test
316233 fun `measures the ANR threshold on the ticker, not on wall time` () {
317234 var anr: ApplicationNotResponding ? = null
0 commit comments