|
| 1 | +package com.bugsnag.android |
| 2 | + |
| 3 | +import android.os.Handler |
| 4 | +import android.os.HandlerThread |
| 5 | +import org.junit.After |
| 6 | +import org.junit.Before |
| 7 | +import org.junit.Test |
| 8 | +import org.mockito.ArgumentMatchers.any |
| 9 | +import org.mockito.Mockito |
| 10 | +import org.mockito.Mockito.times |
| 11 | +import org.mockito.Mockito.verify |
| 12 | +import org.mockito.Mockito.verifyNoInteractions |
| 13 | +import java.util.concurrent.CountDownLatch |
| 14 | +import java.lang.Thread as JThread |
| 15 | + |
| 16 | +private const val APP_HANG_THRESHOLD = 100L |
| 17 | + |
| 18 | +class BugsnagAppHangPluginTest { |
| 19 | + private lateinit var handlerThread: HandlerThread |
| 20 | + private lateinit var plugin: BugsnagAppHangPlugin |
| 21 | + private lateinit var client: Client |
| 22 | + private lateinit var handler: Handler |
| 23 | + |
| 24 | + @Before |
| 25 | + fun setup() { |
| 26 | + handlerThread = HandlerThread("Test Thread") |
| 27 | + handlerThread.start() |
| 28 | + handler = Handler(handlerThread.looper) |
| 29 | + |
| 30 | + plugin = BugsnagAppHangPlugin( |
| 31 | + AppHangConfiguration( |
| 32 | + appHangThresholdMillis = APP_HANG_THRESHOLD, |
| 33 | + watchedLooper = handlerThread.looper |
| 34 | + ) |
| 35 | + ) |
| 36 | + |
| 37 | + client = Mockito.mock() |
| 38 | + plugin.load(client) |
| 39 | + plugin.startMonitoring() |
| 40 | + } |
| 41 | + |
| 42 | + @After |
| 43 | + fun shutdown() { |
| 44 | + plugin.unload() |
| 45 | + handlerThread.quit() |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + fun testIdleHandlerThread() { |
| 50 | + JThread.sleep(APP_HANG_THRESHOLD * 5) |
| 51 | + verifyNoInteractions(client) |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + fun testBelowThresholdEvents() { |
| 56 | + val countDownLatch = CountDownLatch(10) |
| 57 | + repeat(countDownLatch.count.toInt()) { |
| 58 | + handler.post { |
| 59 | + JThread.sleep(APP_HANG_THRESHOLD / 2) |
| 60 | + countDownLatch.countDown() |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + verifyNoInteractions(client) |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + fun appHang() { |
| 69 | + val countDownLatch = CountDownLatch(1) |
| 70 | + handler.post { |
| 71 | + // wait long enough for 2+ AppHang triggers to happen |
| 72 | + JThread.sleep(APP_HANG_THRESHOLD * 3) |
| 73 | + countDownLatch.countDown() |
| 74 | + } |
| 75 | + |
| 76 | + countDownLatch.await() |
| 77 | + |
| 78 | + // we should have reported exactly 1 AppHang |
| 79 | + verify(client, times(1)) |
| 80 | + .notify(any(AppHangException::class.java), any()) |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + fun appHangRecoverHang() { |
| 85 | + val countDownLatch = CountDownLatch(2) |
| 86 | + |
| 87 | + handler.post { |
| 88 | + // wait long enough for 2+ AppHang triggers to happen |
| 89 | + JThread.sleep(APP_HANG_THRESHOLD * 3) |
| 90 | + countDownLatch.countDown() |
| 91 | + |
| 92 | + handler.postDelayed({ |
| 93 | + JThread.sleep(APP_HANG_THRESHOLD * 3) |
| 94 | + countDownLatch.countDown() |
| 95 | + }, APP_HANG_THRESHOLD) |
| 96 | + } |
| 97 | + |
| 98 | + countDownLatch.await() |
| 99 | + |
| 100 | + // we should have reported exactly 1 AppHangs |
| 101 | + verify(client, times(2)) |
| 102 | + .notify(any(AppHangException::class.java), any()) |
| 103 | + } |
| 104 | +} |
0 commit comments