Skip to content

Commit

Permalink
feature(test): support unknown INSTRUMENTATION_CODE values (#13)
Browse files Browse the repository at this point in the history
* feature(test): support unknown INSTRUMENTATION_CODE values
  • Loading branch information
Malinskiy authored Dec 23, 2020
1 parent 1e4533b commit 4eb79b5
Show file tree
Hide file tree
Showing 4 changed files with 615 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ class InstrumentationResponseTransformer : ResponseTransformer<List<TestEvent>?>
}
}

/**
* 1 - Test running
* 0 - Test passed
* -2 - assertion failure
* -1 - other exceptions
*/
private fun parseStatusCode(last: String, atom: List<String>): List<TestEvent>? {
val value = last.substring(TokenType.INSTRUMENTATION_STATUS_CODE.name.length + 1).trim()
val parameters: Map<String, String> = atom.toMap()
Expand Down Expand Up @@ -169,6 +175,11 @@ class InstrumentationResponseTransformer : ResponseTransformer<List<TestEvent>?>
}
}

/**
* Session Result Code:
* -1: Success
* other: Failure
*/
private fun parseInstrumentationCode(
last: String,
atom: List<String>
Expand All @@ -190,7 +201,10 @@ class InstrumentationResponseTransformer : ResponseTransformer<List<TestEvent>?>
finishReported = true
listOf(TestRunEnded(time, metrics))
}
else -> null
else -> {
finishReported = true
listOf(TestRunFailed("Unexpected INSTRUMENTATION_CODE: $code"))
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,27 @@ class InstrumentationResponseTransformerTest {
.isEqualTo(javaClass.getResourceAsStream("/instrumentation/log_5.expected").reader().readText())
}
}

/**
* This is purely theoretical scenario since this hasn't been observed in practice but is possible
*/
@Test
fun testOtherFailure() = runBlocking {
val transformer = InstrumentationResponseTransformer()

val lines = javaClass.getResourceAsStream("/instrumentation/log_6.input").reader().readLines()

val events = mutableListOf<TestEvent>()
for (line in lines) {
val bytes = (line + '\n').toByteArray(Const.DEFAULT_TRANSPORT_ENCODING)
transformer.process(bytes, 0, bytes.size)
transformer.transform()?.let {
events.addAll(it)
}
}
transformer.close()?.let { events.addAll(it) }

assertThat(events.map { it.toString() }.reduce { acc, s -> acc + "\n" + s })
.isEqualTo(javaClass.getResourceAsStream("/instrumentation/log_6.expected").reader().readText())
}
}
122 changes: 122 additions & 0 deletions src/test/resources/instrumentation/log_6.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
TestRunStartedEvent(testCount=9)
TestStarted(id=TestIdentifier(className=com.example.MainActivityFlakyTest, testName=testTextFlaky))
TestEnded(id=TestIdentifier(className=com.example.MainActivityFlakyTest, testName=testTextFlaky), metrics={})
TestStarted(id=TestIdentifier(className=com.example.MainActivityFlakyTest, testName=testTextFlaky1))
TestEnded(id=TestIdentifier(className=com.example.MainActivityFlakyTest, testName=testTextFlaky1), metrics={})
TestStarted(id=TestIdentifier(className=com.example.MainActivityFlakyTest, testName=testTextFlaky2))
TestEnded(id=TestIdentifier(className=com.example.MainActivityFlakyTest, testName=testTextFlaky2), metrics={})
TestStarted(id=TestIdentifier(className=com.example.MainActivityFlakyTest, testName=testTextFlaky3))
TestEnded(id=TestIdentifier(className=com.example.MainActivityFlakyTest, testName=testTextFlaky3), metrics={})
TestStarted(id=TestIdentifier(className=com.example.MainActivityFlakyTest, testName=testTextFlaky4))
TestFailed(id=TestIdentifier(className=com.example.MainActivityFlakyTest, testName=testTextFlaky4), stackTrace=java.lang.AssertionError
at org.junit.Assert.fail(Assert.java:86)
at org.junit.Assert.assertTrue(Assert.java:41)
at org.junit.Assert.assertTrue(Assert.java:52)
at com.example.MainActivityFlakyTest.testTextFlaky4(MainActivityFlakyTest.kt:57)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:433)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:58)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:375)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1932))
TestEnded(id=TestIdentifier(className=com.example.MainActivityFlakyTest, testName=testTextFlaky4), metrics={})
TestStarted(id=TestIdentifier(className=com.example.MainActivityFlakyTest, testName=testTextFlaky5))
TestEnded(id=TestIdentifier(className=com.example.MainActivityFlakyTest, testName=testTextFlaky5), metrics={})
TestStarted(id=TestIdentifier(className=com.example.MainActivityFlakyTest, testName=testTextFlaky6))
TestFailed(id=TestIdentifier(className=com.example.MainActivityFlakyTest, testName=testTextFlaky6), stackTrace=java.lang.AssertionError
at org.junit.Assert.fail(Assert.java:86)
at org.junit.Assert.assertTrue(Assert.java:41)
at org.junit.Assert.assertTrue(Assert.java:52)
at com.example.MainActivityFlakyTest.testTextFlaky6(MainActivityFlakyTest.kt:69)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:433)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:58)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:375)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1932))
TestEnded(id=TestIdentifier(className=com.example.MainActivityFlakyTest, testName=testTextFlaky6), metrics={})
TestStarted(id=TestIdentifier(className=com.example.MainActivityFlakyTest, testName=testTextFlaky7))
TestFailed(id=TestIdentifier(className=com.example.MainActivityFlakyTest, testName=testTextFlaky7), stackTrace=java.lang.AssertionError
at org.junit.Assert.fail(Assert.java:86)
at org.junit.Assert.assertTrue(Assert.java:41)
at org.junit.Assert.assertTrue(Assert.java:52)
at com.example.MainActivityFlakyTest.testTextFlaky7(MainActivityFlakyTest.kt:75)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:433)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:58)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:375)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1932))
TestEnded(id=TestIdentifier(className=com.example.MainActivityFlakyTest, testName=testTextFlaky7), metrics={})
TestStarted(id=TestIdentifier(className=com.example.MainActivityFlakyTest, testName=testTextFlaky8))
TestEnded(id=TestIdentifier(className=com.example.MainActivityFlakyTest, testName=testTextFlaky8), metrics={})
TestRunFailed(error=Unexpected INSTRUMENTATION_CODE: 0)
Loading

0 comments on commit 4eb79b5

Please sign in to comment.