diff --git a/LICENSE-3rdparty.csv b/LICENSE-3rdparty.csv index 510b769ce6..3ddd89172a 100644 --- a/LICENSE-3rdparty.csv +++ b/LICENSE-3rdparty.csv @@ -63,6 +63,8 @@ import,org.jetbrains,Apache-2.0,Copyright 2010-2019 JetBrains s.r.o. and Kotlin import,org.jetbrains.kotlin,Apache-2.0,Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors import,org.jetbrains.kotlinx,Apache-2.0,Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors import,org.reactivestreams,"CC0",Copyright 2014 Reactive Streams +import(test),androidx.concurrent,Apache-2.0,__ +import(test),androidx.dynamicanimation,Apache-2.0,__ import(test),androidx.exifinterface,Apache-2.0,Copyright 2018 The Android Open Source Project import(test),androidx.test,Apache-2.0,Copyright 2018 The Android Open Source Project import(test),androidx.test.espresso,Apache-2.0,Copyright 2018 The Android Open Source Project diff --git a/dd-sdk-android/src/main/kotlin/com/datadog/android/v2/core/internal/data/upload/DataFlusher.kt b/dd-sdk-android/src/main/kotlin/com/datadog/android/v2/core/internal/data/upload/DataFlusher.kt index c199878e04..46e5483fb2 100644 --- a/dd-sdk-android/src/main/kotlin/com/datadog/android/v2/core/internal/data/upload/DataFlusher.kt +++ b/dd-sdk-android/src/main/kotlin/com/datadog/android/v2/core/internal/data/upload/DataFlusher.kt @@ -33,7 +33,11 @@ internal class DataFlusher( toUploadFiles.forEach { val batch = fileReader.readData(it) val metaFile = fileOrchestrator.getMetadataFile(it) - val meta = if (metaFile != null) metadataFileReader.readData(metaFile) else null + val meta = if (metaFile != null && metaFile.existsSafe()) { + metadataFileReader.readData(metaFile) + } else { + null + } uploader.upload(context, batch, meta) fileMover.delete(it) if (metaFile?.existsSafe() == true) { diff --git a/dd-sdk-android/src/main/kotlin/com/datadog/android/v2/core/internal/storage/FileEventBatchWriter.kt b/dd-sdk-android/src/main/kotlin/com/datadog/android/v2/core/internal/storage/FileEventBatchWriter.kt index be06393e16..5b72362273 100644 --- a/dd-sdk-android/src/main/kotlin/com/datadog/android/v2/core/internal/storage/FileEventBatchWriter.kt +++ b/dd-sdk-android/src/main/kotlin/com/datadog/android/v2/core/internal/storage/FileEventBatchWriter.kt @@ -10,6 +10,7 @@ import androidx.annotation.WorkerThread import com.datadog.android.core.internal.persistence.file.FilePersistenceConfig import com.datadog.android.core.internal.persistence.file.FileReaderWriter import com.datadog.android.core.internal.persistence.file.FileWriter +import com.datadog.android.core.internal.persistence.file.existsSafe import com.datadog.android.v2.api.EventBatchWriter import com.datadog.android.v2.api.InternalLogger import java.io.File @@ -26,7 +27,7 @@ internal class FileEventBatchWriter( @WorkerThread override fun currentMetadata(): ByteArray? { - if (metadataFile == null) return null + if (metadataFile == null || !metadataFile.existsSafe()) return null return metadataReaderWriter.readData(metadataFile) } diff --git a/dd-sdk-android/src/test/kotlin/com/datadog/android/v2/core/internal/data/upload/DataFlusherTest.kt b/dd-sdk-android/src/test/kotlin/com/datadog/android/v2/core/internal/data/upload/DataFlusherTest.kt index 0f680afb9e..195eec5a24 100644 --- a/dd-sdk-android/src/test/kotlin/com/datadog/android/v2/core/internal/data/upload/DataFlusherTest.kt +++ b/dd-sdk-android/src/test/kotlin/com/datadog/android/v2/core/internal/data/upload/DataFlusherTest.kt @@ -82,7 +82,10 @@ internal class DataFlusherTest { ) { // Given val fakeFiles = forge.aList { mock() } - val fakeMetaFiles = forge.aList(fakeFiles.size) { forge.aNullable { mock() } } + val fakeMetaFiles = + forge.aList(fakeFiles.size) { + forge.aNullable { mock().apply { whenever(exists()) doReturn true } } + } val fakeBatches = forge .aList(fakeFiles.size) { forge @@ -154,6 +157,39 @@ internal class DataFlusherTest { } } + @Test + fun `M not attempt to read metadata W flush { meta doesn't exist }`( + forge: Forge + ) { + // Given + val fakeFiles = forge.aList { mock() } + val fakeBatches = forge + .aList(fakeFiles.size) { + forge + .aList { + forge.aString() + } + .map { it.toByteArray() } + } + whenever(mockFileOrchestrator.getFlushableFiles()).thenReturn(fakeFiles) + fakeFiles.forEachIndexed { index, file -> + whenever( + mockFileReader.readData(file) + ).thenReturn(fakeBatches[index]) + val fakeBatchFile = + forge.aNullable { mock().apply { whenever(exists()) doReturn false } } + whenever( + mockFileOrchestrator.getMetadataFile(file) + ).thenReturn(fakeBatchFile) + } + + // When + testedFlusher.flush(mockDataUploader) + + // Then + verifyZeroInteractions(mockMetaFileReader) + } + @Test fun `M do nothing W flush { no data available }`() { // Given diff --git a/dd-sdk-android/src/test/kotlin/com/datadog/android/v2/core/internal/storage/FileEventBatchWriterTest.kt b/dd-sdk-android/src/test/kotlin/com/datadog/android/v2/core/internal/storage/FileEventBatchWriterTest.kt index 682f5279a1..7cd817c08c 100644 --- a/dd-sdk-android/src/test/kotlin/com/datadog/android/v2/core/internal/storage/FileEventBatchWriterTest.kt +++ b/dd-sdk-android/src/test/kotlin/com/datadog/android/v2/core/internal/storage/FileEventBatchWriterTest.kt @@ -15,6 +15,7 @@ import com.datadog.android.v2.api.InternalLogger import com.datadog.android.v2.core.internal.storage.FileEventBatchWriter.Companion.ERROR_LARGE_DATA import com.datadog.android.v2.core.internal.storage.FileEventBatchWriter.Companion.WARNING_METADATA_WRITE_FAILED import com.nhaarman.mockitokotlin2.doReturn +import com.nhaarman.mockitokotlin2.mock import com.nhaarman.mockitokotlin2.verify import com.nhaarman.mockitokotlin2.verifyNoMoreInteractions import com.nhaarman.mockitokotlin2.verifyZeroInteractions @@ -29,6 +30,7 @@ import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import org.junit.jupiter.api.extension.ExtendWith import org.junit.jupiter.api.extension.Extensions +import org.junit.jupiter.api.io.TempDir import org.mockito.Mock import org.mockito.junit.jupiter.MockitoExtension import org.mockito.junit.jupiter.MockitoSettings @@ -334,20 +336,49 @@ internal class FileEventBatchWriterTest { verifyZeroInteractions(mockMetaReaderWriter) } + @Test + fun `𝕄 not read metadata 𝕎 currentMetadata() { file doesn't exist }`() { + // Given + val metaFile = mock().apply { + whenever(exists()) doReturn false + } + + testedWriter = FileEventBatchWriter( + batchFile = fakeBatchFile, + metadataFile = metaFile, + eventsWriter = mockBatchWriter, + metadataReaderWriter = mockMetaReaderWriter, + filePersistenceConfig = mockFilePersistenceConfig, + internalLogger = mockInternalLogger + ) + + // When + val meta = testedWriter.currentMetadata() + + // Then + assertThat(meta).isNull() + verifyZeroInteractions(mockMetaReaderWriter) + } + @Test fun `𝕄 read metadata 𝕎 currentMetadata()`( - @StringForgery fakeMetadata: String + @StringForgery fakeMetadata: String, + @TempDir fakeMetadataDir: File, + forge: Forge ) { // Given + val fakeMetaFile = File(fakeMetadataDir, forge.anAlphabeticalString()) + fakeMetaFile.createNewFile() + testedWriter = FileEventBatchWriter( batchFile = fakeBatchFile, - metadataFile = fakeBatchMetadataFile, + metadataFile = fakeMetaFile, eventsWriter = mockBatchWriter, metadataReaderWriter = mockMetaReaderWriter, filePersistenceConfig = mockFilePersistenceConfig, internalLogger = mockInternalLogger ) - whenever(mockMetaReaderWriter.readData(fakeBatchMetadataFile)) doReturn + whenever(mockMetaReaderWriter.readData(fakeMetaFile)) doReturn fakeMetadata.toByteArray() // When diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 5b781482fd..182262d713 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -35,11 +35,11 @@ jUnitVintage = "5.7.1" jUnitMockitoExt = "3.4.6" # Android JUnit -androidXJunitRunner = "1.4.0" -androidXJunitRules = "1.4.0" -androidXExtJunit = "1.1.3" -androidXJunitCore = "1.4.0" -espresso = "3.4.0" +androidXJunitRunner = "1.5.0" +androidXJunitRules = "1.5.0" +androidXExtJunit = "1.1.4" +androidXJunitCore = "1.5.0" +espresso = "3.5.0" # Tests Tools assertJ = "3.18.1" diff --git a/instrumented/nightly-tests/src/main/kotlin/com/datadog/android/nightly/activities/ResourceTrackingActivity.kt b/instrumented/nightly-tests/src/main/kotlin/com/datadog/android/nightly/activities/ResourceTrackingActivity.kt index 1447915ed0..37ebb08261 100644 --- a/instrumented/nightly-tests/src/main/kotlin/com/datadog/android/nightly/activities/ResourceTrackingActivity.kt +++ b/instrumented/nightly-tests/src/main/kotlin/com/datadog/android/nightly/activities/ResourceTrackingActivity.kt @@ -55,7 +55,7 @@ internal open class ResourceTrackingActivity : AppCompatActivity() { // endregion companion object { - internal const val HOST = "source.unsplash.com" - internal const val RANDOM_URL = "https://$HOST/random/800x450" + internal const val HOST = "picsum.photos" + internal const val RANDOM_URL = "https://$HOST/800/450" } } diff --git a/instrumented/nightly-tests/src/main/kotlin/com/datadog/android/nightly/activities/ResourceTrackingCustomSpanAttributesActivity.kt b/instrumented/nightly-tests/src/main/kotlin/com/datadog/android/nightly/activities/ResourceTrackingCustomSpanAttributesActivity.kt index 92c63da1fb..6c28d480a0 100644 --- a/instrumented/nightly-tests/src/main/kotlin/com/datadog/android/nightly/activities/ResourceTrackingCustomSpanAttributesActivity.kt +++ b/instrumented/nightly-tests/src/main/kotlin/com/datadog/android/nightly/activities/ResourceTrackingCustomSpanAttributesActivity.kt @@ -41,6 +41,6 @@ internal class ResourceTrackingCustomSpanAttributesActivity : ResourceTrackingAc companion object { internal const val TEST_METHOD_NAME = "rum_resource_tracking_with_custom_span_attributes" - internal const val RANDOM_RESOURCE_WITH_CUSTOM_SPAN = "https://$HOST/random/25x25" + internal const val RANDOM_RESOURCE_WITH_CUSTOM_SPAN = "https://$HOST/25/25" } } diff --git a/instrumented/nightly-tests/src/main/kotlin/com/datadog/android/nightly/activities/ResourceTrackingNetworkInterceptorActivity.kt b/instrumented/nightly-tests/src/main/kotlin/com/datadog/android/nightly/activities/ResourceTrackingNetworkInterceptorActivity.kt index 4cdd418651..ca7396477e 100644 --- a/instrumented/nightly-tests/src/main/kotlin/com/datadog/android/nightly/activities/ResourceTrackingNetworkInterceptorActivity.kt +++ b/instrumented/nightly-tests/src/main/kotlin/com/datadog/android/nightly/activities/ResourceTrackingNetworkInterceptorActivity.kt @@ -46,6 +46,6 @@ internal class ResourceTrackingNetworkInterceptorActivity : ResourceTrackingActi companion object { internal const val RANDOM_RESOURCE_WITH_REDIRECT_URL = - "https://$HOST/random/20x20" + "https://$HOST/20/20" } }