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

Fix RetryRule #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 7 additions & 14 deletions src/test/java/com/yourcompany/TestRules/RetryRule.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
package com.yourcompany.TestRules;


import org.junit.rules.MethodRule;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;

import java.util.concurrent.atomic.AtomicInteger;

/**
* Created by mehmetgerceker on 12/7/15.
*/
public class RetryRule implements TestRule {

private AtomicInteger retryCount;
private final int retryCount;

public RetryRule(int retries){
super();
this.retryCount = new AtomicInteger(retries);
this.retryCount = retries;
}

@Override
Expand All @@ -31,17 +25,16 @@ public void evaluate() throws Throwable {
Throwable caughtThrowable = null;

// implement retry logic here
while (retryCount.getAndDecrement() > 0) {
int i = retryCount;
while (i-- > 0) {
try {
base.evaluate();
return;
} catch (Throwable t) {
if (retryCount.get() > 0 && description.getAnnotation(Retry.class)!= null) {
caughtThrowable = t;
caughtThrowable = t;
if (i > 0 && description.getAnnotation(Retry.class)!= null) {
System.err.println(description.getDisplayName() +
": Failed, " +
retryCount.toString() +
"retries remain");
": Failed, " + i + "retries remain");
} else {
throw caughtThrowable;
Copy link
Contributor

@mehmetg mehmetg Jun 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't adding a "=" to the first comparison fix or pulling out the assignment to caughtThrowable from within the if statement? Just like the refactored code does.

}
Expand Down