ExponentialDelayRetry.java

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 3 1. terminated : negated conditional → RUN_ERROR
2. terminated : changed conditional boundary → RUN_ERROR
3. terminated : replaced boolean return with true for fr/sii/ogham/core/retry/ExponentialDelayRetry::terminated → RUN_ERROR
		return retries <= 0;
58
	}
59
60
	@Override
61
	public Instant nextDate(Instant executionStartTime, Instant executionFailureTime) {
62 1 1. nextDate : Replaced integer subtraction with addition → RUN_ERROR
		retries--;
63
		long delay = (long) Math.scalb(initialDelay, retried);
64 1 1. nextDate : Replaced integer addition with subtraction → RUN_ERROR
		retried++;
65 1 1. nextDate : replaced return value with null for fr/sii/ogham/core/retry/ExponentialDelayRetry::nextDate → RUN_ERROR
		return executionFailureTime.plusMillis(delay);
66
	}
67
68
	public int getRemainingRetries() {
69 1 1. getRemainingRetries : replaced int return with 0 for fr/sii/ogham/core/retry/ExponentialDelayRetry::getRemainingRetries → RUN_ERROR
		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
Location : terminated
Killed by :
negated conditional → RUN_ERROR

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

3.3
Location : terminated
Killed by :
replaced boolean return with true for fr/sii/ogham/core/retry/ExponentialDelayRetry::terminated → RUN_ERROR

62

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

64

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

65

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

69

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

73

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

Active mutators

Tests examined


Report generated by PIT 1.13.1