Skip to content

Commit

Permalink
RUM-6375: Add check whether core is still active
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanmos committed Oct 22, 2024
1 parent a7e2344 commit e5194c4
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions dd-sdk-android-core/api/apiSurface
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ interface com.datadog.android.api.SdkCore
val name: String
val time: com.datadog.android.api.context.TimeInfo
val service: String
fun isCoreActive(): Boolean
fun setTrackingConsent(com.datadog.android.privacy.TrackingConsent)
fun setUserInfo(String? = null, String? = null, String? = null, Map<String, Any?> = emptyMap())
fun addUserProperties(Map<String, Any?>)
Expand Down
1 change: 1 addition & 0 deletions dd-sdk-android-core/api/dd-sdk-android-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public abstract interface class com/datadog/android/api/SdkCore {
public abstract fun getName ()Ljava/lang/String;
public abstract fun getService ()Ljava/lang/String;
public abstract fun getTime ()Lcom/datadog/android/api/context/TimeInfo;
public abstract fun isCoreActive ()Z
public abstract fun setTrackingConsent (Lcom/datadog/android/privacy/TrackingConsent;)V
public abstract fun setUserInfo (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;)V
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ interface SdkCore {
*/
val service: String

/**
* Returns true if the core is active.
*/
@AnyThread
fun isCoreActive(): Boolean

/**
* Sets the tracking consent regarding the data collection for this instance of the Datadog SDK.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ internal class DatadogCore(
return coreFeature.createScheduledExecutorService(executorContext)
}

override fun isCoreActive(): Boolean = isActive

// endregion

// region InternalSdkCore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ internal object NoOpInternalSdkCore : InternalSdkCore {

override fun clearAllData() = Unit

override fun isCoreActive(): Boolean = false

// endregion

// region FeatureSdkCore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ object SessionReplay {
}

private fun isAlreadyRegistered() =
currentRegisteredCore?.get() != null
currentRegisteredCore?.get()?.isCoreActive() == true

private fun logAlreadyRegisteredWarning(internalLogger: InternalLogger) =
internalLogger.log(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ internal class SessionReplayTest {
@Mock mockInternalLogger: InternalLogger
) {
// Given
whenever(mockCore1.isCoreActive()).thenReturn(true)
whenever(mockCore1.internalLogger).thenReturn(mockInternalLogger)
whenever(mockCore2.internalLogger).thenReturn(mockInternalLogger)
val fakeSessionReplayConfigurationWithMockRequirement = fakeSessionReplayConfiguration.copy(
Expand Down Expand Up @@ -159,5 +160,42 @@ internal class SessionReplayTest {
targets = listOf(InternalLogger.Target.MAINTAINER, InternalLogger.Target.TELEMETRY),
message = IS_ALREADY_REGISTERED_WARNING
)
assertThat(SessionReplay.currentRegisteredCore?.get()).isEqualTo(mockCore1)
}

@Test
fun `M allow changing cores W enable { Session Replay already enabled but old core inactive }`(
@Forgery fakeSessionReplayConfiguration: SessionReplayConfiguration,
@Mock mockCore1: FeatureSdkCore,
@Mock mockCore2: FeatureSdkCore,
@Mock mockInternalLogger: InternalLogger
) {
// Given
whenever(mockCore1.internalLogger).thenReturn(mockInternalLogger)
whenever(mockCore2.internalLogger).thenReturn(mockInternalLogger)
val fakeSessionReplayConfigurationWithMockRequirement = fakeSessionReplayConfiguration.copy(
systemRequirementsConfiguration = mockSystemRequirementsConfiguration
)
whenever(
mockSystemRequirementsConfiguration.runIfRequirementsMet(any(), any())
) doAnswer {
it.getArgument<() -> Unit>(1).invoke()
}
whenever(mockCore1.isCoreActive()).thenReturn(true)
SessionReplay.enable(
sessionReplayConfiguration = fakeSessionReplayConfigurationWithMockRequirement,
sdkCore = mockCore1
)
assertThat(SessionReplay.currentRegisteredCore?.get()).isEqualTo(mockCore1)

// When
whenever(mockCore1.isCoreActive()).thenReturn(false)
SessionReplay.enable(
sessionReplayConfiguration = fakeSessionReplayConfigurationWithMockRequirement,
sdkCore = mockCore2
)

// Then
assertThat(SessionReplay.currentRegisteredCore?.get()).isEqualTo(mockCore2)
}
}

0 comments on commit e5194c4

Please sign in to comment.