#8345 adjust test stacktrace size example to work in all cases by acting on clearly defined thread

Fixes #8350
This commit is contained in:
Armin 2017-09-21 09:50:52 +02:00 committed by Armin Braun
parent e248d3a07d
commit 554418178f

View file

@ -1,14 +1,17 @@
package org.logstash.instruments.monitors;
import org.junit.Test;
import org.logstash.instrument.monitors.HotThreadsMonitor;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.logstash.instrument.monitors.HotThreadsMonitor;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
@ -45,17 +48,38 @@ public class HotThreadMonitorTest {
}
@Test
public void testStackTraceSizeOption(){
public void testStackTraceSizeOption() throws InterruptedException {
final String testStackSize = "4";
Map<String, String> options = new HashMap<>();
options.put("stacktrace_size", testStackSize);
HotThreadsMonitor.detect(options).stream().filter(tr -> !tr.getThreadName().equals("Signal Dispatcher") &&
!tr.getThreadName().equals("Reference Handler") &&
!tr.getThreadName().equals("Attach Listener"))
.forEach(tr -> {
List stackTrace = (List)tr.toMap().get("thread.stacktrace");
assertThat(stackTrace.size(), is(Integer.valueOf(testStackSize)));
});
final CountDownLatch latch = new CountDownLatch(1);
final Thread thread = new Thread() {
@Override
public void run() {
waitEnd();
}
void waitEnd() {
try {
latch.await();
} catch (final InterruptedException ex) {
throw new IllegalArgumentException(ex);
}
}
};
try {
thread.start();
TimeUnit.MILLISECONDS.sleep(300L);
final Map<String, String> options = new HashMap<>();
options.put("stacktrace_size", testStackSize);
assertThat(
((List) HotThreadsMonitor.detect(options).stream()
.filter(tr -> thread.getName().equals(tr.getThreadName())).findFirst()
.get().toMap().get("thread.stacktrace")).size(),
is(Integer.parseInt(testStackSize))
);
} finally {
latch.countDown();
thread.join();
}
}
@Test