|
| 1 | +package io.sentry.android.core |
| 2 | + |
| 3 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 4 | +import io.sentry.ISentryClient |
| 5 | +import io.sentry.SentryMetricsEvent |
| 6 | +import io.sentry.SentryOptions |
| 7 | +import io.sentry.protocol.SentryId |
| 8 | +import io.sentry.test.ImmediateExecutorService |
| 9 | +import kotlin.test.AfterTest |
| 10 | +import kotlin.test.BeforeTest |
| 11 | +import kotlin.test.Test |
| 12 | +import kotlin.test.assertNotNull |
| 13 | +import kotlin.test.assertTrue |
| 14 | +import org.junit.runner.RunWith |
| 15 | +import org.mockito.kotlin.any |
| 16 | +import org.mockito.kotlin.mock |
| 17 | +import org.mockito.kotlin.verify |
| 18 | +import org.mockito.kotlin.whenever |
| 19 | + |
| 20 | +@RunWith(AndroidJUnit4::class) |
| 21 | +class AndroidMetricsBatchProcessorTest { |
| 22 | + |
| 23 | + private class Fixture { |
| 24 | + val options = SentryAndroidOptions() |
| 25 | + val client: ISentryClient = mock() |
| 26 | + |
| 27 | + fun getSut( |
| 28 | + useImmediateExecutor: Boolean = false, |
| 29 | + config: ((SentryOptions) -> Unit)? = null, |
| 30 | + ): AndroidMetricsBatchProcessor { |
| 31 | + if (useImmediateExecutor) { |
| 32 | + options.executorService = ImmediateExecutorService() |
| 33 | + } |
| 34 | + config?.invoke(options) |
| 35 | + return AndroidMetricsBatchProcessor(options, client) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + private val fixture = Fixture() |
| 40 | + |
| 41 | + @BeforeTest |
| 42 | + fun `set up`() { |
| 43 | + AppState.getInstance().resetInstance() |
| 44 | + } |
| 45 | + |
| 46 | + @AfterTest |
| 47 | + fun `tear down`() { |
| 48 | + AppState.getInstance().resetInstance() |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun `constructor registers as AppState listener`() { |
| 53 | + fixture.getSut() |
| 54 | + assertNotNull(AppState.getInstance().lifecycleObserver) |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + fun `onBackground schedules flush`() { |
| 59 | + val sut = fixture.getSut(useImmediateExecutor = true) |
| 60 | + val metricsEvent = SentryMetricsEvent(SentryId(), 1.0, "test", "counter", 3.0) |
| 61 | + sut.add(metricsEvent) |
| 62 | + |
| 63 | + sut.onBackground() |
| 64 | + |
| 65 | + verify(fixture.client).captureBatchedMetricsEvents(any()) |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + fun `onBackground handles executor exception gracefully`() { |
| 70 | + val sut = |
| 71 | + fixture.getSut { options -> |
| 72 | + val rejectingExecutor = mock<io.sentry.ISentryExecutorService>() |
| 73 | + whenever(rejectingExecutor.submit(any())).thenThrow(RuntimeException("Rejected")) |
| 74 | + options.executorService = rejectingExecutor |
| 75 | + } |
| 76 | + |
| 77 | + // Should not throw |
| 78 | + sut.onBackground() |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + fun `close removes AppState listener`() { |
| 83 | + val sut = fixture.getSut() |
| 84 | + sut.close(false) |
| 85 | + |
| 86 | + assertTrue(AppState.getInstance().lifecycleObserver.listeners.isEmpty()) |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + fun `close with isRestarting true still removes listener`() { |
| 91 | + val sut = fixture.getSut() |
| 92 | + sut.close(true) |
| 93 | + |
| 94 | + assertTrue(AppState.getInstance().lifecycleObserver.listeners.isEmpty()) |
| 95 | + } |
| 96 | +} |
0 commit comments