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 3 1. terminated : replaced boolean return with true for fr/sii/ogham/core/retry/FixedIntervalRetry::terminated → RUN_ERROR
2. terminated : changed conditional boundary → RUN_ERROR
3. terminated : negated conditional → RUN_ERROR
		return remainingRetries <= 0;
68
	}
69
70
	@Override
71
	public Instant nextDate(Instant executionStartTime, Instant executionFailureTime) {
72 1 1. nextDate : Replaced integer subtraction with addition → RUN_ERROR
		remainingRetries--;
73 1 1. nextDate : Replaced integer addition with subtraction → RUN_ERROR
		retries++;
74 1 1. nextDate : negated conditional → RUN_ERROR
		if (firstExecutionTime == null) {
75
			firstExecutionTime = executionStartTime;
76
		}
77 2 1. nextDate : Replaced long multiplication with division → RUN_ERROR
2. nextDate : replaced return value with null for fr/sii/ogham/core/retry/FixedIntervalRetry::nextDate → RUN_ERROR
		return firstExecutionTime.plusMillis(interval * retries);
78
	}
79
80
	public int getRemainingRetries() {
81 1 1. getRemainingRetries : replaced int return with 0 for fr/sii/ogham/core/retry/FixedIntervalRetry::getRemainingRetries → RUN_ERROR
		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 :
replaced boolean return with true for fr/sii/ogham/core/retry/FixedIntervalRetry::terminated → RUN_ERROR

2.2
Location : terminated
Killed by :
changed conditional boundary → RUN_ERROR

3.3
Location : terminated
Killed by :
negated conditional → RUN_ERROR

72

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

73

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

74

1.1
Location : nextDate
Killed by :
negated conditional → RUN_ERROR

77

1.1
Location : nextDate
Killed by :
Replaced long multiplication with division → RUN_ERROR

2.2
Location : nextDate
Killed by :
replaced return value with null for fr/sii/ogham/core/retry/FixedIntervalRetry::nextDate → RUN_ERROR

81

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

85

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

Active mutators

Tests examined


Report generated by PIT 1.13.1