Skip to content

Commit

Permalink
fix: Uploading performance metrics for multiple matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Adamczyk committed Nov 18, 2020
1 parent 415330a commit 8081540
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 25 deletions.
38 changes: 13 additions & 25 deletions test_runner/src/main/kotlin/ftl/gc/GcStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import ftl.reports.xml.model.JUnitTestResult
import ftl.reports.xml.parseAllSuitesXml
import ftl.reports.xml.xmlToString
import ftl.run.exception.FlankGeneralError
import ftl.util.ProgressBar
import ftl.util.join
import ftl.util.runWithProgress
import java.io.File
import java.io.FileOutputStream
import java.net.URI
Expand Down Expand Up @@ -73,16 +73,11 @@ object GcStorage {
val name = rawPath.substringAfter('/')

val fileBlob = BlobInfo.newBuilder(bucket, name).build()

val progress = ProgressBar()
try {
progress.start("Uploading smart flank XML")
storage.create(fileBlob, testResult.xmlToString().toByteArray())
} catch (e: Exception) {
throw FlankGeneralError(e)
} finally {
progress.stop()
}
runWithProgress(
startMessage = "Uploading smart flank XML",
action = { storage.create(fileBlob, testResult.xmlToString().toByteArray()) },
onError = { throw FlankGeneralError(it) }
)
}

fun uploadPerformanceMetrics(perfMetricsSummary: PerfMetricsSummary, resultsBucket: String, resultDir: String) =
Expand Down Expand Up @@ -141,10 +136,8 @@ object GcStorage {
storage: Storage = GcStorage.storage
): String {
val file = File(filePath)
val absolutePath = file.absolutePath
val fileName = file.name
return uploadCache[absolutePath] ?: uploadCache.computeIfAbsent(absolutePath) {
val gcsPath = join(runGcsPath, fileName)
return uploadCache.computeIfAbsent("$runGcsPath-${file.absolutePath}") {
val gcsPath = join(runGcsPath, file.name)
val index = duplicatedGcsPathCounter.merge(gcsPath, 0) { old, _ -> old + 1 }
val validGcsPath = when {
index == 0 -> gcsPath
Expand All @@ -154,16 +147,11 @@ object GcStorage {

// 404 Not Found error when rootGcsBucket does not exist
val fileBlob = BlobInfo.newBuilder(rootGcsBucket, validGcsPath).build()

val progress = ProgressBar()
try {
progress.start("Uploading $fileName")
storage.create(fileBlob, fileBytes)
} catch (e: Exception) {
throw FlankGeneralError("Error on uploading $fileName\nCause: $e")
} finally {
progress.stop()
}
runWithProgress(
startMessage = "Uploading ${file.name}",
action = { storage.create(fileBlob, fileBytes) },
onError = { throw FlankGeneralError("Error on uploading ${file.name}\nCause: $it") }
)
GCS_PREFIX + join(rootGcsBucket, validGcsPath)
}
}
Expand Down
16 changes: 16 additions & 0 deletions test_runner/src/main/kotlin/ftl/util/ProgressBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,19 @@ private class ProgressBarTask : TimerTask() {
print(".")
}
}

fun runWithProgress(
startMessage: String,
action: () -> Unit,
onError: (Exception) -> Unit
) {
val progress = ProgressBar()
try {
progress.start(startMessage)
action()
} catch (e: Exception) {
onError(e)
} finally {
progress.stop()
}
}
52 changes: 52 additions & 0 deletions test_runner/src/test/kotlin/ftl/util/ProgressBarTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package ftl.util

import com.google.common.truth.Truth.assertThat
import ftl.test.util.TestHelper.normalizeLineEnding
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import org.junit.Rule
import org.junit.Test
import org.junit.contrib.java.lang.system.SystemOutRule
Expand All @@ -23,4 +26,53 @@ class ProgressBarTest {
progress.stop()
assertThat(systemOutRule.log.normalizeLineEnding()).isEqualTo(" hi .\n")
}

@Test
fun `should properly wrap progress bar execution using helper function for success`() {
// given
val action: Runnable = mockk {
every { run() } returns Unit
}
val onError: Runnable = mockk {
every { run() } returns Unit
}

// when
runWithProgress(
startMessage = "TEST",
action = { action.run() },
onError = { onError.run() }
)

// then
assertThat(systemOutRule.log).contains(" TEST .")
verify { action.run() }
verify(exactly = 0) { onError.run() }
}

@Test
fun `should properly wrap progress bar execution using helper function for failure`() {
// given
val action: Runnable = mockk {
every { run() } returns Unit
}
val onError: Runnable = mockk {
every { run() } returns Unit
}

// when
runWithProgress(
startMessage = "TEST",
action = {
action.run()
throw Exception()
},
onError = { onError.run() }
)

// then
assertThat(systemOutRule.log).contains(" TEST .")
verify { action.run() }
verify { onError.run() }
}
}

0 comments on commit 8081540

Please sign in to comment.