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

Added try/catch when custom retry analyzers are executed #326

Merged
merged 3 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
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 @@ -37,21 +37,6 @@ private TesterraProperties() {

}

/**
* Failed tests maximum number of retries.
*/
public static final String FAILED_TESTS_MAX_RETRIES = "tt.failed.tests.max.retries";

/**
* Failed tests condition: Throwable Class(~es, devided by ','.
*/
public static final String FAILED_TESTS_IF_THROWABLE_CLASSES = "tt.failed.tests.if.throwable.classes";

/**
* Failed tests condition. Throwable Message(~s, devided by ',').
*/
public static final String FAILED_TESTS_IF_THROWABLE_MESSAGES = "tt.failed.tests.if.throwable.messages";

/**
* WDM close windows rule.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,27 @@

import eu.tsystems.mms.tic.testframework.annotations.NoRetry;
import eu.tsystems.mms.tic.testframework.annotations.Retry;
import eu.tsystems.mms.tic.testframework.common.PropertyManager;
import eu.tsystems.mms.tic.testframework.constants.TesterraProperties;
import eu.tsystems.mms.tic.testframework.common.IProperties;
import eu.tsystems.mms.tic.testframework.common.Testerra;
import eu.tsystems.mms.tic.testframework.events.TestStatusUpdateEvent;
import eu.tsystems.mms.tic.testframework.logging.Loggable;
import eu.tsystems.mms.tic.testframework.report.Status;
import eu.tsystems.mms.tic.testframework.report.TesterraListener;
import eu.tsystems.mms.tic.testframework.report.model.context.AbstractContext;
import eu.tsystems.mms.tic.testframework.report.model.context.MethodContext;
import eu.tsystems.mms.tic.testframework.report.utils.ExecutionContextController;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;
import org.testng.IRetryAnalyzer;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.stream.Stream;

/**
* Testng Retry Analyzer.
Expand All @@ -53,27 +53,42 @@
*/
public class RetryAnalyzer implements IRetryAnalyzer, Loggable {

public enum Properties implements IProperties {
//Failed tests maximum number of retries.
FAILED_TESTS_MAX_RETRIES("tt.failed.tests.max.retries", 1),

// Failed tests condition: Throwable Class(~es, devided by ','.
FAILED_TESTS_IF_THROWABLE_CLASSES("tt.failed.tests.if.throwable.classes", ""),

// Failed tests condition. Throwable Message(~s, devided by ',').
FAILED_TESTS_IF_THROWABLE_MESSAGES("tt.failed.tests.if.throwable.messages", "");

private final String property;
private final Object defaultValue;

Properties(String property, Object defaultValue) {
this.property = property;
this.defaultValue = defaultValue;
}

@Override
public Object getDefault() {
return null;
}
}

private static final Queue<AdditionalRetryAnalyzer> ADDITIONAL_RETRY_ANALYZERS = new ConcurrentLinkedQueue<>();

/**
* Classes list.
*/
private static final List<Class> CLASSES_LIST = new ArrayList<>();

/**
* Messages list.
*/
private static final List<String> MESSAGES_LIST = new ArrayList<>();

private static final Queue<MethodContext> RETRIED_METHODS = new ConcurrentLinkedQueue<>();

/**
* The retry counter.
*/
private static final Map<String, Integer> retryCounters = new ConcurrentHashMap<>();

static {
final String classes = PropertyManager.getProperty(TesterraProperties.FAILED_TESTS_IF_THROWABLE_CLASSES);
final String classes = Properties.FAILED_TESTS_IF_THROWABLE_CLASSES.asString();
if (classes != null) {
String[] split = classes.split(",");
for (String clazz : split) {
Expand All @@ -86,7 +101,7 @@ public class RetryAnalyzer implements IRetryAnalyzer, Loggable {
}
}

final String messages = PropertyManager.getProperty(TesterraProperties.FAILED_TESTS_IF_THROWABLE_MESSAGES);
final String messages = Properties.FAILED_TESTS_IF_THROWABLE_MESSAGES.asString();
if (messages != null) {
String[] split = messages.split(",");
for (String message : split) {
Expand All @@ -103,7 +118,7 @@ public boolean retry(final ITestResult testResult) {
* Announce the test status change
*/
if (methodContext.isStatusOneOf(Status.RETRIED, Status.RECOVERED, Status.FAILED)) {
TesterraListener.getEventBus().post(new TestStatusUpdateEvent(methodContext));
Testerra.getEventBus().post(new TestStatusUpdateEvent(methodContext));
}
return retry;
}
Expand All @@ -124,7 +139,7 @@ private boolean shouldRetry(ITestResult testResult, MethodContext methodContext)
annotatedRetries = optionalRetry.get().maxRetries();
}

int defaultRetries = PropertyManager.getIntProperty(TesterraProperties.FAILED_TESTS_MAX_RETRIES, 1);
int defaultRetries = Properties.FAILED_TESTS_MAX_RETRIES.asLong().intValue();
int maxRetries = Math.max(defaultRetries, annotatedRetries);

final String retryMessageString = "(" + (retryCounter + 1) + "/" + maxRetries + ")";
Expand Down Expand Up @@ -218,11 +233,16 @@ private Throwable checkThrowable(Throwable throwable) {
do {

for (AdditionalRetryAnalyzer additionalRetryAnalyzer : ADDITIONAL_RETRY_ANALYZERS) {
Optional<Throwable> optionalRetryCause = additionalRetryAnalyzer.analyzeThrowable(throwable);
if (optionalRetryCause.isPresent()) {
retryCause = optionalRetryCause.get();
log().info(String.format("Found retry cause: \"%s\"", retryCause.getMessage()));
break;
try {
Optional<Throwable> optionalRetryCause = additionalRetryAnalyzer.analyzeThrowable(throwable);
if (optionalRetryCause.isPresent()) {
retryCause = optionalRetryCause.get();
log().info(String.format("Found retry cause: \"%s\"", retryCause.getMessage()));
break;
}
} catch (Exception e) {
log().warn("Exception while executing {}", additionalRetryAnalyzer.getClass(), e);

}
}

Expand Down Expand Up @@ -283,6 +303,7 @@ private static Stream<MethodContext> readRetriedMethodsForMethod(MethodContext m

/**
* Tells the RetryAnalyzer that a method has been passed
*
* @param methodContext
*/
public static void methodHasBeenPassed(MethodContext methodContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
import eu.tsystems.mms.tic.testframework.events.TestStatusUpdateEvent;
import eu.tsystems.mms.tic.testframework.internal.MethodRelations;
import eu.tsystems.mms.tic.testframework.logging.Loggable;
import eu.tsystems.mms.tic.testframework.report.model.context.ExecutionContext;
import eu.tsystems.mms.tic.testframework.report.model.context.MethodContext;
import eu.tsystems.mms.tic.testframework.report.model.context.RunConfig;
import eu.tsystems.mms.tic.testframework.report.utils.IExecutionContextController;
import org.testng.ITestResult;
import org.testng.SkipException;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.Map;
Expand Down Expand Up @@ -105,6 +105,7 @@ private void writeCounterToLog() {
String logMessage = runConfig.getReportName() + " " + runConfig.RUNCFG + ": " + getCounterInfoMessage();
log().info(logMessage);
}

@Override
public int getTestsFailed() {
return statusCounter.get(Status.FAILED);
Expand Down Expand Up @@ -140,7 +141,7 @@ public int getTestsSkipped() {
public void onTestStatusUpdate(TestStatusUpdateEvent event) {
MethodContext methodContext = event.getMethodContext();
finalizeMethod(methodContext);
TesterraListener.getEventBus().post(new ContextUpdateEvent().setContext(methodContext));
Testerra.getEventBus().post(new ContextUpdateEvent().setContext(methodContext));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ public void onTestSkipped(ITestResult testResult) {
if (!testResult.wasRetried() && !dataProviderSemaphore.containsKey(testResult.getMethod())) {
MethodContext methodContext = ExecutionContextController.getMethodContextFromTestResult(testResult);
methodContext.setStatus(Status.SKIPPED);
TesterraListener.getEventBus().post(new TestStatusUpdateEvent(methodContext));
Testerra.getEventBus().post(new TestStatusUpdateEvent(methodContext));
}
}

Expand Down