DataDog / dd-sdk-android

Datadog SDK for Android (Compatible with Kotlin and Java)
Apache License 2.0
159 stars 61 forks source link

Exception in thread "datadog_shutdown" java.lang.IllegalArgumentException: Receiver not registered: com.datadog.android.core.internal.system.BroadcastReceiverSystemInfoProvider #2306

Closed britolucaspatrick closed 1 month ago

britolucaspatrick commented 2 months ago

Describe the bug

running datadog using unit tests the error is thrown

roboeletric 4.10.2 mockito 3.12.4

even attempts to mock the Datadog class don't work

Reproduction steps

create a unit test and call the initialize function, observe in the logcat

Logcat logs

Exception in thread "datadog_shutdown" java.lang.IllegalArgumentException: Receiver not registered: com.datadog.android.core.internal.system.BroadcastReceiverSystemInfoProvider@6eaf1125

          at org.robolectric.shadows.ShadowInstrumentation.unregisterReceiver(ShadowInstrumentation.java:959)

          at org.robolectric.shadows.ShadowContextImpl.unregisterReceiver(ShadowContextImpl.java:329)

          at android.app.ContextImpl.unregisterReceiver(ContextImpl.java)

          at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:664)

          at com.datadog.android.core.internal.receiver.ThreadSafeReceiver.unregisterReceiver(ThreadSafeReceiver.kt:40)

          at com.datadog.android.core.internal.system.BroadcastReceiverSystemInfoProvider.unregister(BroadcastReceiverSystemInfoProvider.kt:60)

          at com.datadog.android.core.internal.CoreFeature.stop(CoreFeature.kt:238)

          at com.datadog.android.core.internal.DatadogCore.stop$dd_sdk_android_core_release(DatadogCore.kt:539)

          at com.datadog.android.core.internal.DatadogCore.setupShutdownHook$lambda$11(DatadogCore.kt:446)

          at java.base/java.lang.Thread.run(Thread.java:840)

Expected behavior

don't throw any exceptions,

and work with mockk

Affected SDK versions

2.14.0

Latest working SDK version

2.14.0

Did you confirm if the latest SDK version fixes the bug?

Yes

Kotlin / Java version

1.8.0

Gradle / AGP version

7.3.3

Other dependencies versions

No response

Device Information

No response

Other relevant information

No response

xgouchet commented 1 month ago

Hi @britolucaspatrick, thanks for opening this issue.

Could you please give us the exact dependencies and test dependencies you're using for your tests? Are those Unit Tests running in the JVM (which I guess is the case, as you're using Robolectric)? What are you trying to achieve by initialising the Datadog SDK in the tests?

britolucaspatrick commented 1 month ago

Hi @xgouchet , Thank you for your contact.

implementation 'androidx.core:core-ktx:1.7.0' implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'com.google.android.material:material:1.9.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.databinding:viewbinding:7.1.2' implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0' implementation 'com.google.code.gson:gson:2.8.8' implementation "com.airbnb.android:lottie:6.1.0" implementation "io.reactivex.rxjava2:rxjava:2.2.6" implementation "io.reactivex.rxjava2:rxandroid:2.1.1" implementation 'com.android.volley:volley:1.2.1' implementation 'net.danlew:android.joda:2.9.9.4' implementation 'com.google.guava:guava:31.1-jre'

implementation "com.datadoghq:dd-sdk-android-okhttp:2.3.0"
implementation "com.datadoghq:dd-sdk-android-rum:2.3.0"
implementation "com.datadoghq:dd-sdk-android-trace:2.3.0"
implementation "com.datadoghq:dd-sdk-android-logs:2.3.0"

testImplementation 'androidx.test.ext:junit-ktx:1.1.5'
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.mockito:mockito-core:3.12.4'
testImplementation 'org.mockito:mockito-inline:3.12.4'
testImplementation 'com.squareup.okhttp3:mockwebserver:4.9.1'
testImplementation 'org.robolectric:robolectric:4.10.2'
testImplementation "io.mockk:mockk-android:1.13.5"
testImplementation "io.mockk:mockk-agent:1.13.5"
testImplementation 'androidx.test:core-ktx:1.5.0'
testImplementation 'androidx.arch.core:core-testing:2.2.0'
britolucaspatrick commented 1 month ago

I managed to solve the problems with mocks