Skip to content

Commit

Permalink
Add retry limit functionality
Browse files Browse the repository at this point in the history
- added variables retryLimit and retryNumber to Retryable.java
- added function isNotSurpassingRetryLimit() to Retryable.java
- added retry limit check to actor.does() and actor.checks() for RetryableTask & Retryable.Question

#234
  • Loading branch information
rd-daniel-alshut committed Apr 27, 2023
1 parent f10d948 commit 0d3aff5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 16 deletions.
10 changes: 10 additions & 0 deletions modules/core/src/main/java/org/shakespeareframework/Actor.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ public Actor does(RetryableTask task) {

while (true) {
try {
if (task.surpassesRetryLimit()){
reporters.forEach(reporter -> reporter.success(this, task));
return this;
}

task.performAs(this);
reporters.forEach(reporter -> reporter.success(this, task));
return this;
Expand Down Expand Up @@ -156,6 +161,11 @@ public <A> A checks(RetryableQuestion<A> question) {
lastAnswer = answer;
lastException = null;

if(question.surpassesRetryLimit()){
reporters.forEach(reporter -> reporter.success(this, question, answer));
return answer;
}

if (question.acceptable(lastAnswer)) {
reporters.forEach(reporter -> reporter.success(this, question, answer));
return answer;
Expand Down
61 changes: 45 additions & 16 deletions modules/core/src/main/java/org/shakespeareframework/Retryable.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,49 @@
*/
public interface Retryable {

/** The default timeout */
Duration DEFAULT_TIMEOUT = Duration.ofSeconds(2);

/**
* @return the timeout for this (default: {@link #DEFAULT_TIMEOUT})
*/
default Duration getTimeout() {
return DEFAULT_TIMEOUT;
}

/**
* @return the interval to retry this (default {@link #getTimeout()} / 10)
*/
default Duration getInterval() {
return DEFAULT_TIMEOUT.dividedBy(10);
}
/**
* The default timeout
*/
Duration DEFAULT_TIMEOUT = Duration.ofSeconds(2);
Integer retryLimit = null;
Integer retryNumber = 0;

/**
* @return the timeout for this (default: {@link #DEFAULT_TIMEOUT})
*/
default Duration getTimeout() {
return DEFAULT_TIMEOUT;
}

/**
* @return the interval to retry this (default {@link #getTimeout()} / 10)
*/
default Duration getInterval() {
return DEFAULT_TIMEOUT.dividedBy(10);
}

/**
* @return the maximum amount of allowed retries (default {@link #retryLimit})
*/
default Integer getMaxRetries() {
return retryLimit;
}

/**
* @return the number of the current retry (default {@link #retryNumber})
*/
default Integer getRetryNumber() {
return retryNumber;
}

/**
* @return false if a retry limit is set and the current retry number surpasses given limit (default {@link true})
* function call increases retry number by one
*/
default boolean surpassesRetryLimit() {
if (retryLimit == null) return false;
if (retryNumber > retryLimit) return true;
retryNumber++;
return false;
}
}

0 comments on commit 0d3aff5

Please sign in to comment.