Skip to content

Commit

Permalink
Fix: Remove onActivityPreCreated call in favor of onActivityCreated (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto authored Aug 19, 2021
1 parent bcdb061 commit 41654d8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 66 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

* Fix: Remove onActivityPreCreated call in favor of onActivityCreated (#1661)
* Fix: Do not crash if SENSOR_SERVICE throws (#1655)
* Feat: Add support for async methods in Spring MVC (#1652)
* Feat: Add secondary constructor taking IHub to SentryOkHttpInterceptor (#1657)
Expand Down
1 change: 0 additions & 1 deletion sentry-android-core/api/sentry-android-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ public final class io/sentry/android/core/ActivityLifecycleIntegration : android
public fun onActivityDestroyed (Landroid/app/Activity;)V
public fun onActivityPaused (Landroid/app/Activity;)V
public fun onActivityPostResumed (Landroid/app/Activity;)V
public fun onActivityPreCreated (Landroid/app/Activity;Landroid/os/Bundle;)V
public fun onActivityResumed (Landroid/app/Activity;)V
public fun onActivitySaveInstanceState (Landroid/app/Activity;Landroid/os/Bundle;)V
public fun onActivityStarted (Landroid/app/Activity;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,40 +224,14 @@ private void finishTransaction(final @Nullable ITransaction transaction) {
}
}

@Override
public synchronized void onActivityPreCreated(
final @NonNull Activity activity, final @Nullable Bundle savedInstanceState) {

// only executed if API >= 29 otherwise it happens on onActivityCreated
if (isAllActivityCallbacksAvailable) {
// start collecting frame metrics for transaction
activityFramesTracker.addActivity(activity);

setColdStart(savedInstanceState);

// if activity has global fields being init. and
// they are slow, this won't count the whole fields/ctor initialization time, but only
// when onCreate is actually called.
startTracing(activity);
}
}

@Override
public synchronized void onActivityCreated(
final @NonNull Activity activity, final @Nullable Bundle savedInstanceState) {
if (!isAllActivityCallbacksAvailable) {
// start collecting frame metrics for transaction
activityFramesTracker.addActivity(activity);

setColdStart(savedInstanceState);
}

activityFramesTracker.addActivity(activity);
setColdStart(savedInstanceState);
addBreadcrumb(activity, "created");
startTracing(activity);

// fallback call for API < 29 compatibility, otherwise it happens on onActivityPreCreated
if (!isAllActivityCallbacksAvailable) {
startTracing(activity);
}
firstActivityCreated = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ class ActivityLifecycleIntegrationTest {
sut.register(fixture.hub, fixture.options)

val activity = mock<Activity>()
sut.onActivityPreCreated(activity, fixture.bundle)
sut.onActivityCreated(activity, fixture.bundle)

verify(fixture.hub, never()).startTransaction(any(), any(), anyOrNull(), any(), any())
}
Expand All @@ -241,8 +241,8 @@ class ActivityLifecycleIntegrationTest {
sut.register(fixture.hub, fixture.options)

val activity = mock<Activity>()
sut.onActivityPreCreated(activity, fixture.bundle)
sut.onActivityPreCreated(activity, fixture.bundle)
sut.onActivityCreated(activity, fixture.bundle)
sut.onActivityCreated(activity, fixture.bundle)

verify(fixture.hub).startTransaction(any(), any(), anyOrNull(), any(), any())
}
Expand All @@ -256,7 +256,7 @@ class ActivityLifecycleIntegrationTest {
setAppStartTime()

val activity = mock<Activity>()
sut.onActivityPreCreated(activity, fixture.bundle)
sut.onActivityCreated(activity, fixture.bundle)

verify(fixture.hub).startTransaction(any(), check {
assertEquals("ui.load", it)
Expand All @@ -270,7 +270,7 @@ class ActivityLifecycleIntegrationTest {
sut.register(fixture.hub, fixture.options)

val activity = mock<Activity>()
sut.onActivityPreCreated(activity, fixture.bundle)
sut.onActivityCreated(activity, fixture.bundle)

verify(fixture.activityFramesTracker).addActivity(eq(activity))
}
Expand All @@ -284,7 +284,7 @@ class ActivityLifecycleIntegrationTest {
setAppStartTime()

val activity = mock<Activity>()
sut.onActivityPreCreated(activity, fixture.bundle)
sut.onActivityCreated(activity, fixture.bundle)

verify(fixture.hub).startTransaction(check {
assertEquals("Activity", it)
Expand All @@ -307,7 +307,7 @@ class ActivityLifecycleIntegrationTest {
}

val activity = mock<Activity>()
sut.onActivityPreCreated(activity, fixture.bundle)
sut.onActivityCreated(activity, fixture.bundle)
}

@Test
Expand All @@ -328,7 +328,7 @@ class ActivityLifecycleIntegrationTest {
}

val activity = mock<Activity>()
sut.onActivityPreCreated(activity, fixture.bundle)
sut.onActivityCreated(activity, fixture.bundle)
}

@Test
Expand All @@ -338,7 +338,7 @@ class ActivityLifecycleIntegrationTest {
sut.register(fixture.hub, fixture.options)

val activity = mock<Activity>()
sut.onActivityPreCreated(activity, fixture.bundle)
sut.onActivityCreated(activity, fixture.bundle)
sut.onActivityPostResumed(activity)

verify(fixture.hub).captureTransaction(check {
Expand All @@ -353,7 +353,7 @@ class ActivityLifecycleIntegrationTest {
sut.register(fixture.hub, fixture.options)

val activity = mock<Activity>()
sut.onActivityPreCreated(activity, fixture.bundle)
sut.onActivityCreated(activity, fixture.bundle)

fixture.transaction.status = SpanStatus.UNKNOWN_ERROR

Expand All @@ -372,7 +372,7 @@ class ActivityLifecycleIntegrationTest {
sut.register(fixture.hub, fixture.options)

val activity = mock<Activity>()
sut.onActivityPreCreated(activity, fixture.bundle)
sut.onActivityCreated(activity, fixture.bundle)
sut.onActivityPostResumed(activity)

verify(fixture.hub, never()).captureTransaction(any())
Expand All @@ -396,7 +396,7 @@ class ActivityLifecycleIntegrationTest {
sut.register(fixture.hub, fixture.options)

val activity = mock<Activity>()
sut.onActivityPreCreated(activity, fixture.bundle)
sut.onActivityCreated(activity, fixture.bundle)
sut.onActivityDestroyed(activity)

verify(fixture.hub).captureTransaction(any())
Expand All @@ -409,7 +409,7 @@ class ActivityLifecycleIntegrationTest {
sut.register(fixture.hub, fixture.options)

val activity = mock<Activity>()
sut.onActivityPreCreated(activity, fixture.bundle)
sut.onActivityCreated(activity, fixture.bundle)

assertFalse(sut.activitiesWithOngoingTransactions.isEmpty())
}
Expand All @@ -421,7 +421,7 @@ class ActivityLifecycleIntegrationTest {
sut.register(fixture.hub, fixture.options)

val activity = mock<Activity>()
sut.onActivityPreCreated(activity, fixture.bundle)
sut.onActivityCreated(activity, fixture.bundle)
sut.onActivityDestroyed(activity)

assertTrue(sut.activitiesWithOngoingTransactions.isEmpty())
Expand All @@ -433,32 +433,20 @@ class ActivityLifecycleIntegrationTest {
fixture.options.tracesSampleRate = 1.0
sut.register(fixture.hub, fixture.options)

sut.onActivityPreCreated(mock(), mock())
sut.onActivityCreated(mock(), mock())

sut.onActivityPreCreated(mock(), fixture.bundle)
sut.onActivityCreated(mock(), fixture.bundle)
verify(fixture.hub).captureTransaction(any())
}

@Test
fun `do not start transaction on created if API 29`() {
val sut = fixture.getSut()
fixture.options.tracesSampleRate = 1.0
sut.register(fixture.hub, fixture.options)

val activity = mock<Activity>()
sut.onActivityCreated(activity, mock())

verify(fixture.hub, never()).startTransaction(any(), any(), anyOrNull(), any(), any())
}

@Test
fun `do not stop transaction on resumed if API 29`() {
val sut = fixture.getSut()
fixture.options.tracesSampleRate = 1.0
sut.register(fixture.hub, fixture.options)

val activity = mock<Activity>()
sut.onActivityPreCreated(activity, mock())
sut.onActivityCreated(activity, mock())
sut.onActivityResumed(activity)

verify(fixture.hub, never()).captureTransaction(any())
Expand Down Expand Up @@ -556,7 +544,7 @@ class ActivityLifecycleIntegrationTest {
setAppStartTime(date)

val activity = mock<Activity>()
sut.onActivityPreCreated(activity, fixture.bundle)
sut.onActivityCreated(activity, fixture.bundle)

// call only once
verify(fixture.hub).startTransaction(any(), any(), eq(date), any(), any())
Expand All @@ -572,7 +560,7 @@ class ActivityLifecycleIntegrationTest {
setAppStartTime(date)

val activity = mock<Activity>()
sut.onActivityPreCreated(activity, fixture.bundle)
sut.onActivityCreated(activity, fixture.bundle)

val span = fixture.transaction.children.first()
assertEquals(span.operation, "app.start.warm")
Expand All @@ -589,7 +577,7 @@ class ActivityLifecycleIntegrationTest {
setAppStartTime(date)

val activity = mock<Activity>()
sut.onActivityPreCreated(activity, null)
sut.onActivityCreated(activity, null)

val span = fixture.transaction.children.first()
assertEquals(span.operation, "app.start.cold")
Expand All @@ -606,7 +594,7 @@ class ActivityLifecycleIntegrationTest {
setAppStartTime(date)

val activity = mock<Activity>()
sut.onActivityPreCreated(activity, fixture.bundle)
sut.onActivityCreated(activity, fixture.bundle)

val span = fixture.transaction.children.first()
assertEquals(span.description, "Warm Start")
Expand All @@ -623,7 +611,7 @@ class ActivityLifecycleIntegrationTest {
setAppStartTime(date)

val activity = mock<Activity>()
sut.onActivityPreCreated(activity, null)
sut.onActivityCreated(activity, null)

val span = fixture.transaction.children.first()
assertEquals(span.description, "Cold Start")
Expand All @@ -640,14 +628,14 @@ class ActivityLifecycleIntegrationTest {
setAppStartTime()

val activity = mock<Activity>()
sut.onActivityPreCreated(activity, fixture.bundle)
sut.onActivityCreated(activity, fixture.bundle)

verify(fixture.hub).startTransaction(any(), any(), eq(date), any(), any())
sut.onActivityCreated(activity, fixture.bundle)
sut.onActivityPostResumed(activity)

val newActivity = mock<Activity>()
sut.onActivityPreCreated(newActivity, fixture.bundle)
sut.onActivityCreated(newActivity, fixture.bundle)

val nullDate: Date? = null
verify(fixture.hub).startTransaction(any(), any(), eq(nullDate), any(), any())
Expand Down

0 comments on commit 41654d8

Please sign in to comment.