|
| 1 | +package io.sentry |
| 2 | + |
| 3 | +import com.google.common.truth.Truth.assertThat |
| 4 | +import io.sentry.clientreport.ClientReportTestHelper.Companion.assertClientReport |
| 5 | +import io.sentry.clientreport.DiscardReason |
| 6 | +import io.sentry.clientreport.DiscardedEvent |
| 7 | +import io.sentry.protocol.SentryId |
| 8 | +import io.sentry.protocol.SentryTransaction |
| 9 | +import kotlin.test.Test |
| 10 | +import org.junit.runner.RunWith |
| 11 | +import org.junit.runners.Parameterized |
| 12 | +import org.mockito.kotlin.any |
| 13 | +import org.mockito.kotlin.anyOrNull |
| 14 | +import org.mockito.kotlin.mock |
| 15 | +import org.mockito.kotlin.never |
| 16 | +import org.mockito.kotlin.verify |
| 17 | +import org.mockito.kotlin.verifyNoMoreInteractions |
| 18 | +import org.mockito.kotlin.whenever |
| 19 | + |
| 20 | +@RunWith(Parameterized::class) |
| 21 | +class SentryClientTransactionProfileTest( |
| 22 | + private val callback: String, |
| 23 | + private val hasProfile: Boolean, |
| 24 | +) { |
| 25 | + companion object { |
| 26 | + @JvmStatic |
| 27 | + @Parameterized.Parameters(name = "callback={0}, hasProfile={1}") |
| 28 | + fun data(): List<Array<Any>> = |
| 29 | + listOf("scope processor", "options processor", "beforeSendTransaction").flatMap { callback -> |
| 30 | + listOf(false, true).map { hasProfile -> arrayOf<Any>(callback, hasProfile) } |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + fun `transaction callback failure reports attached profile exactly once`() { |
| 36 | + val fixture = SentryClientTest.Fixture() |
| 37 | + val options = fixture.sentryOptions |
| 38 | + options.eventProcessors.clear() |
| 39 | + val scope = Scope(options) |
| 40 | + val onDiscard = mock<SentryOptions.OnDiscardCallback>() |
| 41 | + options.onDiscard = onDiscard |
| 42 | + val failure = IllegalStateException("callback failed") |
| 43 | + if (callback == "beforeSendTransaction") { |
| 44 | + options.setBeforeSendTransaction { _, _ -> throw failure } |
| 45 | + } else { |
| 46 | + val processor = mock<EventProcessor>() |
| 47 | + whenever(processor.process(any<SentryTransaction>(), any())).thenThrow(failure) |
| 48 | + if (callback == "scope processor") { |
| 49 | + scope.addEventProcessor(processor) |
| 50 | + } else { |
| 51 | + options.addEventProcessor(processor) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + val id = |
| 56 | + fixture |
| 57 | + .getSut() |
| 58 | + .captureTransaction( |
| 59 | + SentryTransaction(fixture.sentryTracer), |
| 60 | + fixture.sentryTracer.traceContext(), |
| 61 | + scope, |
| 62 | + null, |
| 63 | + if (hasProfile) fixture.profilingTraceData else null, |
| 64 | + ) |
| 65 | + |
| 66 | + assertThat(id).isEqualTo(SentryId.EMPTY_ID) |
| 67 | + verify(fixture.transport, never()).send(any(), anyOrNull()) |
| 68 | + val expected = |
| 69 | + mutableListOf( |
| 70 | + DiscardedEvent(DiscardReason.CALLBACK_ERROR.reason, DataCategory.Transaction.category, 1), |
| 71 | + DiscardedEvent(DiscardReason.CALLBACK_ERROR.reason, DataCategory.Span.category, 2), |
| 72 | + ) |
| 73 | + if (hasProfile) { |
| 74 | + expected.add( |
| 75 | + DiscardedEvent(DiscardReason.CALLBACK_ERROR.reason, DataCategory.Profile.category, 1) |
| 76 | + ) |
| 77 | + verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Profile, 1) |
| 78 | + } |
| 79 | + assertClientReport(options.clientReportRecorder, expected) |
| 80 | + verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Transaction, 1) |
| 81 | + verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Span, 2) |
| 82 | + verifyNoMoreInteractions(onDiscard) |
| 83 | + } |
| 84 | +} |
0 commit comments