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

Unregister process lifecycle monitor in core instance stop #1589

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
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ internal class DatadogCore(
)
}

private var processLifecycleMonitor: ProcessLifecycleMonitor? = null

// region SdkCore

/** @inheritDoc */
Expand Down Expand Up @@ -358,12 +360,15 @@ internal class DatadogCore(

private fun setupLifecycleMonitorCallback(appContext: Context) {
if (appContext is Application) {
val callback = ProcessLifecycleCallback(
coreFeature.networkInfoProvider,
appContext,
internalLogger
)
appContext.registerActivityLifecycleCallbacks(ProcessLifecycleMonitor(callback))
processLifecycleMonitor = ProcessLifecycleMonitor(
ProcessLifecycleCallback(
coreFeature.networkInfoProvider,
appContext,
internalLogger
)
).apply {
appContext.registerActivityLifecycleCallbacks(this)
}
}
}

Expand Down Expand Up @@ -444,6 +449,10 @@ internal class DatadogCore(
}
features.clear()

if (context is Application && processLifecycleMonitor != null) {
context.unregisterActivityLifecycleCallbacks(processLifecycleMonitor)
}

coreFeature.stop()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.datadog.android.core.configuration.Configuration
import com.datadog.android.core.internal.ContextProvider
import com.datadog.android.core.internal.CoreFeature
import com.datadog.android.core.internal.SdkFeature
import com.datadog.android.core.internal.lifecycle.ProcessLifecycleMonitor
import com.datadog.android.core.internal.net.DefaultFirstPartyHostHeaderTypeResolver
import com.datadog.android.core.internal.net.info.NetworkInfoProvider
import com.datadog.android.core.internal.privacy.ConsentProvider
Expand Down Expand Up @@ -55,10 +56,12 @@ import org.mockito.Mock
import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.junit.jupiter.MockitoSettings
import org.mockito.kotlin.any
import org.mockito.kotlin.argumentCaptor
import org.mockito.kotlin.doAnswer
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.reset
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.mockito.kotlin.verifyNoInteractions
import org.mockito.kotlin.whenever
Expand Down Expand Up @@ -648,6 +651,22 @@ internal class DatadogCoreTest {
assertThat(testedCore.features).isEmpty()
}

@Test
fun `𝕄 unregister process lifecycle monitor 𝕎 stop()`() {
// Given
val expectedInvocations = if (fakeConfiguration.crashReportsEnabled) 2 else 1

// When
testedCore.stop()

// Then
argumentCaptor<ProcessLifecycleMonitor> {
verify(appContext.mockInstance, times(expectedInvocations))
.unregisterActivityLifecycleCallbacks(capture())
assertThat(lastValue).isInstanceOf(ProcessLifecycleMonitor::class.java)
}
}

companion object {
val appContext = ApplicationContextTestConfiguration(Application::class.java)

Expand Down