Skip to content

Commit

Permalink
Merge pull request #524 from MarathonLabs/fix/android-test-sync-lifec…
Browse files Browse the repository at this point in the history
…ycle

fix(android): add afterTestRun hook
  • Loading branch information
Malinskiy authored Aug 11, 2021
2 parents a8e8633 + 7a8c7e0 commit 59763d7
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ object Versions {
val coroutines = "1.4.3"

val ddmlib = "27.1.2"
val adam = "0.3.1"
val adam = "0.3.2"
val dexTestParser = "2.3.3"
val kotlinLogging = "2.0.8"
val logbackClassic = "1.2.3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class AndroidDeviceTestRunner(private val device: AdamAndroidDevice, private val
logger.error(e) { errorMessage }
listener.testRunFailed(errorMessage)
} finally {
listener.afterTestRun()
channel?.cancel(null)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.malinskiy.marathon.android.model.TestRunResultsAccumulator
import com.malinskiy.marathon.time.Timer

abstract class AbstractTestRunResultListener(timer: Timer) : NoOpTestRunListener() {
private val runResult = TestRunResultsAccumulator(timer)
protected val runResult = TestRunResultsAccumulator(timer)

override suspend fun testRunStarted(runName: String, testCount: Int) {
runResult.testRunStarted(runName, testCount)
Expand Down Expand Up @@ -33,18 +33,13 @@ abstract class AbstractTestRunResultListener(timer: Timer) : NoOpTestRunListener

override suspend fun testRunFailed(errorMessage: String) {
runResult.testRunFailed(errorMessage)
handleTestRunResults(runResult)
}

override suspend fun testRunStopped(elapsedTime: Long) {
runResult.testRunStopped(elapsedTime)
handleTestRunResults(runResult)
}

override suspend fun testRunEnded(elapsedTime: Long, runMetrics: Map<String, String>) {
runResult.testRunEnded(elapsedTime, runMetrics)
handleTestRunResults(runResult)
}

abstract suspend fun handleTestRunResults(runResult: TestRunResultsAccumulator)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ interface AndroidTestRunListener {
suspend fun testRunStopped(elapsedTime: Long) {}

suspend fun testRunEnded(elapsedTime: Long, runMetrics: Map<String, String>) {}

suspend fun afterTestRun() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@ class CompositeTestRunListener(private val listeners: List<AndroidTestRunListene
override suspend fun testRunEnded(elapsedTime: Long, runMetrics: Map<String, String>) {
execute { it.testRunEnded(elapsedTime, runMetrics) }
}

override suspend fun afterTestRun() {
execute { it.afterTestRun() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class TestRunResultsListener(

private val logger = MarathonLogging.logger("TestRunResultsListener")

override suspend fun handleTestRunResults(runResult: TestRunResultsAccumulator) {
override suspend fun afterTestRun() {
val results = mergeParameterisedResults(runResult.testResults)
val tests = testBatch.tests.associateBy { it.identifier() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class TestRunResultsListenerTest {
testStarted(TestRunResultsAccumulatorTest.test5)

testRunEnded(1234, mapOf("metric1" to "value1"))
afterTestRun()
}

verify(attachmentProvider).registerListener(any())
Expand Down Expand Up @@ -155,6 +156,7 @@ class TestRunResultsListenerTest {
listener.apply {
beforeTestRun()
testRunEnded(1000, emptyMap())
afterTestRun()
}

val result = deferred.await()
Expand Down Expand Up @@ -208,6 +210,7 @@ class TestRunResultsListenerTest {
testStarted(id2)
testEnded(id2, emptyMap())
testRunEnded(1000, emptyMap())
afterTestRun()
}

val result = deferred.await()
Expand Down Expand Up @@ -252,6 +255,7 @@ class TestRunResultsListenerTest {
beforeTestRun()
testRunStarted("Testing", 3)
testRunFailed("Problems")
afterTestRun()
}

val result = deferred.await()
Expand Down Expand Up @@ -296,6 +300,7 @@ class TestRunResultsListenerTest {
testStarted(test1)
testEnded(test1, emptyMap())
testRunFailed("Problems")
afterTestRun()
}

val result = deferred.await()
Expand Down Expand Up @@ -351,6 +356,7 @@ class TestRunResultsListenerTest {
testStarted(id2)
testFailed(id2, "trace")
testRunEnded(1000, emptyMap())
afterTestRun()
}

val result = deferred.await()
Expand Down Expand Up @@ -404,6 +410,7 @@ class TestRunResultsListenerTest {
testStarted(id2)
testFailed(id2, "trace")
testRunEnded(1000, emptyMap())
afterTestRun()
}

val result = deferred.await()
Expand Down Expand Up @@ -455,6 +462,7 @@ class TestRunResultsListenerTest {
testStarted(id1)
testStarted(id2)
testRunEnded(1000, emptyMap())
afterTestRun()
}

val result = deferred.await()
Expand Down Expand Up @@ -509,6 +517,7 @@ class TestRunResultsListenerTest {
testStarted(id2)
testAssumptionFailure(id2, "trace2")
testRunEnded(1000, emptyMap())
afterTestRun()
}

val result = deferred.await()
Expand Down Expand Up @@ -565,6 +574,7 @@ class TestRunResultsListenerTest {
testStarted(id2)
testIgnored(id2)
testRunEnded(1000, emptyMap())
afterTestRun()
}

val result = deferred.await()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class AndroidDeviceTestRunner(private val device: DdmlibAndroidDevice, private v
logger.error(e) { errorMessage }
listener.testRunFailed(errorMessage)
} finally {

listener.afterTestRun()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class AndroidDeviceTestRunnerTest {
verify(listener).testStarted(eq(identifier))
verify(listener).testIgnored(eq(identifier))
verify(listener).testEnded(eq(identifier), eq(hashMapOf()))
verify(listener).afterTestRun()
}

verifyNoMoreInteractions(listener)
Expand Down

0 comments on commit 59763d7

Please sign in to comment.