FixedIntervalRetry.java

1
package fr.sii.ogham.core.retry;
2
3
import java.time.Instant;
4
5
/**
6
 * Retry several times with a fixed delay between each try until the maximum
7
 * attempts is reached. The next execution is based on the execution start date.
8
 * 
9
 * If delay is 500ms and max retries is 5, it means that a retry will be
10
 * attempted every 500ms until 5 attempts are reached (inclusive). For example,
11
 * you want to connect to an external system at t1=0 and the connection timeout
12
 * (100ms) is triggered at t2=100ms. Using this retry will provide the following
13
 * behavior:
14
 * 
15
 * <ul>
16
 * <li>0: connect</li>
17
 * <li>100: timeout</li>
18
 * <li>500: connect</li>
19
 * <li>600: timeout</li>
20
 * <li>1000: connect</li>
21
 * <li>1100: timeout</li>
22
 * <li>1500: connect</li>
23
 * <li>1600: timeout</li>
24
 * <li>2000: connect</li>
25
 * <li>2100: timeout</li>
26
 * <li>2500: connect</li>
27
 * <li>2600: timeout</li>
28
 * <li>fail</li>
29
 * </ul>
30
 * 
31
 * 
32
 * <strong>NOTE:</strong> The provided date doesn't take the duration of the
33
 * execution in account. If an execution takes 1s to execute while retry delay
34
 * is set to 500ms, there may have several executions in parallel. However, this
35
 * totally depends on the {@link RetryExecutor} implementation. For example
36
 * {@link SimpleRetryExecutor} won't run several executions in parallel. In this
37
 * case, it will execute the action as soon as the previous one has failed
38
 * therefore the delay may not be complied.
39
 * 
40
 * @author Aurélien Baudet
41
 *
42
 */
43
public class FixedIntervalRetry implements RetryStrategy {
44
	private final int maxRetries;
45
	private final long interval;
46
	private Instant firstExecutionTime;
47
	private int retries;
48
	private int remainingRetries;
49
50
	/**
51
	 * Initializes with the maximum attempts and the delay between each attempt.
52
	 * 
53
	 * @param maxRetries
54
	 *            the maximum attempts
55
	 * @param interval
56
	 *            the interval between attempts
57
	 */
58
	public FixedIntervalRetry(int maxRetries, long interval) {
59
		super();
60
		this.maxRetries = maxRetries;
61
		this.interval = interval;
62
		remainingRetries = maxRetries;
63
	}
64
65
	@Override
66
	public boolean terminated() {
67 6 1. terminated : replaced boolean return with true for fr/sii/ogham/core/retry/FixedIntervalRetry::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/FixedIntervalRetry::terminated → KILLED
5. terminated : changed conditional boundary → KILLED
6. terminated : negated conditional → KILLED
		return remainingRetries <= 0;
68
	}
69
70
	@Override
71
	public Instant nextDate(Instant executionStartTime, Instant executionFailureTime) {
72 2 1. nextDate : Replaced integer subtraction with addition → NO_COVERAGE
2. nextDate : Replaced integer subtraction with addition → KILLED
		remainingRetries--;
73 2 1. nextDate : Replaced integer addition with subtraction → NO_COVERAGE
2. nextDate : Replaced integer addition with subtraction → KILLED
		retries++;
74 2 1. nextDate : negated conditional → NO_COVERAGE
2. nextDate : negated conditional → KILLED
		if (firstExecutionTime == null) {
75
			firstExecutionTime = executionStartTime;
76
		}
77 4 1. nextDate : Replaced long multiplication with division → NO_COVERAGE
2. nextDate : replaced return value with null for fr/sii/ogham/core/retry/FixedIntervalRetry::nextDate → NO_COVERAGE
3. nextDate : Replaced long multiplication with division → KILLED
4. nextDate : replaced return value with null for fr/sii/ogham/core/retry/FixedIntervalRetry::nextDate → KILLED
		return firstExecutionTime.plusMillis(interval * retries);
78
	}
79
80
	public int getRemainingRetries() {
81 2 1. getRemainingRetries : replaced int return with 0 for fr/sii/ogham/core/retry/FixedIntervalRetry::getRemainingRetries → NO_COVERAGE
2. getRemainingRetries : replaced int return with 0 for fr/sii/ogham/core/retry/FixedIntervalRetry::getRemainingRetries → KILLED
		return remainingRetries;
82
	}
83
84
	public int getMaxRetries() {
85 1 1. getMaxRetries : replaced int return with 0 for fr/sii/ogham/core/retry/FixedIntervalRetry::getMaxRetries → NO_COVERAGE
		return maxRetries;
86
	}
87
88
}

