|
| 1 | +package io.sentry.android.core |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import androidx.lifecycle.Lifecycle.Event.ON_START |
| 5 | +import androidx.lifecycle.Lifecycle.Event.ON_STOP |
| 6 | +import androidx.lifecycle.LifecycleRegistry |
| 7 | +import androidx.test.core.app.ApplicationProvider |
| 8 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 9 | +import io.sentry.Hint |
| 10 | +import io.sentry.ISentryClient |
| 11 | +import io.sentry.ProfilingTraceData |
| 12 | +import io.sentry.Scope |
| 13 | +import io.sentry.Sentry |
| 14 | +import io.sentry.SentryEnvelope |
| 15 | +import io.sentry.SentryEvent |
| 16 | +import io.sentry.SentryOptions |
| 17 | +import io.sentry.Session |
| 18 | +import io.sentry.Session.State.Exited |
| 19 | +import io.sentry.Session.State.Ok |
| 20 | +import io.sentry.TraceContext |
| 21 | +import io.sentry.UserFeedback |
| 22 | +import io.sentry.protocol.SentryId |
| 23 | +import io.sentry.protocol.SentryTransaction |
| 24 | +import org.junit.runner.RunWith |
| 25 | +import org.mockito.kotlin.mock |
| 26 | +import org.robolectric.annotation.Config |
| 27 | +import java.util.LinkedList |
| 28 | +import kotlin.test.BeforeTest |
| 29 | +import kotlin.test.Test |
| 30 | +import kotlin.test.assertTrue |
| 31 | + |
| 32 | +@RunWith(AndroidJUnit4::class) |
| 33 | +@Config(sdk = [31]) |
| 34 | +class SessionTrackingIntegrationTest { |
| 35 | + |
| 36 | + private lateinit var context: Context |
| 37 | + |
| 38 | + @BeforeTest |
| 39 | + fun `set up`() { |
| 40 | + context = ApplicationProvider.getApplicationContext() |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + fun `session tracking works properly with multiple backgrounds and foregrounds`() { |
| 45 | + lateinit var options: SentryAndroidOptions |
| 46 | + SentryAndroid.init(context) { |
| 47 | + it.dsn = "https://[email protected]/proj" |
| 48 | + |
| 49 | + it.environment = "production" |
| 50 | + it.sessionTrackingIntervalMillis = 0L |
| 51 | + options = it |
| 52 | + } |
| 53 | + val client = CapturingSentryClient() |
| 54 | + Sentry.bindClient(client) |
| 55 | + val lifecycle = setupLifecycle(options) |
| 56 | + val initSid = lastSessionId() |
| 57 | + |
| 58 | + lifecycle.handleLifecycleEvent(ON_START) |
| 59 | + val sidAfterFirstStart = lastSessionId() |
| 60 | + Thread.sleep(100L) |
| 61 | + lifecycle.handleLifecycleEvent(ON_STOP) |
| 62 | + Thread.sleep(100L) |
| 63 | + val sidAfterFirstStop = lastSessionId() |
| 64 | + |
| 65 | + Thread.sleep(100L) |
| 66 | + |
| 67 | + lifecycle.handleLifecycleEvent(ON_START) |
| 68 | + val sidAfterSecondStart = lastSessionId() |
| 69 | + Thread.sleep(100L) |
| 70 | + lifecycle.handleLifecycleEvent(ON_STOP) |
| 71 | + Thread.sleep(100L) |
| 72 | + val sidAfterSecondStop = lastSessionId() |
| 73 | + // we bind our CapturingSentryClient only after .init is called, so we'll be able to capture |
| 74 | + // only the Exited status of the session started in .init |
| 75 | + val initSessionUpdate = client.sessionUpdates.pop() |
| 76 | + assertTrue { |
| 77 | + initSid == initSessionUpdate.sessionId.toString() && |
| 78 | + Session.State.Exited == initSessionUpdate.status |
| 79 | + } |
| 80 | + |
| 81 | + val afterFirstStartSessionUpdate = client.sessionUpdates.pop() |
| 82 | + assertTrue { |
| 83 | + sidAfterFirstStart == afterFirstStartSessionUpdate.sessionId.toString() && |
| 84 | + Session.State.Ok == afterFirstStartSessionUpdate.status |
| 85 | + } |
| 86 | + |
| 87 | + val afterFirstStopSessionUpdate = client.sessionUpdates.pop() |
| 88 | + assertTrue { |
| 89 | + "null" == sidAfterFirstStop && |
| 90 | + sidAfterFirstStart == afterFirstStopSessionUpdate.sessionId.toString() && |
| 91 | + Session.State.Exited == afterFirstStopSessionUpdate.status |
| 92 | + } |
| 93 | + |
| 94 | + val afterSecondStartSessionUpdate = client.sessionUpdates.pop() |
| 95 | + assertTrue { |
| 96 | + sidAfterSecondStart == afterSecondStartSessionUpdate.sessionId.toString() && |
| 97 | + Session.State.Ok == afterSecondStartSessionUpdate.status |
| 98 | + } |
| 99 | + |
| 100 | + val afterSecondStopSessionUpdate = client.sessionUpdates.pop() |
| 101 | + assertTrue { |
| 102 | + "null" == sidAfterSecondStop && |
| 103 | + sidAfterSecondStart == afterSecondStopSessionUpdate.sessionId.toString() && |
| 104 | + Session.State.Exited == afterSecondStopSessionUpdate.status |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private fun lastSessionId(): String? { |
| 109 | + var sid: String? = null |
| 110 | + Sentry.configureScope { scope -> |
| 111 | + sid = scope.session?.sessionId.toString() |
| 112 | + } |
| 113 | + return sid |
| 114 | + } |
| 115 | + |
| 116 | + private fun setupLifecycle(options: SentryOptions): LifecycleRegistry { |
| 117 | + val lifecycle = LifecycleRegistry(mock()) |
| 118 | + val lifecycleWatcher = ( |
| 119 | + options.integrations.find { |
| 120 | + it is AppLifecycleIntegration |
| 121 | + } as AppLifecycleIntegration |
| 122 | + ).watcher |
| 123 | + lifecycle.addObserver(lifecycleWatcher!!) |
| 124 | + return lifecycle |
| 125 | + } |
| 126 | + |
| 127 | + private class CapturingSentryClient : ISentryClient { |
| 128 | + val sessionUpdates = LinkedList<Session>() |
| 129 | + |
| 130 | + override fun isEnabled(): Boolean = true |
| 131 | + |
| 132 | + override fun captureEvent(event: SentryEvent, scope: Scope?, hint: Hint?): SentryId { |
| 133 | + TODO("Not yet implemented") |
| 134 | + } |
| 135 | + |
| 136 | + override fun close() { |
| 137 | + TODO("Not yet implemented") |
| 138 | + } |
| 139 | + |
| 140 | + override fun flush(timeoutMillis: Long) { |
| 141 | + TODO("Not yet implemented") |
| 142 | + } |
| 143 | + |
| 144 | + override fun captureUserFeedback(userFeedback: UserFeedback) { |
| 145 | + TODO("Not yet implemented") |
| 146 | + } |
| 147 | + |
| 148 | + override fun captureSession(session: Session, hint: Hint?) { |
| 149 | + sessionUpdates.add(session) |
| 150 | + } |
| 151 | + |
| 152 | + override fun captureEnvelope(envelope: SentryEnvelope, hint: Hint?): SentryId? { |
| 153 | + TODO("Not yet implemented") |
| 154 | + } |
| 155 | + |
| 156 | + override fun captureTransaction( |
| 157 | + transaction: SentryTransaction, |
| 158 | + traceContext: TraceContext?, |
| 159 | + scope: Scope?, |
| 160 | + hint: Hint?, |
| 161 | + profilingTraceData: ProfilingTraceData? |
| 162 | + ): SentryId { |
| 163 | + TODO("Not yet implemented") |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments