Skip to content
Closed
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 @@ -14,6 +14,7 @@

package com.google.testing.junit.runner;

import com.google.testing.junit.runner.internal.SignalHandlers;
import com.google.testing.junit.runner.internal.StackTraces;
import com.google.testing.junit.runner.junit4.JUnit4Bazel;
import com.google.testing.junit.runner.junit4.JUnit4InstanceModules.Config;
Expand All @@ -27,6 +28,7 @@
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
import org.junit.runner.Result;
import sun.misc.Signal;

/**
* A class to run JUnit tests in a controlled environment.
Expand All @@ -40,6 +42,7 @@
* <p>It also traps SIGTERM signals to make sure that the test report is written when the signal is
* closed by the unit test framework for running over time.
*/
@SuppressWarnings("SunApi")
public class BazelTestRunner {
/**
* If no arguments are passed on the command line, use this System property to
Expand Down Expand Up @@ -78,6 +81,10 @@ private BazelTestRunner() {
public static void main(String[] args) {
PrintStream stderr = System.err;

// Install signal handlers early to ensure stack traces are printed even if the test
// is interrupted during suite creation.
installSignalHandlers(stderr);

String suiteClassName = System.getProperty(TEST_SUITE_PROPERTY_NAME);
if (!checkTestSuiteProperty(suiteClassName)) {
System.exit(EXIT_CODE_TEST_RUNNER_FAILURE);
Expand Down Expand Up @@ -255,4 +262,15 @@ private static void sleepUninterruptibly(long sleepForSeconds) {
}
}
}

/** Installs a SIGTERM handler that prints stack traces for all threads. */
private static void installSignalHandlers(PrintStream errPrintStream) {
SignalHandlers signalHandlers = new SignalHandlers(SignalHandlers.createRealHandlerInstaller());
signalHandlers.installHandler(
new Signal("TERM"),
signal -> {
errPrintStream.println("Received SIGTERM, dumping stack traces for all threads\n");
StackTraces.printAll(errPrintStream);
});
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.google.testing.junit.runner.internal.SignalHandlers;
import com.google.testing.junit.runner.internal.junit4.CancellableRequestFactory;
import com.google.testing.junit.runner.internal.junit4.JUnit4TestNameListener;
import com.google.testing.junit.runner.internal.junit4.JUnit4TestStackTraceListener;
import com.google.testing.junit.runner.internal.junit4.JUnit4TestXmlListener;
import com.google.testing.junit.runner.internal.junit4.SettableCurrentRunningTest;
import com.google.testing.junit.runner.model.TestSuiteModel;
Expand Down Expand Up @@ -127,9 +126,6 @@ Set<RunListener> setOfRunListeners(
Supplier<TestSuiteModel> testSuiteModelSupplier,
CancellableRequestFactory cancellableRequestFactory) {
Set<RunListener> listeners = new HashSet<>();
listeners.add(
new JUnit4TestStackTraceListener(
new SignalHandlers(SignalHandlers.createRealHandlerInstaller()), System.err));
listeners.add(
new JUnit4TestXmlListener(
testSuiteModelSupplier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,23 @@ sh_test(
],
)

sh_test(
name = "stack_trace_integration_test",
size = "medium",
srcs = ["stack_trace_integration_tests.sh"],
args = [
"$(location //src/test/shell:unittest.bash)",
"$(location :TestbedBinary)",
"bazel.test_suite",
"4",
],
data = [
":TestbedBinary",
"//src/test/shell:bashunit",
"//src/test/shell:unittest.bash",
],
)

sh_test(
name = "antxmlresultwriter_integration_test",
size = "medium",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,28 @@
# and checking the output.
#

DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# Load the unit-testing framework
source "$1" || \
{ echo "Failed to load unit-testing framework $1" >&2; exit 1; }

set +o errexit

unset TEST_PREMATURE_EXIT_FILE

TESTBED="${PWD}/$1"
SUITE_PARAMETER="$2"
JUNIT_VERSION="$3"
TESTBED="${PWD}/$2"
SUITE_PARAMETER="$3"
JUNIT_VERSION="$4"
SUITE="com.google.testing.junit.runner.testbed.StackTraceExercises"
SUITE_FLAG="-D${SUITE_PARAMETER}=${SUITE}"
SLOW_CREATION_SUITE_FLAG="-D${SUITE_PARAMETER}=com.google.testing.junit.runner.testbed.SuiteMethodTakesForever"

if [[ "$SUITE_PARAMETER" == "bazel.test_suite" ]]; then
is_bazel=true
else
is_bazel=false
fi

shift 3
source ${DIR}/testenv.sh || { echo "testenv.sh not found!" >&2; exit 1; }

# Usage: COUNT=count_in_log <regex>
function count_in_log() {
Expand Down Expand Up @@ -90,7 +100,11 @@ function test_ShutdownHook() {
kill -TERM $test_pid
sleep 3

expect_log 'INTERRUPTED TEST: SIGTERM'
if $is_bazel; then
expect_log 'Received SIGTERM, dumping stack traces for all threads'
else
expect_log 'INTERRUPTED TEST: SIGTERM'
fi
expect_log 'Shutdown\.runHooks'
expect_log 'StackTraceExercises\.handleHook'
expect_log 'Thread\.sleep'
Expand Down Expand Up @@ -120,7 +134,11 @@ function test_SlowSuite() {
kill -TERM $test_pid
sleep 3

expect_log "Execution interrupted while running 'TestSuite creation'"
if $is_bazel; then
expect_log 'Received SIGTERM, dumping stack traces for all threads'
else
expect_log "Execution interrupted while running 'TestSuite creation'"
fi

# expect threads to be dumped exactly once
expect_thread_dumps_in_log 1
Expand Down
Loading