diff --git a/dd-sdk-android-core/src/main/kotlin/com/datadog/android/core/internal/persistence/file/advanced/ConsentAwareFileOrchestrator.kt b/dd-sdk-android-core/src/main/kotlin/com/datadog/android/core/internal/persistence/file/advanced/ConsentAwareFileOrchestrator.kt index eec35a36c6..daf9112d12 100644 --- a/dd-sdk-android-core/src/main/kotlin/com/datadog/android/core/internal/persistence/file/advanced/ConsentAwareFileOrchestrator.kt +++ b/dd-sdk-android-core/src/main/kotlin/com/datadog/android/core/internal/persistence/file/advanced/ConsentAwareFileOrchestrator.kt @@ -27,6 +27,7 @@ internal open class ConsentAwareFileOrchestrator( internal val internalLogger: InternalLogger ) : FileOrchestrator, TrackingConsentProviderCallback { + @Volatile private lateinit var delegateOrchestrator: FileOrchestrator init { diff --git a/reliability/core-it/src/androidTest/kotlin/com/datadog/android/core/integration/tests/FeatureScopeTest.kt b/reliability/core-it/src/androidTest/kotlin/com/datadog/android/core/integration/tests/FeatureScopeTest.kt index c540fdaf64..3c806d8de8 100644 --- a/reliability/core-it/src/androidTest/kotlin/com/datadog/android/core/integration/tests/FeatureScopeTest.kt +++ b/reliability/core-it/src/androidTest/kotlin/com/datadog/android/core/integration/tests/FeatureScopeTest.kt @@ -31,6 +31,8 @@ import org.junit.Before import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit /** * Instrumentation tests for the feature scope. @@ -156,6 +158,50 @@ class FeatureScopeTest : MockServerTest() { }.doWait(LONG_WAIT_MS) } + @Test + fun mustReceiveTheEvents_whenFeatureWrite_trackingConsentPendingToGranted_switched_asynchronously() { + // Given + trackingConsent = TrackingConsent.PENDING + testedInternalSdkCore = Datadog.initialize( + context = ApplicationProvider.getApplicationContext(), + configuration = fakeConfiguration, + trackingConsent = trackingConsent + ) as InternalSdkCore + testedInternalSdkCore.registerFeature(stubFeature) + val featureScope = testedInternalSdkCore.getFeature(fakeFeatureName) + checkNotNull(featureScope) + val countDownLatch = CountDownLatch(1) + Thread { + fakeBatchData.forEach { rawBatchEvent -> + featureScope.withWriteContext { _, eventBatchWriter -> + eventBatchWriter.write( + rawBatchEvent, + fakeBatchMetadata, + eventType + ) + countDownLatch.countDown() + } + } + }.start() + + // When + val switchConsentThread = Thread { + Datadog.setTrackingConsent(TrackingConsent.GRANTED) + } + switchConsentThread.start() + switchConsentThread.join() + + // Then + countDownLatch.await(TimeUnit.SECONDS.toMillis(5), TimeUnit.MILLISECONDS) + ConditionWatcher { + MockWebServerAssert.assertThat(getMockServerWrapper()) + .withConfiguration(fakeConfiguration) + .withTrackingConsent(TrackingConsent.GRANTED) + .receivedData(fakeBatchData, fakeBatchMetadata) + true + }.doWait(LONG_WAIT_MS) + } + @Test fun mustReceiveTheEvents_whenFeatureWrite_trackingConsentGranted_metadataIsNull() { // Given