mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-03-01 00:19:11 +00:00
Dump stack traces of all threads on test failure due to AssertionError
Closes #12425
This commit is contained in:
parent
18079168e4
commit
878301c795
@ -54,6 +54,7 @@ import org.elasticsearch.env.Environment;
|
|||||||
import org.elasticsearch.env.NodeEnvironment;
|
import org.elasticsearch.env.NodeEnvironment;
|
||||||
import org.elasticsearch.test.cache.recycler.MockBigArrays;
|
import org.elasticsearch.test.cache.recycler.MockBigArrays;
|
||||||
import org.elasticsearch.test.cache.recycler.MockPageCacheRecycler;
|
import org.elasticsearch.test.cache.recycler.MockPageCacheRecycler;
|
||||||
|
import org.elasticsearch.test.junit.listeners.AssertionErrorThreadDumpPrinter;
|
||||||
import org.elasticsearch.test.junit.listeners.LoggingListener;
|
import org.elasticsearch.test.junit.listeners.LoggingListener;
|
||||||
import org.elasticsearch.test.junit.listeners.ReproduceInfoPrinter;
|
import org.elasticsearch.test.junit.listeners.ReproduceInfoPrinter;
|
||||||
import org.elasticsearch.test.search.MockSearchService;
|
import org.elasticsearch.test.search.MockSearchService;
|
||||||
@ -78,7 +79,8 @@ import static com.google.common.collect.Lists.newArrayList;
|
|||||||
*/
|
*/
|
||||||
@Listeners({
|
@Listeners({
|
||||||
ReproduceInfoPrinter.class,
|
ReproduceInfoPrinter.class,
|
||||||
LoggingListener.class
|
LoggingListener.class,
|
||||||
|
AssertionErrorThreadDumpPrinter.class
|
||||||
})
|
})
|
||||||
// remove this entire annotation on upgrade to 5.3!
|
// remove this entire annotation on upgrade to 5.3!
|
||||||
@ThreadLeakFilters(defaultFilters = true, filters = {
|
@ThreadLeakFilters(defaultFilters = true, filters = {
|
||||||
@ -550,44 +552,7 @@ public abstract class ElasticsearchTestCase extends LuceneTestCase {
|
|||||||
protected static final void printStackDump(ESLogger logger) {
|
protected static final void printStackDump(ESLogger logger) {
|
||||||
// print stack traces if we can't create any native thread anymore
|
// print stack traces if we can't create any native thread anymore
|
||||||
Map<Thread, StackTraceElement[]> allStackTraces = Thread.getAllStackTraces();
|
Map<Thread, StackTraceElement[]> allStackTraces = Thread.getAllStackTraces();
|
||||||
logger.error(formatThreadStacks(allStackTraces));
|
logger.error(StackTraces.formatThreadStacks(allStackTraces));
|
||||||
}
|
|
||||||
|
|
||||||
/** Dump threads and their current stack trace. */
|
|
||||||
private static String formatThreadStacks(Map<Thread, StackTraceElement[]> threads) {
|
|
||||||
StringBuilder message = new StringBuilder();
|
|
||||||
int cnt = 1;
|
|
||||||
final Formatter f = new Formatter(message, Locale.ENGLISH);
|
|
||||||
for (Map.Entry<Thread, StackTraceElement[]> e : threads.entrySet()) {
|
|
||||||
if (e.getKey().isAlive()) {
|
|
||||||
f.format(Locale.ENGLISH, "\n %2d) %s", cnt++, threadName(e.getKey())).flush();
|
|
||||||
}
|
|
||||||
if (e.getValue().length == 0) {
|
|
||||||
message.append("\n at (empty stack)");
|
|
||||||
} else {
|
|
||||||
for (StackTraceElement ste : e.getValue()) {
|
|
||||||
message.append("\n at ").append(ste);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return message.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String threadName(Thread t) {
|
|
||||||
return "Thread[" +
|
|
||||||
"id=" + t.getId() +
|
|
||||||
", name=" + t.getName() +
|
|
||||||
", state=" + t.getState() +
|
|
||||||
", group=" + groupName(t.getThreadGroup()) +
|
|
||||||
"]";
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String groupName(ThreadGroup threadGroup) {
|
|
||||||
if (threadGroup == null) {
|
|
||||||
return "{null group}";
|
|
||||||
} else {
|
|
||||||
return threadGroup.getName();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
63
core/src/test/java/org/elasticsearch/test/StackTraces.java
Normal file
63
core/src/test/java/org/elasticsearch/test/StackTraces.java
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
/*
|
||||||
|
* 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 java.util.Formatter;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class StackTraces {
|
||||||
|
/** Dump threads and their current stack trace. */
|
||||||
|
public static String formatThreadStacks(Map<Thread, StackTraceElement[]> threads) {
|
||||||
|
StringBuilder message = new StringBuilder();
|
||||||
|
int cnt = 1;
|
||||||
|
final Formatter f = new Formatter(message, Locale.ENGLISH);
|
||||||
|
for (Map.Entry<Thread, StackTraceElement[]> e : threads.entrySet()) {
|
||||||
|
if (e.getKey().isAlive()) {
|
||||||
|
f.format(Locale.ENGLISH, "\n %2d) %s", cnt++, threadName(e.getKey())).flush();
|
||||||
|
}
|
||||||
|
if (e.getValue().length == 0) {
|
||||||
|
message.append("\n at (empty stack)");
|
||||||
|
} else {
|
||||||
|
for (StackTraceElement ste : e.getValue()) {
|
||||||
|
message.append("\n at ").append(ste);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return message.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String groupName(ThreadGroup threadGroup) {
|
||||||
|
if (threadGroup == null) {
|
||||||
|
return "{null group}";
|
||||||
|
} else {
|
||||||
|
return threadGroup.getName();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String threadName(Thread t) {
|
||||||
|
return "Thread[" +
|
||||||
|
"id=" + t.getId() +
|
||||||
|
", name=" + t.getName() +
|
||||||
|
", state=" + t.getState() +
|
||||||
|
", group=" + groupName(t.getThreadGroup()) +
|
||||||
|
"]";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* 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.junit.listeners;
|
||||||
|
|
||||||
|
import org.elasticsearch.test.StackTraces;
|
||||||
|
import org.junit.runner.notification.Failure;
|
||||||
|
import org.junit.runner.notification.RunListener;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class AssertionErrorThreadDumpPrinter extends RunListener {
|
||||||
|
@Override
|
||||||
|
public void testFailure(Failure failure) throws Exception {
|
||||||
|
if (failure.getException() instanceof AssertionError) {
|
||||||
|
Map<Thread, StackTraceElement[]> allStackTraces = Thread.getAllStackTraces();
|
||||||
|
String threadStacks = StackTraces.formatThreadStacks(allStackTraces);
|
||||||
|
System.err.println(threadStacks);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user