forked from mockito/mockito
/
VerificationOverTimeImpl.java
140 lines (122 loc) · 5.8 KB
/
VerificationOverTimeImpl.java
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.verification;
import java.time.Duration;
import org.mockito.exceptions.base.MockitoAssertionError;
import org.mockito.internal.util.Timer;
import org.mockito.internal.verification.api.VerificationData;
import org.mockito.verification.VerificationMode;
/**
* Verifies that another verification mode (the delegate) is satisfied within a certain timeframe
* (before timeout has passed, measured from the call to verify()), and either returns immediately
* once it does, or waits until it is definitely satisfied once the full time has passed.
*/
public class VerificationOverTimeImpl implements VerificationMode {
private final Duration pollingPeriod;
private final VerificationMode delegate;
private final boolean returnOnSuccess;
private final Timer timer;
/**
* Create this verification mode, to be used to verify invocation ongoing data later.
*
* @param pollingPeriod The frequency to poll delegate.verify(), to check whether the delegate has been satisfied
* @param duration The max time to wait for the delegate verification mode to be satisfied
* @param delegate The verification mode to delegate overall success or failure to
* @param returnOnSuccess Whether to immediately return successfully once the delegate is satisfied (as in
* {@link org.mockito.verification.VerificationWithTimeout}, or to only return once
* the delegate is satisfied and the full duration has passed (as in
* {@link org.mockito.verification.VerificationAfterDelay}).
*/
public VerificationOverTimeImpl(Duration pollingPeriod, Duration duration, VerificationMode delegate, boolean returnOnSuccess) {
this(pollingPeriod, delegate, returnOnSuccess, new Timer(duration));
}
/**
* Create this verification mode, to be used to verify invocation ongoing data later.
*
* @param pollingPeriod The frequency to poll delegate.verify(), to check whether the delegate has been satisfied
* @param delegate The verification mode to delegate overall success or failure to
* @param returnOnSuccess Whether to immediately return successfully once the delegate is satisfied (as in
* {@link org.mockito.verification.VerificationWithTimeout}, or to only return once
* the delegate is satisfied and the full duration has passed (as in
* {@link org.mockito.verification.VerificationAfterDelay}).
* @param timer Checker of whether the duration of the verification is still acceptable
*/
public VerificationOverTimeImpl(Duration pollingPeriod, VerificationMode delegate, boolean returnOnSuccess, Timer timer) {
this.pollingPeriod = pollingPeriod;
this.delegate = delegate;
this.returnOnSuccess = returnOnSuccess;
this.timer = timer;
}
/**
* Verify the given ongoing verification data, and confirm that it satisfies the delegate verification mode
* before the full duration has passed.
*
* In practice, this polls the delegate verification mode until it is satisfied. If it is not satisfied once
* the full duration has passed, the last error returned by the delegate verification mode will be thrown
* here in turn. This may be thrown early if the delegate is unsatisfied and the verification mode is known
* to never recover from this situation (e.g. {@link AtMost}).
*
* If it is satisfied before the full duration has passed, behaviour is dependent on the returnOnSuccess parameter
* given in the constructor. If true, this verification mode is immediately satisfied once the delegate is. If
* false, this verification mode is not satisfied until the delegate is satisfied and the full time has passed.
*
* @throws MockitoAssertionError if the delegate verification mode does not succeed before the timeout
*/
public void verify(VerificationData data) {
AssertionError error = null;
timer.start();
while (timer.isCounting()) {
try {
delegate.verify(data);
if (returnOnSuccess) {
return;
} else {
error = null;
}
} catch (MockitoAssertionError e) {
error = handleVerifyException(e);
}
catch (AssertionError e) {
error = handleVerifyException(e);
}
}
if (error != null) {
throw error;
}
}
private AssertionError handleVerifyException(AssertionError e) {
if (canRecoverFromFailure(delegate)) {
sleep(pollingPeriod);
return e;
} else {
throw e;
}
}
protected boolean canRecoverFromFailure(VerificationMode verificationMode) {
return !(verificationMode instanceof AtMost || verificationMode instanceof NoMoreInteractions);
}
public VerificationOverTimeImpl copyWithVerificationMode(VerificationMode verificationMode) {
return new VerificationOverTimeImpl(pollingPeriod, timer.duration(), verificationMode, returnOnSuccess);
}
private void sleep(Duration sleep) {
try {
Thread.sleep(sleep.toMillis());
} catch (InterruptedException ie) {
throw new RuntimeException("Thread sleep has been interrupted", ie);
}
}
public boolean isReturnOnSuccess() {
return returnOnSuccess;
}
public Duration getPollingPeriod() {
return pollingPeriod;
}
public Timer getTimer() {
return timer;
}
public VerificationMode getDelegate() {
return delegate;
}
}