Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BazelTestRunner: exit with 137 on OOMs #24436

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ java_library(
deps = [
"//src/java_tools/junitrunner/java/com/google/testing/junit/runner/internal",
"//src/java_tools/junitrunner/java/com/google/testing/junit/runner/junit4",
"//third_party:junit4",
],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import org.junit.runner.Result;

/**
* A class to run JUnit tests in a controlled environment.
Expand All @@ -44,6 +45,11 @@ public class BazelTestRunner {
*/
static final String TEST_SUITE_PROPERTY_NAME = "bazel.test_suite";

private static final int EXIT_CODE_SUCCESS = 0;
private static final int EXIT_CODE_TEST_FAILURE_OTHER = 1;
private static final int EXIT_CODE_TEST_RUNNER_FAILURE = 2;
private static final int EXIT_CODE_TEST_FAILURE_OOM = 137;

private BazelTestRunner() {
// utility class; should not be instantiated
}
Expand All @@ -59,6 +65,7 @@ private BazelTestRunner() {
* <p>Return codes:
* <ul>
* <li>Test runner failure, bad arguments, etc.: exit code of 2</li>
* <li>Test failure that included an OutOfMemoryException: exit code of 137</li>
* <li>Normal test failure: exit code of 1</li>
* <li>All tests pass: exit code of 0</li>
* </ul>
Expand All @@ -68,7 +75,7 @@ public static void main(String[] args) {

String suiteClassName = System.getProperty(TEST_SUITE_PROPERTY_NAME);
if (!checkTestSuiteProperty(suiteClassName)) {
System.exit(2);
System.exit(EXIT_CODE_TEST_RUNNER_FAILURE);
}

int exitCode;
Expand All @@ -79,7 +86,8 @@ public static void main(String[] args) {
// logged
// by the executing strategy, and return a failure, so this process can gracefully shut down.
e.printStackTrace();
exitCode = 1;
exitCode =
e instanceof OutOfMemoryError ? EXIT_CODE_TEST_FAILURE_OOM : EXIT_CODE_TEST_FAILURE_OTHER;
}

System.err.printf("%nBazelTestRunner exiting with a return value of %d%n", exitCode);
Expand Down Expand Up @@ -135,14 +143,21 @@ private static int runTestsInSuite(String suiteClassName, String[] args) {
// No class found corresponding to the system property passed in from Bazel
if (args.length == 0 && suiteClassName != null) {
System.err.printf("Class not found: [%s]%n", suiteClassName);
return 2;
return EXIT_CODE_TEST_RUNNER_FAILURE;
}
}

// TODO(kush): Use a new classloader for the following instantiation.
JUnit4Runner runner =
JUnit4Bazel.builder().suiteClass(suite).config(new Config(args)).build().runner();
return runner.run().wasSuccessful() ? 0 : 1;
Result result = runner.run();
if (result.wasSuccessful()) {
return EXIT_CODE_SUCCESS;
}
return result.getFailures().stream()
.anyMatch(failure -> failure.getException() instanceof OutOfMemoryError)
? EXIT_CODE_TEST_FAILURE_OOM
: EXIT_CODE_TEST_FAILURE_OTHER;
}

private static Class<?> getTestClass(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ set +o errexit
TESTBED="${PWD}/$2"
SUITE_PARAMETER="$3"
SUITE_FLAG="-D${SUITE_PARAMETER}=com.google.testing.junit.runner.testbed.JUnit4TestbridgeExercises"
OOM_SUITE_FLAG="-D${SUITE_PARAMETER}=com.google.testing.junit.runner.testbed.JUnit4TestbridgeOomExercises"
XML_OUTPUT_FILE="${TEST_TMPDIR}/test.xml"

shift 3
Expand Down Expand Up @@ -67,6 +68,44 @@ function test_Junit4() {
rm -rf "${XML_OUTPUT_FILE}" || fail "failed to remove XML output"
}

# Test that the exit code reflects the success / failure reason.
function test_Junit4ExitCodes() {
cd "${TEST_TMPDIR}" || fail "Unexpected failure"

# Run the test without environment flag; it should fail.
declare +x TESTBRIDGE_TEST_ONLY
"${TESTBED}" --jvm_flag="${OOM_SUITE_FLAG}" >& "${TEST_log}"
assert_equals 137 $? || fail "Expected OOM failure"
expect_log 'Failures: 2'

# Run the test with environment flag and check the different expected exit codes.

declare -x TESTBRIDGE_TEST_ONLY="testFailAssertion"
"${TESTBED}" --jvm_flag="${OOM_SUITE_FLAG}" >& "${TEST_log}" && fail "Expected failure"
assert_equals 1 $? || fail "Expected non-OOM failure"
expect_log 'Failures: 1'

declare -x TESTBRIDGE_TEST_ONLY="testFailWithOom"
"${TESTBED}" --jvm_flag="${OOM_SUITE_FLAG}" >& "${TEST_log}" && fail "Expected failure"
assert_equals 137 $? || fail "Expected OOM failure on single test case"
expect_log 'Failures: 1'

declare -x TESTBRIDGE_TEST_ONLY="testPass"
"${TESTBED}" --jvm_flag="${OOM_SUITE_FLAG}" >& "${TEST_log}"
assert_equals 0 $? || fail "Expected success"
expect_log 'OK.*1 test'

# Finally, run the test once again without environment flag; it should fail.
declare +x TESTBRIDGE_TEST_ONLY
"${TESTBED}" --jvm_flag="${OOM_SUITE_FLAG}" >& "${TEST_log}"
assert_equals 137 $? || fail "Expected OOM failure again"
expect_log 'Failures: 2'

# Remove the XML output with failures, so it does not get picked up to
# indicate a failure.
rm -rf "${XML_OUTPUT_FILE}" || fail "failed to remove XML output"
}

# Test that TESTBRIDGE_TEST_ONLY is overridden by a direct flag.
function test_Junit4FlagOverridesEnv() {
cd "${TEST_TMPDIR}" || fail "Unexpected failure"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2024 The Bazel Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.testing.junit.runner.testbed;

import static org.junit.Assert.fail;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
* A JUnit4-style test meant to be invoked by junit4_testbridge_tests.sh.
*/
@RunWith(JUnit4.class)
public class JUnit4TestbridgeOomExercises {
@Test
public void testPass() {}

@Test
public void testFailAssertion() {
fail();
}

@Test
public void testFailWithOom() {
throw new OutOfMemoryError("testing");
}
}