From 101ea31fdffda1d6245dde29e86f8e6ede694a04 Mon Sep 17 00:00:00 2001 From: Michael McCandless Date: Thu, 9 Oct 2014 18:23:04 -0400 Subject: [PATCH] Tests: move thread stacks on failure to a RunListener, so it actually works --- .../lucene/util/AbstractRandomizedTest.java | 4 +- .../test/ElasticsearchIntegrationTest.java | 6 +-- .../test/PrintAllThreadStacksOnFailure.java | 42 +++++++++++++++++++ 3 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 src/test/java/org/elasticsearch/test/PrintAllThreadStacksOnFailure.java diff --git a/src/test/java/org/apache/lucene/util/AbstractRandomizedTest.java b/src/test/java/org/apache/lucene/util/AbstractRandomizedTest.java index af934d38b7c..54701f2d60e 100644 --- a/src/test/java/org/apache/lucene/util/AbstractRandomizedTest.java +++ b/src/test/java/org/apache/lucene/util/AbstractRandomizedTest.java @@ -34,6 +34,7 @@ import org.elasticsearch.common.util.concurrent.EsExecutors; import org.elasticsearch.test.CurrentTestFailedMarker; import org.elasticsearch.test.ElasticsearchIntegrationTest; import org.elasticsearch.test.ElasticsearchTestCase; +import org.elasticsearch.test.PrintAllThreadStacksOnFailure; import org.elasticsearch.test.junit.listeners.ReproduceInfoPrinter; import org.junit.After; import org.junit.Before; @@ -58,7 +59,8 @@ import java.util.logging.Logger; @Listeners({ ReproduceInfoPrinter.class, FailureMarker.class, - CurrentTestFailedMarker.class + CurrentTestFailedMarker.class, + PrintAllThreadStacksOnFailure.class }) @RunWith(value = com.carrotsearch.randomizedtesting.RandomizedRunner.class) @SuppressCodecs(value = "Lucene3x") diff --git a/src/test/java/org/elasticsearch/test/ElasticsearchIntegrationTest.java b/src/test/java/org/elasticsearch/test/ElasticsearchIntegrationTest.java index 1a46f2c999f..a5194d40289 100644 --- a/src/test/java/org/elasticsearch/test/ElasticsearchIntegrationTest.java +++ b/src/test/java/org/elasticsearch/test/ElasticsearchIntegrationTest.java @@ -606,12 +606,8 @@ public abstract class ElasticsearchIntegrationTest extends ElasticsearchTestCase logger.info("[{}#{}]: cleaned up after test", getTestClass().getSimpleName(), getTestName()); success = true; } finally { + // TODO: it looks like CurrentTestFailedMarker is never set at this point, so testFailed() will always be false? if (!success || CurrentTestFailedMarker.testFailed()) { - - logger.info("[{}#{}]: now dump all thread stacks on failure", getTestClass().getSimpleName(), getTestName()); - ElasticsearchTestCase.printStackDump(logger); - logger.info("[{}#{}]: done dump all thread stacks on failure", getTestClass().getSimpleName(), getTestName()); - // if we failed that means that something broke horribly so we should // clear all clusters and if the current cluster is the global we shut that one // down as well to prevent subsequent tests from failing due to the same problem. diff --git a/src/test/java/org/elasticsearch/test/PrintAllThreadStacksOnFailure.java b/src/test/java/org/elasticsearch/test/PrintAllThreadStacksOnFailure.java new file mode 100644 index 00000000000..691aa9f6155 --- /dev/null +++ b/src/test/java/org/elasticsearch/test/PrintAllThreadStacksOnFailure.java @@ -0,0 +1,42 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you 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 org.elasticsearch.test; + +import org.elasticsearch.common.logging.ESLogger; +import org.elasticsearch.common.logging.Loggers; +import org.junit.runner.Description; +import org.junit.runner.notification.Failure; +import org.junit.runner.notification.RunListener; + +/** + * A {@link RunListener} that detects test failures. We need it because we need + * to reset the global / suite level cluster if a test fails but don't wanna reset it + * for every subsequent test. + */ +public class PrintAllThreadStacksOnFailure extends RunListener { + + private final ESLogger logger = Loggers.getLogger(getClass()); + + @Override + public void testFailure(Failure failure) throws Exception { + logger.info("now dump all thread stacks on failure"); + ElasticsearchTestCase.printStackDump(logger); + logger.info("done dump all thread stacks on failure"); + } +}