From dc1dc89bf036d64152e80521eef7b6f3bf2d1b3d Mon Sep 17 00:00:00 2001 From: "Daniel.Alshut" Date: Fri, 14 Apr 2023 14:34:12 +0200 Subject: [PATCH] Add retry limit functionality - 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 https://github.com/mkutz/Shakespeare/issues/234 --- .../java/org/shakespeareframework/Actor.java | 14 +++++ .../org/shakespeareframework/Retryable.java | 61 ++++++++++++++----- 2 files changed, 59 insertions(+), 16 deletions(-) diff --git a/modules/core/src/main/java/org/shakespeareframework/Actor.java b/modules/core/src/main/java/org/shakespeareframework/Actor.java index 0b222496..f11b97e9 100644 --- a/modules/core/src/main/java/org/shakespeareframework/Actor.java +++ b/modules/core/src/main/java/org/shakespeareframework/Actor.java @@ -87,6 +87,11 @@ public Actor does(RetryableTask task) { while (true) { try { + if (task.isNotSurpassingRetryLimit()){ + reporters.forEach(reporter -> reporter.success(this, task)); + return this; + } + task.performAs(this); reporters.forEach(reporter -> reporter.success(this, task)); return this; @@ -156,6 +161,15 @@ public A checks(RetryableQuestion question) { lastAnswer = answer; lastException = null; + if(question.isNotSurpassingRetryLimit()){ + reporters.forEach(reporter -> reporter.success(this, question, answer)); + return answer; + } + if(question.maxRetries != null && question.retryNumber > question.maxRetries){ + reporters.forEach(reporter -> reporter.success(this, question, answer)); + return answer; + } + if (question.acceptable(lastAnswer)) { reporters.forEach(reporter -> reporter.success(this, question, answer)); return answer; diff --git a/modules/core/src/main/java/org/shakespeareframework/Retryable.java b/modules/core/src/main/java/org/shakespeareframework/Retryable.java index 0e8e542f..14719120 100644 --- a/modules/core/src/main/java/org/shakespeareframework/Retryable.java +++ b/modules/core/src/main/java/org/shakespeareframework/Retryable.java @@ -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 isNotSurpassingRetryLimit() { + if (retryLimit == null) return true + if (retryNumber > retryLimit) return false + retryNumber++; + return true; + } }