Skip to content

Commit

Permalink
[integration-test] Create thread dump if (Multi)ResultSyncPoint throw…
Browse files Browse the repository at this point in the history
…s timeout exception
  • Loading branch information
Flowdalic committed Nov 3, 2024
1 parent ebd4709 commit 77819e0
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import org.igniterealtime.smack.inttest.SmackIntegrationTestFramework.ConcreteTest;
import org.igniterealtime.smack.inttest.SmackIntegrationTestFramework.TestRunResult;
import org.igniterealtime.smack.inttest.util.ResultSyncPoint.ResultSyncPointTimeoutException;

public class StandardSinttestDebugger implements SinttestDebugger {

Expand Down Expand Up @@ -237,6 +238,16 @@ public void onTestFailure(ConcreteTest test, ZonedDateTime endTime, Throwable th
if (markerFile != null) {
Files.writeString(markerFile, stacktrace);
}
if (currentTestMethodDirectory != null) {
if (throwable instanceof ResultSyncPointTimeoutException) {
var resultSyncPointTimeoutException = (ResultSyncPointTimeoutException) throwable;
var threadDump = resultSyncPointTimeoutException.getThreadDump();
var threadDumpFile = currentTestMethodDirectory.resolve("thread-dump");
Files.writeString(threadDumpFile, threadDump);

logSink("Wrote thread dump to file://" + threadDumpFile);
}
}

onTestEnd();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;

import org.jivesoftware.smack.util.Objects;

import org.igniterealtime.smack.inttest.util.ResultSyncPoint.ResultSyncPointTimeoutException;

public class MultiResultSyncPoint<R, E extends Exception> {

private final List<R> results;
Expand All @@ -33,11 +34,11 @@ public MultiResultSyncPoint(int expectedResultCount) {
this.results = new ArrayList<>(expectedResultCount);
}

public synchronized List<R> waitForResults(long timeout) throws E, InterruptedException, TimeoutException {
public synchronized List<R> waitForResults(long timeout) throws E, InterruptedException, ResultSyncPointTimeoutException {
return waitForResults(timeout, null);
}

public synchronized List<R> waitForResults(long timeout, String timeoutMessage) throws E, InterruptedException, TimeoutException {
public synchronized List<R> waitForResults(long timeout, String timeoutMessage) throws E, InterruptedException, ResultSyncPointTimeoutException {
long now = System.currentTimeMillis();
final long deadline = now + timeout;
while (results.size() < expectedResultCount && exception == null && now < deadline) {
Expand All @@ -51,7 +52,7 @@ public synchronized List<R> waitForResults(long timeout, String timeoutMessage)
}
sb.append("MultiResultSyncPoint timeout waiting " + timeout + " ms. Got " + results.size() + " results of " + expectedResultCount + " results");

throw new TimeoutException(sb.toString());
throw new ResultSyncPointTimeoutException(sb.toString());
}
if (exception != null) throw exception;
return new ArrayList<>(results);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ public class ResultSyncPoint<R, E extends Exception> {
private R result;
private E exception;

public R waitForResult(long timeout) throws E, InterruptedException, TimeoutException {
public R waitForResult(long timeout) throws E, InterruptedException, ResultSyncPointTimeoutException {
return waitForResult(timeout, null);
}

public R waitForResult(long timeout, String timeoutMessage) throws E, InterruptedException, TimeoutException {
public R waitForResult(long timeout, String timeoutMessage) throws E, InterruptedException, ResultSyncPointTimeoutException {
synchronized (this) {
if (result != null) {
return result;
Expand All @@ -55,7 +55,7 @@ public R waitForResult(long timeout, String timeoutMessage) throws E, Interrupte
if (timeoutMessage != null) {
message += ": " + timeoutMessage;
}
throw new TimeoutException(message);
throw new ResultSyncPointTimeoutException(message);
}


Expand All @@ -72,4 +72,19 @@ public void signal(E exception) {
notifyAll();
}
}

public static class ResultSyncPointTimeoutException extends TimeoutException {

private static final long serialVersionUID = 1L;

private final String threadDump = ThreadDumpUtil.threadDump();

public ResultSyncPointTimeoutException(String message) {
super(message);
}

public String getThreadDump() {
return threadDump;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
*
* Copyright 2024 Florian Schmaus
*
* 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 org.igniterealtime.smack.inttest.util;

import java.io.IOException;
import java.lang.management.ManagementFactory;

public class ThreadDumpUtil {

public static String threadDump() {
var sb = new StringBuilder();
try {
threadDump(sb);
} catch (IOException e) {
throw new AssertionError(e);
}
return sb.toString();
}

public static void threadDump(Appendable appendable) throws IOException {
var bean = ManagementFactory.getThreadMXBean();
var infos = bean.dumpAllThreads(true, true);
for (var info : infos) {
appendable.append(info.toString());
}
}
}

0 comments on commit 77819e0

Please sign in to comment.