Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid session creation when app is launched in background #2543

Merged
merged 5 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

- Ignore Shutdown in progress when closing ShutdownHookIntegration ([#2521](https://github.com/getsentry/sentry-java/pull/2521))
- Fix app start span end-time is wrong if SDK init is deferred ([#2519](https://github.com/getsentry/sentry-java/pull/2519))
- Fix invalid session creation when app is launched in background ([#2543](https://github.com/getsentry/sentry-java/pull/2543))

## 6.13.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ public static synchronized void init(
true);

final @NotNull IHub hub = Sentry.getCurrentHub();
if (hub.getOptions().isEnableAutoSessionTracking()) {
if (hub.getOptions().isEnableAutoSessionTracking()
&& ContextUtils.isForegroundImportance(context)) {
hub.addBreadcrumb(BreadcrumbFactory.forSession("session.start"));
hub.startSession();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import io.sentry.SentryLevel
import io.sentry.SentryLevel.DEBUG
import io.sentry.SentryLevel.FATAL
import io.sentry.SentryOptions
import io.sentry.Session
import io.sentry.android.core.cache.AndroidEnvelopeCache
import io.sentry.android.fragment.FragmentLifecycleIntegration
import io.sentry.android.timber.SentryTimberIntegration
import io.sentry.cache.IEnvelopeCache
import io.sentry.transport.NoOpEnvelopeCache
import io.sentry.util.StringUtils
import org.junit.runner.RunWith
import org.mockito.Mockito
import org.mockito.kotlin.any
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
Expand Down Expand Up @@ -188,12 +190,36 @@ class SentryAndroidTest {
}

@Test
fun `init starts a session if auto session tracking is enabled`() {
fixture.initSut { options ->
options.isEnableAutoSessionTracking = true
fun `init starts a session if auto session tracking is enabled and app is in foreground`() {
initSentryWithForegroundImportance(true) { session: Session? ->
assertNotNull(session)
}
Sentry.getCurrentHub().withScope { scope ->
assertNotNull(scope.session)
}

@Test
fun `init does not start a session if auto session tracking is enabled but the app is in background`() {
initSentryWithForegroundImportance(false) { session: Session? ->
assertNull(session)
}
}

private fun initSentryWithForegroundImportance(inForeground: Boolean, callback: (session: Session?) -> Unit) {
val context = ContextUtilsTest.createMockContext()

Mockito.mockStatic(ContextUtils::class.java).use { mockedContextUtils ->
mockedContextUtils.`when`<Any> { ContextUtils.isForegroundImportance(context) }
.thenReturn(inForeground)
SentryAndroid.init(context) { options ->
options.release = "prod"
options.dsn = "https://key@sentry.io/123"
options.isEnableAutoSessionTracking = true
}

var session: Session? = null
Sentry.getCurrentHub().configureScope { scope ->
session = scope.session
}
callback(session)
}
}

Expand Down