Mutations

67

1.1
Location : terminated
Killed by : oghamcore.ut.core.retry.FixedIntervalRetryExecutorTest.callingTerminatedSeveralTimesShouldNotDecrementRemainingRetries(oghamcore.ut.core.retry.FixedIntervalRetryExecutorTest)
replaced boolean return with true for fr/sii/ogham/core/retry/FixedIntervalRetry::terminated → KILLED

2.2
Location : terminated
Killed by : none
replaced boolean return with true for fr/sii/ogham/core/retry/FixedIntervalRetry::terminated → NO_COVERAGE

3.3
Location : terminated
Killed by : oghamcore.ut.core.retry.FixedIntervalRetryExecutorTest.reachMaximumAttempts(oghamcore.ut.core.retry.FixedIntervalRetryExecutorTest)
changed conditional boundary → KILLED

4.4
Location : terminated
Killed by : none
changed conditional boundary → NO_COVERAGE

5.5
Location : terminated
Killed by : oghamcore.ut.core.retry.FixedIntervalRetryExecutorTest.callingTerminatedSeveralTimesShouldNotDecrementRemainingRetries(oghamcore.ut.core.retry.FixedIntervalRetryExecutorTest)
negated conditional → KILLED

6.6
Location : terminated
Killed by : none
negated conditional → NO_COVERAGE

72

1.1
Location : nextDate
Killed by : none
Replaced integer subtraction with addition → NO_COVERAGE

2.2
Location : nextDate
Killed by : oghamcore.ut.core.retry.FixedIntervalRetryExecutorTest.reachMaximumAttempts(oghamcore.ut.core.retry.FixedIntervalRetryExecutorTest)
Replaced integer subtraction with addition → KILLED

73

1.1
Location : nextDate
Killed by : none
Replaced integer addition with subtraction → NO_COVERAGE

2.2
Location : nextDate
Killed by : oghamcore.ut.core.retry.FixedIntervalRetryExecutorTest.reachMaximumAttempts(oghamcore.ut.core.retry.FixedIntervalRetryExecutorTest)
Replaced integer addition with subtraction → KILLED

74

1.1
Location : nextDate
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : nextDate
Killed by : oghamcore.ut.core.retry.FixedIntervalRetryExecutorTest.reachMaximumAttempts(oghamcore.ut.core.retry.FixedIntervalRetryExecutorTest)
negated conditional → KILLED

77

1.1
Location : nextDate
Killed by : oghamcore.ut.core.retry.FixedIntervalRetryExecutorTest.reachMaximumAttempts(oghamcore.ut.core.retry.FixedIntervalRetryExecutorTest)
Replaced long multiplication with division → KILLED

2.2
Location : nextDate
Killed by : none
Replaced long multiplication with division → NO_COVERAGE

3.3
Location : nextDate
Killed by : oghamcore.ut.core.retry.FixedIntervalRetryExecutorTest.reachMaximumAttempts(oghamcore.ut.core.retry.FixedIntervalRetryExecutorTest)
replaced return value with null for fr/sii/ogham/core/retry/FixedIntervalRetry::nextDate → KILLED

4.4
Location : nextDate
Killed by : none
replaced return value with null for fr/sii/ogham/core/retry/FixedIntervalRetry::nextDate → NO_COVERAGE

81

1.1
Location : getRemainingRetries
Killed by : none
replaced int return with 0 for fr/sii/ogham/core/retry/FixedIntervalRetry::getRemainingRetries → NO_COVERAGE

2.2
Location : getRemainingRetries
Killed by : oghamcore.ut.core.retry.FixedIntervalRetryExecutorTest.callingTerminatedSeveralTimesShouldNotDecrementRemainingRetries(oghamcore.ut.core.retry.FixedIntervalRetryExecutorTest)
replaced int return with 0 for fr/sii/ogham/core/retry/FixedIntervalRetry::getRemainingRetries → KILLED

85

1.1
Location : getMaxRetries
Killed by : none
replaced int return with 0 for fr/sii/ogham/core/retry/FixedIntervalRetry::getMaxRetries → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT OGHAM