Debian Codegraph @

src/test/java/io/reactivex/rxjava3/testsupport/BaseTestConsumerEx.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.testsupport;
15
16 import java.util.List;
17
18 import io.reactivex.rxjava3.functions.Predicate;
19 import io.reactivex.rxjava3.internal.util.ExceptionHelper;
20 import io.reactivex.rxjava3.observers.BaseTestConsumer;
21 import io.reactivex.rxjava3.operators.QueueFuseable;
22
23 import java.util.Objects;
24
25 /**
26 * Base class with shared infrastructure to support TestSubscriber and TestObserver.
27 * @param <T> the value type consumed
28 * @param <U> the subclass of this BaseTestConsumer
29 */
30 public abstract class BaseTestConsumerEx<T, U extends BaseTestConsumerEx<T, U>>
31 extends BaseTestConsumer<T, U> {
32
33 protected int initialFusionMode;
34
35 protected int establishedFusionMode;
36
37 /**
38 * The optional tag associated with this test consumer.
39 * @since 2.0.7
40 */
41 protected CharSequence tag;
42
43 /**
44 * Indicates that one of the awaitX method has timed out.
45 * @since 2.0.7
46 */
47 protected boolean timeout;
48
49 public BaseTestConsumerEx() {
50 super();
51 }
52
53 /**
54 * Returns the last thread which called the onXXX methods of this TestObserver/TestSubscriber.
55 * @return the last thread which called the onXXX methods
56 */
57 public final Thread lastThread() {
58 return lastThread;
59 }
60
61 // assertion methods
62
63 /**
64 * Assert that this TestObserver/TestSubscriber did not receive an onNext value which is equal to
65 * the given value with respect to null-safe Object.equals.
66 *
67 * <p>History: 2.0.5 - experimental
68 * @param value the value to expect not being received
69 * @return this
70 * @since 2.1
71 */
72 @SuppressWarnings("unchecked")
73 public final U assertNever(T value) {
74 int s = values.size();
75
76 for (int i = 0; i < s; i++) {
77 T v = this.values.get(i);
78 if (Objects.equals(v, value)) {
79 throw fail("Value at position " + i + " is equal to " + valueAndClass(value) + "; Expected them to be different");
80 }
81 }
82 return (U) this;
83 }
84
85 /**
86 * Asserts that this TestObserver/TestSubscriber did not receive any onNext value for which
87 * the provided predicate returns true.
88 *
89 * <p>History: 2.0.5 - experimental
90 * @param valuePredicate the predicate that receives the onNext value
91 * and should return true for the expected value.
92 * @return this
93 * @since 2.1
94 */
95 @SuppressWarnings("unchecked")
96 public final U assertNever(Predicate<? super T> valuePredicate) {
97 int s = values.size();
98
99 for (int i = 0; i < s; i++) {
100 T v = this.values.get(i);
101 try {
102 if (valuePredicate.test(v)) {
103 throw fail("Value at position " + i + " matches predicate " + valuePredicate.toString() + ", which was not expected.");
104 }
105 } catch (Throwable ex) {
106 throw ExceptionHelper.wrapOrThrow(ex);
107 }
108 }
109 return (U)this;
110 }
111
112 /**
113 * Assert that the TestObserver/TestSubscriber terminated (i.e., the terminal latch reached zero).
114 * @return this
115 */
116 @SuppressWarnings("unchecked")
117 public final U assertTerminated() {
118 if (done.getCount() != 0) {
119 throw fail("Subscriber still running!");
120 }
121 long c = completions;
122 if (c > 1) {
123 throw fail("Terminated with multiple completions: " + c);
124 }
125 int s = errors.size();
126 if (s > 1) {
127 throw fail("Terminated with multiple errors: " + s);
128 }
129
130 if (c != 0 && s != 0) {
131 throw fail("Terminated with multiple completions and errors: " + c);
132 }
133 return (U)this;
134 }
135
136 /**
137 * Assert that the TestObserver/TestSubscriber has not terminated (i.e., the terminal latch is still non-zero).
138 * @return this
139 */
140 @SuppressWarnings("unchecked")
141 public final U assertNotTerminated() {
142 if (done.getCount() == 0) {
143 throw fail("Subscriber terminated!");
144 }
145 return (U)this;
146 }
147
148 /**
149 * Assert that there is a single error and it has the given message.
150 * @param message the message expected
151 * @return this
152 */
153 @SuppressWarnings("unchecked")
154 public final U assertErrorMessage(String message) {
155 int s = errors.size();
156 if (s == 0) {
157 throw fail("No errors");
158 } else
159 if (s == 1) {
160 Throwable e = errors.get(0);
161 String errorMessage = e.getMessage();
162 if (!Objects.equals(message, errorMessage)) {
163 throw fail("\nexpected: " + message + "\ngot: " + errorMessage
164 + "; Error message differs");
165 }
166 } else {
167 throw fail("Multiple errors");
168 }
169 return (U)this;
170 }
171
172 /**
173 * Assert that the upstream signalled the specified values in order and then failed
174 * with a Throwable for which the provided predicate returns true.
175 * @param errorPredicate
176 * the predicate that receives the error Throwable
177 * and should return true for expected errors.
178 * @param values the expected values, asserted in order
179 * @return this
180 */
181 @SafeVarargs
182 public final U assertFailure(Predicate<Throwable> errorPredicate, T... values) {
183 return assertSubscribed()
184 .assertValues(values)
185 .assertError(errorPredicate)
186 .assertNotComplete();
187 }
188
189 /**
190 * Assert that the upstream signalled the specified values in order,
191 * then failed with a specific class or subclass of Throwable
192 * and with the given exact error message.
193 * @param error the expected exception (parent) class
194 * @param message the expected failure message
195 * @param values the expected values, asserted in order
196 * @return this
197 */
198 @SafeVarargs
199 public final U assertFailureAndMessage(Class<? extends Throwable> error,
200 String message, T... values) {
201 return assertSubscribed()
202 .assertValues(values)
203 .assertError(error)
204 .assertErrorMessage(message)
205 .assertNotComplete();
206 }
207
208 /**
209 * Returns true if an await timed out.
210 * @return true if one of the timeout-based await methods has timed out.
211 * <p>History: 2.0.7 - experimental
212 * @see #clearTimeout()
213 * @see #assertTimeout()
214 * @see #assertNoTimeout()
215 * @since 2.1
216 */
217 public final boolean isTimeout() {
218 return timeout;
219 }
220
221 /**
222 * Clears the timeout flag set by the await methods when they timed out.
223 * <p>History: 2.0.7 - experimental
224 * @return this
225 * @since 2.1
226 * @see #isTimeout()
227 */
228 @SuppressWarnings("unchecked")
229 public final U clearTimeout() {
230 timeout = false;
231 return (U)this;
232 }
233
234 /**
235 * Asserts that some awaitX method has timed out.
236 * <p>History: 2.0.7 - experimental
237 * @return this
238 * @since 2.1
239 */
240 @SuppressWarnings("unchecked")
241 public final U assertTimeout() {
242 if (!timeout) {
243 throw fail("No timeout?!");
244 }
245 return (U)this;
246 }
247
248 /**
249 * Asserts that some awaitX method has not timed out.
250 * <p>History: 2.0.7 - experimental
251 * @return this
252 * @since 2.1
253 */
254 @SuppressWarnings("unchecked")
255 public final U assertNoTimeout() {
256 if (timeout) {
257 throw fail("Timeout?!");
258 }
259 return (U)this;
260 }
261
262 /**
263 * Returns the internal shared list of errors.
264 * @return Returns the internal shared list of errors.
265 */
266 public final List<Throwable> errors() {
267 return errors;
268 }
269
270 /**
271 * Returns true if this test consumer has terminated in any fashion.
272 * @return true if this test consumer has terminated in any fashion
273 */
274 public final boolean isTerminated() {
275 return done.getCount() == 0;
276 }
277
278 /**
279 * Returns the number of times onComplete() was called.
280 * @return the number of times onComplete() was called
281 */
282 public final long completions() {
283 return completions;
284 }
285
286 /**
287 * Fail with the given message and add the sequence of errors as suppressed ones.
288 * <p>Note this is deliberately the only fail method. Most of the times an assertion
289 * would fail but it is possible it was due to an exception somewhere. This construct
290 * will capture those potential errors and report it along with the original failure.
291 *
292 * @param message the message to use
293 * @return AssertionError the prepared AssertionError instance
294 */
295 public final AssertionError failWith(String message) {
296 return fail(message);
297 }
298
299 static String fusionModeToString(int mode) {
300 switch (mode) {
301 case QueueFuseable.NONE : return "NONE";
302 case QueueFuseable.SYNC : return "SYNC";
303 case QueueFuseable.ASYNC : return "ASYNC";
304 default: return "Unknown(" + mode + ")";
305 }
306 }
307 }