Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: log all exceptions caught by RetryRule #1663

Merged
merged 2 commits into from
Aug 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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