-
Notifications
You must be signed in to change notification settings - Fork 338
Expand file tree
/
Copy pathRetryUntilSuccessful.java
More file actions
79 lines (65 loc) · 2.28 KB
/
RetryUntilSuccessful.java
File metadata and controls
79 lines (65 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package datadog.trace.civisibility.execution;
import datadog.trace.api.civisibility.execution.TestExecutionPolicy;
import datadog.trace.api.civisibility.execution.TestStatus;
import datadog.trace.api.civisibility.telemetry.tag.RetryReason;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.Nullable;
/** Retries a test case if it failed, up to a maximum number of times. */
public class RetryUntilSuccessful implements TestExecutionPolicy {
private final int maxExecutions;
private final boolean suppressFailures;
private int executions;
private boolean successfulExecutionSeen;
/** Total retry counter that is shared by all retry until successful policies (currently ATR) */
private final AtomicInteger totalRetryCount;
public RetryUntilSuccessful(
int maxExecutions, boolean suppressFailures, AtomicInteger totalRetryCount) {
this.maxExecutions = maxExecutions;
this.suppressFailures = suppressFailures;
this.totalRetryCount = totalRetryCount;
this.executions = 0;
}
@Override
public void registerExecution(TestStatus status, long durationMillis) {
++executions;
successfulExecutionSeen |= (status != TestStatus.fail);
if (executions > 1) {
totalRetryCount.incrementAndGet();
}
}
@Override
public boolean wasLastExecution() {
return successfulExecutionSeen || executions == maxExecutions;
}
private boolean currentExecutionIsLast() {
return executions == maxExecutions - 1;
}
@Override
public boolean applicable() {
// executions must always be registered, therefore consider it applicable as long as there are
// retries left
return !wasLastExecution();
}
@Override
public boolean suppressFailures() {
// do not suppress failures for last execution
// (unless flag to suppress all failures is set)
return !currentExecutionIsLast() || suppressFailures;
}
@Nullable
@Override
public RetryReason currentExecutionRetryReason() {
return currentExecutionIsRetry() ? RetryReason.atr : null;
}
private boolean currentExecutionIsRetry() {
return executions > 0;
}
@Override
public boolean hasFailedAllRetries() {
return wasLastExecution() && !successfulExecutionSeen;
}
@Override
public boolean hasSucceededAllRetries() {
return false;
}
}