Skip to content

Commit a0ef4bd

Browse files
author
Mike Reiche
committed
Prevent multiple contexts
1 parent a315e0e commit a0ef4bd

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

core/src/main/java/eu/tsystems/mms/tic/testframework/execution/testng/RetryAnalyzer.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,7 @@ private boolean shouldRetry(ITestResult testResult, MethodContext methodContext)
135135
return false;
136136
}
137137

138-
boolean retry = false;
139138
final String testMethodName = methodContext.getName();
140-
String retryReason = null;
141139

142140
/*
143141
check retry counter
@@ -174,13 +172,6 @@ private boolean shouldRetry(ITestResult testResult, MethodContext methodContext)
174172

175173
boolean containingFilteredThrowable = isTestResultContainingFilteredThrowable(testResult);
176174
if (containingFilteredThrowable) {
177-
retry = true;
178-
}
179-
180-
/*
181-
* process retry
182-
*/
183-
if (retry) {
184175
methodHasBeenRetried(methodContext);
185176
RETRIED_METHODS.add(methodContext);
186177

core/src/main/java/eu/tsystems/mms/tic/testframework/execution/testng/worker/start/MethodStartWorker.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ private void addRetryAnalyzer(MethodStartEvent event) {
5757
Method method = event.getMethod();
5858
if (retryAnalyzer == null || retryAnalyzer instanceof DisabledRetryAnalyzer) {
5959
testNGMethod.setRetryAnalyzerClass(RetryAnalyzer.class);
60-
} else {
61-
log().info("Using a non-default retry analyzer: " + retryAnalyzer + " on " + method.getName());
60+
} else if (!(retryAnalyzer instanceof RetryAnalyzer)){
61+
log().info("Using a non-default retry analyzer: " + retryAnalyzer.getClass().getSimpleName() + " on " + method.getName());
6262
}
6363
}
6464
}

core/src/main/java/eu/tsystems/mms/tic/testframework/report/TesterraListener.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import eu.tsystems.mms.tic.testframework.report.utils.ExecutionContextController;
5252
import java.util.Date;
5353
import java.util.Locale;
54+
import java.util.concurrent.ConcurrentHashMap;
5455
import org.apache.logging.log4j.Level;
5556
import org.apache.logging.log4j.core.LoggerContext;
5657
import org.apache.logging.log4j.core.config.Configurator;
@@ -75,9 +76,7 @@
7576
import org.testng.internal.InvokedMethod;
7677
import org.testng.internal.TestResult;
7778
import org.testng.xml.XmlSuite;
78-
7979
import java.util.List;
80-
import java.util.Locale;
8180

8281
/**
8382
* Listener for JUnit and TestNg, collects test informations for testreport.
@@ -117,6 +116,7 @@ public class TesterraListener implements
117116
private static final Report report;
118117
private static DefaultTestNGContextGenerator contextGenerator;
119118
private static final TestStatusController testStatusController = new TestStatusController();
119+
private static final ConcurrentHashMap<ITestNGMethod, Boolean> dataProviderSemaphore = new ConcurrentHashMap<>();
120120

121121
static {
122122
String logLevel = PropertyManager.getProperty("log4j.level");
@@ -524,14 +524,22 @@ public static boolean isActive() {
524524

525525
@Override
526526
public void onDataProviderFailure(ITestNGMethod testNGMethod, ITestContext testContext, RuntimeException exception) {
527-
TestResult testResult = TestResult.newContextAwareTestResult(testNGMethod, testContext);
528-
InvokedMethod invokedMethod = new InvokedMethod(new Date().getTime(), testResult);
529-
MethodContext methodContext = pBeforeInvocation(invokedMethod, testResult, testContext);
530-
if (exception.getCause() != null) {
531-
methodContext.addError(exception.getCause());
532-
} else {
533-
methodContext.addError(exception);
527+
/**
528+
* TestNG calls the data provider initialization for every thread.
529+
* Added a semaphore to prevent adding multiple method contexts.
530+
*/
531+
if (!dataProviderSemaphore.containsKey(testNGMethod)) {
532+
TestResult testResult = TestResult.newContextAwareTestResult(testNGMethod, testContext);
533+
InvokedMethod invokedMethod = new InvokedMethod(new Date().getTime(), testResult);
534+
MethodContext methodContext = pBeforeInvocation(invokedMethod, testResult, testContext);
535+
if (exception.getCause() != null) {
536+
methodContext.addError(exception.getCause());
537+
} else {
538+
methodContext.addError(exception);
539+
}
540+
pAfterInvocation(invokedMethod, testResult, testContext);
541+
542+
dataProviderSemaphore.put(testNGMethod, true);
534543
}
535-
pAfterInvocation(invokedMethod, testResult, testContext);
536544
}
537545
}

0 commit comments

Comments
 (0)