Skip to content

Commit

Permalink
Merge pull request #1163 from DataDog/nogorodnikov/nightly-tests-impr…
Browse files Browse the repository at this point in the history
…ovements

Nightly tests improvements
  • Loading branch information
0xnm authored Nov 29, 2022
2 parents 3e8b300 + a49d3a1 commit 9eb9417
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 15 deletions.
2 changes: 2 additions & 0 deletions LICENSE-3rdparty.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ internal class DataFlusherTest {
) {
// Given
val fakeFiles = forge.aList { mock<File>() }
val fakeMetaFiles = forge.aList(fakeFiles.size) { forge.aNullable { mock<File>() } }
val fakeMetaFiles =
forge.aList(fakeFiles.size) {
forge.aNullable { mock<File>().apply { whenever(exists()) doReturn true } }
}
val fakeBatches = forge
.aList(fakeFiles.size) {
forge
Expand Down Expand Up @@ -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<File>() }
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<File>().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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -334,20 +336,49 @@ internal class FileEventBatchWriterTest {
verifyZeroInteractions(mockMetaReaderWriter)
}

@Test
fun `𝕄 not read metadata 𝕎 currentMetadata() { file doesn't exist }`() {
// Given
val metaFile = mock<File>().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
Expand Down
10 changes: 5 additions & 5 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

0 comments on commit 9eb9417

Please sign in to comment.