| 1 | package fr.sii.ogham.core.retry; | |
| 2 | ||
| 3 | import java.time.Instant; | |
| 4 | ||
| 5 | /** | |
| 6 | * Retry several times with an initial delay to wait after the last execution | |
| 7 | * failure. The following delays are doubled until the maximum attempts is | |
| 8 | * reached. | |
| 9 | * | |
| 10 | * If maximum attempts are set to five, the initial delay is set to 500ms and | |
| 11 | * the action (named "connect" for the example) takes 100ms to execute before | |
| 12 | * failing, it will result in: | |
| 13 | * | |
| 14 | * <ul> | |
| 15 | * <li>0: connect</li> | |
| 16 | * <li>100: timeout</li> | |
| 17 | * <li>600: connect</li> | |
| 18 | * <li>700: timeout</li> | |
| 19 | * <li>1700: connect</li> | |
| 20 | * <li>1800: timeout</li> | |
| 21 | * <li>3800: connect</li> | |
| 22 | * <li>3900: timeout</li> | |
| 23 | * <li>7900: connect</li> | |
| 24 | * <li>8000: timeout</li> | |
| 25 | * <li>16000: connect</li> | |
| 26 | * <li>16100: timeout</li> | |
| 27 | * <li>fail</li> | |
| 28 | * </ul> | |
| 29 | * | |
| 30 | * @author Aurélien Baudet | |
| 31 | * | |
| 32 | */ | |
| 33 | public class ExponentialDelayRetry implements RetryStrategy { | |
| 34 | private final int maxRetries; | |
| 35 | private final long initialDelay; | |
| 36 | private int retries; | |
| 37 | private int retried; | |
| 38 | ||
| 39 | /** | |
| 40 | * Initializes with the maximum attempts and the initial delay to wait after | |
| 41 | * a failure. | |
| 42 | * | |
| 43 | * @param maxRetries | |
| 44 | * the maximum attempts | |
| 45 | * @param initialDelay | |
| 46 | * the initial delay that will be doubled for each attempt | |
| 47 | */ | |
| 48 | public ExponentialDelayRetry(int maxRetries, long initialDelay) { | |
| 49 | super(); | |
| 50 | this.maxRetries = maxRetries; | |
| 51 | this.initialDelay = initialDelay; | |
| 52 | retries = maxRetries; | |
| 53 | } | |
| 54 | ||
| 55 | @Override | |
| 56 | public boolean terminated() { | |
| 57 |
6
1. terminated : replaced boolean return with true for fr/sii/ogham/core/retry/ExponentialDelayRetry::terminated → NO_COVERAGE 2. terminated : changed conditional boundary → NO_COVERAGE 3. terminated : negated conditional → NO_COVERAGE 4. terminated : replaced boolean return with true for fr/sii/ogham/core/retry/ExponentialDelayRetry::terminated → KILLED 5. terminated : changed conditional boundary → KILLED 6. terminated : negated conditional → KILLED |
return retries <= 0; |
| 58 | } | |
| 59 | ||
| 60 | @Override | |
| 61 | public Instant nextDate(Instant executionStartTime, Instant executionFailureTime) { | |
| 62 |
2
1. nextDate : Replaced integer subtraction with addition → NO_COVERAGE 2. nextDate : Replaced integer subtraction with addition → KILLED |
retries--; |
| 63 | long delay = (long) Math.scalb(initialDelay, retried); | |
| 64 |
2
1. nextDate : Replaced integer addition with subtraction → NO_COVERAGE 2. nextDate : Replaced integer addition with subtraction → KILLED |
retried++; |
| 65 |
2
1. nextDate : replaced return value with null for fr/sii/ogham/core/retry/ExponentialDelayRetry::nextDate → NO_COVERAGE 2. nextDate : replaced return value with null for fr/sii/ogham/core/retry/ExponentialDelayRetry::nextDate → KILLED |
return executionFailureTime.plusMillis(delay); |
| 66 | } | |
| 67 | ||
| 68 | public int getRemainingRetries() { | |
| 69 |
2
1. getRemainingRetries : replaced int return with 0 for fr/sii/ogham/core/retry/ExponentialDelayRetry::getRemainingRetries → NO_COVERAGE 2. getRemainingRetries : replaced int return with 0 for fr/sii/ogham/core/retry/ExponentialDelayRetry::getRemainingRetries → KILLED |
return retries; |
| 70 | } | |
| 71 | ||
| 72 | public int getMaxRetries() { | |
| 73 |
1
1. getMaxRetries : replaced int return with 0 for fr/sii/ogham/core/retry/ExponentialDelayRetry::getMaxRetries → NO_COVERAGE |
return maxRetries; |
| 74 | } | |
| 75 | } | |
Mutations | ||
| 57 |
1.1 2.2 3.3 4.4 5.5 6.6 |
|
| 62 |
1.1 2.2 |
|
| 64 |
1.1 2.2 |
|
| 65 |
1.1 2.2 |
|
| 69 |
1.1 2.2 |
|
| 73 |
1.1 |