Skip to content

Commit

Permalink
RUM-6307 Make sure ConsentAwareFileOrchestrator is thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusc83 committed Oct 7, 2024
1 parent 7ebe827 commit 021b878
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ internal open class ConsentAwareFileOrchestrator(
internal val internalLogger: InternalLogger
) : FileOrchestrator, TrackingConsentProviderCallback {

@Volatile
private lateinit var delegateOrchestrator: FileOrchestrator

init {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 021b878

Please sign in to comment.