Skip to content

Commit

Permalink
Merge branch 'fix/android_remote_parser_listener_2' into snapshot/0.9…
Browse files Browse the repository at this point in the history
….1/22.12.23.1
  • Loading branch information
matzuk committed Dec 22, 2023
2 parents 1c1cf32 + 68d5981 commit 704930f
Showing 1 changed file with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.NonCancellable.isActive
import kotlinx.coroutines.withTimeoutOrNull

private const val LISTENER_ARGUMENT = "listener"
private const val TEST_ANNOTATION_PRODUCER = "com.malinskiy.adam.junit4.android.listener.TestAnnotationProducer"

class AmInstrumentTestParser(
private val configuration: Configuration,
private val testBundleIdentifier: AndroidTestBundleIdentifier,
Expand All @@ -42,15 +45,22 @@ class AmInstrumentTestParser(

override suspend fun extract(device: Device): List<Test> {
val testBundles = vendorConfiguration.testBundlesCompat()
var blockListenerArgumentOverride = false
return withRetry(3, 0) {
try {
val device = device as? AdamAndroidDevice ?: throw ConfigurationException("Unexpected device type for remote test parsing")
return@withRetry parseTests(device, configuration, vendorConfiguration, testBundles)
return@withRetry parseTests(device, configuration, vendorConfiguration, testBundles, blockListenerArgumentOverride)
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
logger.debug(e) { "Remote parsing failed. Retrying" }
} catch (e: PossibleListenerIssueException) {
logger.warn { "The previous parse operation failed. The most possible reason is " +
"a developer missed this step https://docs.marathonlabs.io/android/configure#test-parser. " +
"The next attempt will be done without overridden testrun listener." }
blockListenerArgumentOverride = true
throw e
} catch (throwable: Throwable) {
logger.debug(throwable) { "Remote parsing failed. Retrying" }
throw throwable
}
}
}
Expand All @@ -59,7 +69,8 @@ class AmInstrumentTestParser(
device: AdamAndroidDevice,
configuration: Configuration,
vendorConfiguration: VendorConfiguration.AndroidConfiguration,
testBundles: List<AndroidTestBundle>
testBundles: List<AndroidTestBundle>,
blockListenerArgumentOverride: Boolean,
): List<Test> {
return testBundles.flatMap { bundle ->
val androidTestBundle =
Expand All @@ -68,7 +79,11 @@ class AmInstrumentTestParser(

val testParserConfiguration = vendorConfiguration.testParserConfiguration
val overrides: Map<String, String> = when {
testParserConfiguration is TestParserConfiguration.RemoteTestParserConfiguration -> testParserConfiguration.instrumentationArgs
testParserConfiguration is TestParserConfiguration.RemoteTestParserConfiguration -> {
if (blockListenerArgumentOverride) testParserConfiguration.instrumentationArgs
.filterNot { it.key == LISTENER_ARGUMENT && it.value == TEST_ANNOTATION_PRODUCER }
else testParserConfiguration.instrumentationArgs
}
else -> emptyMap()
}

Expand Down Expand Up @@ -112,7 +127,10 @@ class AmInstrumentTestParser(
testBundleIdentifier.put(test, androidTestBundle)
}

is TestRunFailed -> Unit
is TestRunFailed -> {
if (overrides.containsKey(LISTENER_ARGUMENT) && overrides[LISTENER_ARGUMENT] == TEST_ANNOTATION_PRODUCER)
throw PossibleListenerIssueException()
}
is TestRunStopped -> Unit
is TestRunEnded -> Unit
}
Expand All @@ -131,3 +149,5 @@ class AmInstrumentTestParser(
}
}
}

private class PossibleListenerIssueException : RuntimeException()

0 comments on commit 704930f

Please sign in to comment.