Skip to content

Commit 7b091c1

Browse files
iirinadamienmg
authored andcommitted
Add a global failure when a test is interrupted/cancelled.
When a timeout occurred, the current test case is interrupted and the others are cancelled. This was not reflected in any way and all tests were reported as success, even if there was a timeout and the tests were cancelled/interrupted. Also add a status xml attribute to mark if the test was completed, cancelled or interrupted. Fixes #3763 RELNOTES: None. PiperOrigin-RevId: 169665622
1 parent 9faf8b7 commit 7b091c1

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed

src/java_tools/junitrunner/java/com/google/testing/junit/runner/model/AntXmlResultWriter.java

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public final class AntXmlResultWriter implements XmlResultWriter {
4949
private static final String JUNIT_ATTR_PROPERTY_VALUE = "value";
5050
private static final String JUNIT_ELEMENT_TESTCASE = "testcase";
5151
private static final String JUNIT_ELEMENT_FAILURE = "failure";
52+
private static final String JUNIT_ELEMENT_STATUS = "status";
5253
private static final String JUNIT_ATTR_FAILURE_MESSAGE = "message";
5354
private static final String JUNIT_ATTR_FAILURE_TYPE = "type";
5455
private static final String JUNIT_ATTR_TESTCASE_NAME = "name";
@@ -162,6 +163,7 @@ private void writeTestCase(XmlWriter writer, TestResult result,
162163
writer.writeAttribute(JUNIT_ATTR_TESTCASE_CLASSNAME, result.getClassName());
163164
writer.writeAttribute(JUNIT_ATTR_TESTCASE_TIME, getFormattedRunTime(
164165
result.getRunTimeInterval()));
166+
writer.writeAttribute(JUNIT_ELEMENT_STATUS, result.getStatus().toString());
165167

166168
for (Throwable failure : parentFailures) {
167169
writeThrowableToXmlWriter(writer, failure);

src/java_tools/junitrunner/java/com/google/testing/junit/runner/model/TestCaseNode.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,12 @@ public void started(long now) {
8181
@Override
8282
public void testInterrupted(long now) {
8383
if (compareAndSetState(State.STARTED, State.INTERRUPTED, now)) {
84+
globalFailures.add(new Exception("Test interrupted"));
8485
return;
8586
}
86-
compareAndSetState(INITIAL_STATES, State.CANCELLED, now);
87+
if (compareAndSetState(INITIAL_STATES, State.CANCELLED, now)) {
88+
globalFailures.add(new Exception("Test cancelled"));
89+
}
8790
}
8891

8992
@Override

src/java_tools/junitrunner/javatests/com/google/testing/junit/runner/testbed/XmlOutputExercises.ant.xml

+7-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</testsuite>
88
<testsuite name='com.google.testing.junit.runner.testbed.XmlOutputExercises$FailureTest' timestamp='' hostname='localhost' tests='1' failures='1' errors='0' time='' package='' id='1'>
99
<properties />
10-
<testcase name='testFail' classname='com.google.testing.junit.runner.testbed.XmlOutputExercises$FailureTest' time=''>
10+
<testcase name='testFail' classname='com.google.testing.junit.runner.testbed.XmlOutputExercises$FailureTest' time='' status='COMPLETED'>
1111
<failure message='This is an expected error. The test is supposed to fail.' type='java.lang.AssertionError'>java.lang.AssertionError: This is an expected error. The test is supposed to fail.
1212

1313
</failure>
@@ -17,22 +17,22 @@
1717
</testsuite>
1818
<testsuite name='com.google.testing.junit.runner.testbed.XmlOutputExercises$OtherTests' timestamp='' hostname='localhost' tests='1' failures='0' errors='0' time='' package='' id='2'>
1919
<properties />
20-
<testcase name='testToString' classname='com.google.testing.junit.runner.testbed.XmlOutputExercises$OtherTests' time='' />
20+
<testcase name='testToString' classname='com.google.testing.junit.runner.testbed.XmlOutputExercises$OtherTests' time='' status='COMPLETED' />
2121
<system-out />
2222
<system-err />
2323
</testsuite>
2424
<testsuite name='com.google.testing.junit.runner.testbed.XmlOutputExercises$EqualsHashCodeTest' timestamp='' hostname='localhost' tests='2' failures='0' errors='0' time='' package='' id='3'>
2525
<properties />
26-
<testcase name='testHashCode' classname='com.google.testing.junit.runner.testbed.XmlOutputExercises$EqualsHashCodeTest' time='' />
27-
<testcase name='testEquals' classname='com.google.testing.junit.runner.testbed.XmlOutputExercises$EqualsHashCodeTest' time='' />
26+
<testcase name='testHashCode' classname='com.google.testing.junit.runner.testbed.XmlOutputExercises$EqualsHashCodeTest' time='' status='COMPLETED' />
27+
<testcase name='testEquals' classname='com.google.testing.junit.runner.testbed.XmlOutputExercises$EqualsHashCodeTest' time='' status='COMPLETED' />
2828
<system-out />
2929
<system-err />
3030
</testsuite>
3131
<testsuite name='com.google.testing.junit.runner.testbed.XmlOutputExercises$ComparabilityTest' timestamp='' hostname='localhost' tests='3' failures='0' errors='0' time='' package='' id='4'>
3232
<properties />
33-
<testcase name='compareToEqualInstance' classname='com.google.testing.junit.runner.testbed.XmlOutputExercises$ComparabilityTest' time='' />
34-
<testcase name='compareToGreaterInstance' classname='com.google.testing.junit.runner.testbed.XmlOutputExercises$ComparabilityTest' time='' />
35-
<testcase name='compareToLessInstance' classname='com.google.testing.junit.runner.testbed.XmlOutputExercises$ComparabilityTest' time='' />
33+
<testcase name='compareToEqualInstance' classname='com.google.testing.junit.runner.testbed.XmlOutputExercises$ComparabilityTest' time='' status='COMPLETED' />
34+
<testcase name='compareToGreaterInstance' classname='com.google.testing.junit.runner.testbed.XmlOutputExercises$ComparabilityTest' time='' status='COMPLETED' />
35+
<testcase name='compareToLessInstance' classname='com.google.testing.junit.runner.testbed.XmlOutputExercises$ComparabilityTest' time='' status='COMPLETED' />
3636
<system-out />
3737
<system-err />
3838
</testsuite>

src/test/shell/bazel/bazel_java_test.sh

+46
Original file line numberDiff line numberDiff line change
@@ -1384,5 +1384,51 @@ EOF
13841384
expect_log "In java_common.create_provider the value of use_ijar is True. Make sure the java_toolchain argument is a valid java_toolchain Target."
13851385
}
13861386

1387+
function test_java_test_timeout() {
1388+
setup_javatest_support
1389+
mkdir -p javatests/com/google/timeout
1390+
touch javatests/com/google/timeout/{BUILD,TimeoutTests.java}
1391+
1392+
cat > javatests/com/google/timeout/TimeoutTests.java << EOF
1393+
package com.google.timeout;
1394+
1395+
import org.junit.runner.RunWith;
1396+
import org.junit.runners.JUnit4;
1397+
import org.junit.Test;
1398+
1399+
@RunWith(JUnit4.class)
1400+
public class TimeoutTests {
1401+
1402+
@Test
1403+
public void testPasses() throws InterruptedException { }
1404+
1405+
@Test
1406+
public void testTimesOut() throws InterruptedException {
1407+
// sleep more than 1 min
1408+
Thread.sleep(Long.MAX_VALUE);
1409+
}
1410+
}
1411+
EOF
1412+
1413+
cat > javatests/com/google/timeout/BUILD <<EOF
1414+
java_test(
1415+
name = "TimeoutTests",
1416+
srcs = ["TimeoutTests.java"],
1417+
deps = ['//third_party:junit4'],
1418+
timeout = "short", # 1 min
1419+
)
1420+
EOF
1421+
1422+
bazel test javatests/com/google/timeout:TimeoutTests --test_timeout=5 >& "$TEST_log" && fail "Unexpected success"
1423+
xml_log=bazel-testlogs/javatests/com/google/timeout/TimeoutTests/test.xml
1424+
[[ -s $xml_log ]] || fail "$xml_log was not present after test"
1425+
cat "$xml_log" > "$TEST_log"
1426+
expect_log "failures='2'"
1427+
expect_log "status='INTERRUPTED"
1428+
expect_log "status='CANCELLED'"
1429+
expect_log "<failure message='Test cancelled' type='java.lang.Exception'>java.lang.Exception: Test cancelled"
1430+
expect_log "<failure message='Test interrupted' type='java.lang.Exception'>java.lang.Exception: Test interrupted"
1431+
}
1432+
13871433
run_suite "Java integration tests"
13881434

0 commit comments

Comments
 (0)