Skip to content

Commit

Permalink
feat: log all exceptions caught by RetryRule (#1663)
Browse files Browse the repository at this point in the history
Currently, RetryRule show details only of the last caught exception, but this
one may not be the real reason of the test failure.
This change prints the stack trace of all caught expections when debug level
for RetryRule is at least 'debug', helping the develper to detect the real
cause of the test failure.

Fixes #1527

Co-authored-by: Mikhail Shabarov <[email protected]>
  • Loading branch information
mcollovati and mshabarov authored Aug 21, 2023
1 parent 79432e1 commit d185ae4
Showing 1 changed file with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@
*/
package com.vaadin.testbench;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.internal.AssumptionViolatedException;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A retry rule is used to re-run a test several times in case of a random
Expand Down Expand Up @@ -61,20 +67,33 @@ private Statement statement(final Statement base,
return new Statement() {
@Override
public void evaluate() throws Throwable {
Throwable caughtThrowable = null;
List<Throwable> caughtThrowables = new ArrayList<>();
for (int i = 0; i < maxAttempts; i++) {
try {
base.evaluate();
return;
} catch (AssumptionViolatedException t) {
throw t;
} catch (Throwable t) {
caughtThrowable = t;
caughtThrowables.add(t);
}
}
String testDisplayName = description.getDisplayName();
Logger logger = LoggerFactory.getLogger(RetryRule.class);
if (logger.isDebugEnabled() && caughtThrowables.size() > 1) {
logger.debug("Caught {} exceptions for {} test",
caughtThrowables.size(), testDisplayName);
AtomicInteger attempt = new AtomicInteger();
caughtThrowables
.forEach(t -> logger.debug("\t{} [attempt {}]: {}",
testDisplayName, attempt.incrementAndGet(),
t.getMessage(), t));
}
Throwable lastCaught = caughtThrowables
.get(caughtThrowables.size() - 1);
String errorMessage = String.format("%s: run failed %s times",
description.getDisplayName(), maxAttempts);
throw new RuntimeException(errorMessage, caughtThrowable);
testDisplayName, maxAttempts);
throw new RuntimeException(errorMessage, lastCaught);
}
};
}
Expand Down

0 comments on commit d185ae4

Please sign in to comment.