Skip to content

Commit 3caccb3

Browse files
committed
Simplify and fix retry extension and analyzer
1 parent af6446e commit 3caccb3

File tree

2 files changed

+30
-33
lines changed

2 files changed

+30
-33
lines changed

junit-vs-testng/src/test/java/io/github/bonigarcia/junit/selenium/RetryExtension.java

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
package io.github.bonigarcia.junit.selenium;
1818

19-
import java.util.concurrent.atomic.AtomicInteger;
19+
import java.lang.reflect.Method;
2020

2121
import org.junit.jupiter.api.extension.ExtensionContext;
2222
import org.junit.jupiter.api.extension.TestExecutionExceptionHandler;
@@ -25,43 +25,42 @@ public class RetryExtension implements TestExecutionExceptionHandler {
2525

2626
static final int DEFAULT_MAX_RETRIES = 3;
2727

28-
final AtomicInteger retryCount = new AtomicInteger(1);
29-
final AtomicInteger maxRetries = new AtomicInteger(DEFAULT_MAX_RETRIES);
28+
private final int maxRetries;
3029

3130
public RetryExtension() {
32-
// Default constructor
31+
this.maxRetries = DEFAULT_MAX_RETRIES;
3332
}
3433

3534
public RetryExtension(int maxRetries) {
36-
this.maxRetries.set(maxRetries);
35+
this.maxRetries = maxRetries;
3736
}
3837

3938
@Override
40-
public void handleTestExecutionException(ExtensionContext extensionContext,
41-
Throwable throwable) throws Throwable {
42-
logError(throwable);
39+
public void handleTestExecutionException(ExtensionContext context,
40+
Throwable firstFailure) throws Throwable {
41+
logError(firstFailure, 1); // attempt #1 already failed
4342

44-
extensionContext.getTestMethod().ifPresent(method -> {
45-
while (retryCount.incrementAndGet() <= maxRetries.get()) {
46-
try {
47-
extensionContext.getExecutableInvoker().invoke(method,
48-
extensionContext.getRequiredTestInstance());
49-
return;
50-
} catch (Throwable t) {
51-
logError(t);
52-
if (retryCount.get() >= maxRetries.get()) {
53-
throw t;
54-
}
43+
Method method = context.getRequiredTestMethod();
44+
Object instance = context.getRequiredTestInstance();
45+
int attempt = 1;
46+
while (attempt < maxRetries) {
47+
attempt++;
48+
try {
49+
// Re-run test method (no @BeforeEach/@AfterEach)
50+
context.getExecutableInvoker().invoke(method, instance);
51+
return; // Success: swallow the original exception
52+
} catch (Throwable t) {
53+
logError(t, attempt);
54+
if (attempt >= maxRetries) {
55+
throw t; // failure after last retry
5556
}
5657
}
57-
});
58-
throw throwable;
58+
}
59+
throw firstFailure; // throw original failure as a fallback
5960
}
6061

61-
private void logError(Throwable e) {
62-
System.err.println("Attempt test execution #" + retryCount.get()
63-
+ " failed (" + e.getClass().getName() + "thrown): "
64-
+ e.getMessage());
62+
private void logError(Throwable e, int attempt) {
63+
System.err.println("Attempt #" + attempt + " failed ("
64+
+ e.getClass().getName() + " thrown): " + e.getMessage());
6565
}
66-
6766
}

junit-vs-testng/src/test/java/io/github/bonigarcia/testng/selenium/RetryAnalyzer.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package io.github.bonigarcia.testng.selenium;
1818

1919
import java.lang.reflect.Method;
20-
import java.util.concurrent.atomic.AtomicInteger;
2120

2221
import org.testng.IRetryAnalyzer;
2322
import org.testng.ITestResult;
@@ -26,7 +25,7 @@ public class RetryAnalyzer implements IRetryAnalyzer {
2625

2726
static final int DEFAULT_MAX_RETRIES = 3;
2827

29-
final AtomicInteger retryCount = new AtomicInteger(1);
28+
private int retryCount = 1;
3029

3130
@Override
3231
public boolean retry(ITestResult result) {
@@ -36,18 +35,17 @@ public boolean retry(ITestResult result) {
3635
Retry retry = method.getAnnotation(Retry.class);
3736
maxRetries = retry.value();
3837
}
39-
if (retryCount.get() <= maxRetries) {
38+
if (retryCount <= maxRetries) {
4039
logError(result.getThrowable());
41-
retryCount.incrementAndGet();
40+
retryCount++;
4241
return true;
4342
}
4443
return false;
4544
}
4645

4746
private void logError(Throwable e) {
48-
System.err.println("Attempt test execution #" + retryCount.get()
49-
+ " failed (" + e.getClass().getName() + "thrown): "
50-
+ e.getMessage());
47+
System.err.println("Attempt test execution #" + retryCount + " failed ("
48+
+ e.getClass().getName() + "thrown): " + e.getMessage());
5149
}
5250

5351
}

0 commit comments

Comments
 (0)