Skip to content

Commit

Permalink
Merge pull request #35 from PabloMGrela/feature/progress-indicator
Browse files Browse the repository at this point in the history
Add progress to logs
  • Loading branch information
joshschriever authored Jun 5, 2020
2 parents 845c333 + 7e99ad2 commit 1750280
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
17 changes: 14 additions & 3 deletions gordon-plugin/src/main/kotlin/com/banno/gordon/TestDistributor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ internal fun runAllTests(
dispatcher,
deviceSerials,
allTestCases.associateWith { TestResult.NotRun }
)
),
totalTests = allTestCases.size
).awaitAll()
.fold(emptyMap()) { accumulated, item -> accumulated + item }

Expand Down Expand Up @@ -132,14 +133,20 @@ private fun CoroutineScope.runTestsInPool(
instrumentationRunnerOptions: InstrumentationRunnerOptions,
testTimeoutMillis: Long,
devices: List<JadbDevice>,
testDistributor: TestDistributor
testDistributor: TestDistributor,
totalTests: Int? = null
): List<Deferred<Map<TestCase, TestResult>>> {
var index = 0
return devices.map { device ->
async(context = dispatcher, start = CoroutineStart.LAZY) {
testDistributor.testCasesChannel(device.serial)
.consumeAsFlow()
.map { test ->
index++
val progress = getProgress(index, totalTests)

if (test.isIgnored) {
logger.logIgnored(test, progress)
test to TestResult.Ignored
} else {
test to runTest(
Expand All @@ -148,7 +155,8 @@ private fun CoroutineScope.runTestsInPool(
instrumentationRunnerOptions = instrumentationRunnerOptions,
testTimeoutMillis = testTimeoutMillis,
test = test,
device = device
device = device,
progress = progress
)
}
}
Expand All @@ -158,6 +166,9 @@ private fun CoroutineScope.runTestsInPool(
}
}

private fun getProgress(currentTest: Int, totalTests: Int?) =
totalTests?.let { "$currentTest/$totalTests" } ?: "Retrying"

private class TestDistributor(
private val dispatcher: CoroutineDispatcher,
private val deviceSerials: Set<String>,
Expand Down
21 changes: 13 additions & 8 deletions gordon-plugin/src/main/kotlin/com/banno/gordon/TestRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ internal fun runTest(
instrumentationRunnerOptions: InstrumentationRunnerOptions,
testTimeoutMillis: Long,
test: TestCase,
device: JadbDevice
device: JadbDevice,
progress: String
): TestResult {
val targetInstrumentation = "$instrumentationPackage/${instrumentationRunnerOptions.testInstrumentationRunner}"

Expand All @@ -27,14 +28,12 @@ internal fun runTest(

val command = "am instrument $flags $options $targetInstrumentation"

val testName = "${test.fullyQualifiedClassName.substringAfterLast('.')}.${test.methodName}"

return device.executeShellWithTimeout(testTimeoutMillis, command)
.attempt()
.unsafeRunSync()
.fold(
{
logger.error("${device.serial}: $testName: UNABLE TO RUN")
logger.error("$progress -> ${device.serial}: ${test.classAndMethodName}: UNABLE TO RUN")
TestResult.NotRun
}
) { shellOutput: String? ->
Expand All @@ -45,25 +44,31 @@ internal fun runTest(

when {
shellOutput == null -> {
logger.error("${device.serial}: $testName: TIMED OUT")
logger.error("$progress -> ${device.serial}: ${test.classAndMethodName}: TIMED OUT")
TestResult.Failed(testTime, "Test timed out", device.serial)
}

shellOutput.endsWith("OK (1 test)") -> {
logger.lifecycle("${device.serial}: $testName: PASSED")
logger.lifecycle("$progress -> ${device.serial}: ${test.classAndMethodName}: PASSED")
TestResult.Passed(testTime)
}

shellOutput.endsWith("OK (0 tests)") -> {
logger.lifecycle("${device.serial}: $testName: IGNORED")
logger.lifecycle("$progress -> ${device.serial}: ${test.classAndMethodName}: IGNORED")
TestResult.Ignored
}

else -> {
val failureOutput = shellOutput.substringBeforeLast("There was 1 failure")
logger.error("${device.serial}: $testName: FAILED\n$failureOutput\n")
logger.error("$progress -> ${device.serial}: ${test.classAndMethodName}: FAILED\n$failureOutput\n")
TestResult.Failed(testTime, failureOutput, device.serial)
}
}
}
}

internal fun Logger.logIgnored(test: TestCase, progress: String) =
lifecycle("$progress -> ${test.classAndMethodName}: IGNORED")

private val TestCase.classAndMethodName
get() = "${fullyQualifiedClassName.substringAfterLast('.')}.$methodName"

0 comments on commit 1750280

Please sign in to comment.