Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/subscribers/BoundedSubscriberTest.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.internal.subscribers;
15
16 import static org.junit.Assert.*;
17
18 import java.util.*;
19
20 import org.junit.Test;
21 import org.reactivestreams.*;
22
23 import io.reactivex.rxjava3.core.*;
24 import io.reactivex.rxjava3.exceptions.*;
25 import io.reactivex.rxjava3.functions.*;
26 import io.reactivex.rxjava3.internal.functions.Functions;
27 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
28 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
29 import io.reactivex.rxjava3.processors.PublishProcessor;
30 import io.reactivex.rxjava3.testsupport.*;
31
32 public class BoundedSubscriberTest extends RxJavaTest {
33
34 @Test
35 public void onSubscribeThrows() {
36 final List<Object> received = new ArrayList<>();
37
38 BoundedSubscriber<Object> subscriber = new BoundedSubscriber<>(new Consumer<Object>() {
39 @Override
40 public void accept(Object o) throws Exception {
41 received.add(o);
42 }
43 }, new Consumer<Throwable>() {
44 @Override
45 public void accept(Throwable throwable) throws Exception {
46 received.add(throwable);
47 }
48 }, new Action() {
49 @Override
50 public void run() throws Exception {
51 received.add(1);
52 }
53 }, new Consumer<Subscription>() {
54 @Override
55 public void accept(Subscription subscription) throws Exception {
56 throw new TestException();
57 }
58 }, 128);
59
60 assertFalse(subscriber.isDisposed());
61
62 Flowable.just(1).subscribe(subscriber);
63
64 assertTrue(received.toString(), received.get(0) instanceof TestException);
65 assertEquals(received.toString(), 1, received.size());
66
67 assertTrue(subscriber.isDisposed());
68 }
69
70 @Test
71 public void onNextThrows() {
72 final List<Object> received = new ArrayList<>();
73
74 BoundedSubscriber<Object> subscriber = new BoundedSubscriber<>(new Consumer<Object>() {
75 @Override
76 public void accept(Object o) throws Exception {
77 throw new TestException();
78 }
79 }, new Consumer<Throwable>() {
80 @Override
81 public void accept(Throwable throwable) throws Exception {
82 received.add(throwable);
83 }
84 }, new Action() {
85 @Override
86 public void run() throws Exception {
87 received.add(1);
88 }
89 }, new Consumer<Subscription>() {
90 @Override
91 public void accept(Subscription subscription) throws Exception {
92 subscription.request(128);
93 }
94 }, 128);
95
96 assertFalse(subscriber.isDisposed());
97
98 Flowable.just(1).subscribe(subscriber);
99
100 assertTrue(received.toString(), received.get(0) instanceof TestException);
101 assertEquals(received.toString(), 1, received.size());
102
103 assertTrue(subscriber.isDisposed());
104 }
105
106 @Test
107 public void onErrorThrows() {
108 List<Throwable> errors = TestHelper.trackPluginErrors();
109
110 try {
111 final List<Object> received = new ArrayList<>();
112
113 BoundedSubscriber<Object> subscriber = new BoundedSubscriber<>(new Consumer<Object>() {
114 @Override
115 public void accept(Object o) throws Exception {
116 received.add(o);
117 }
118 }, new Consumer<Throwable>() {
119 @Override
120 public void accept(Throwable throwable) throws Exception {
121 throw new TestException("Inner");
122 }
123 }, new Action() {
124 @Override
125 public void run() throws Exception {
126 received.add(1);
127 }
128 }, new Consumer<Subscription>() {
129 @Override
130 public void accept(Subscription subscription) throws Exception {
131 subscription.request(128);
132 }
133 }, 128);
134
135 assertFalse(subscriber.isDisposed());
136
137 Flowable.<Integer>error(new TestException("Outer")).subscribe(subscriber);
138
139 assertTrue(received.toString(), received.isEmpty());
140
141 assertTrue(subscriber.isDisposed());
142
143 TestHelper.assertError(errors, 0, CompositeException.class);
144 List<Throwable> ce = TestHelper.compositeList(errors.get(0));
145 TestHelper.assertError(ce, 0, TestException.class, "Outer");
146 TestHelper.assertError(ce, 1, TestException.class, "Inner");
147 } finally {
148 RxJavaPlugins.reset();
149 }
150 }
151
152 @Test
153 public void onCompleteThrows() {
154 List<Throwable> errors = TestHelper.trackPluginErrors();
155
156 try {
157 final List<Object> received = new ArrayList<>();
158
159 BoundedSubscriber<Object> subscriber = new BoundedSubscriber<>(new Consumer<Object>() {
160 @Override
161 public void accept(Object o) throws Exception {
162 received.add(o);
163 }
164 }, new Consumer<Throwable>() {
165 @Override
166 public void accept(Throwable throwable) throws Exception {
167 received.add(throwable);
168 }
169 }, new Action() {
170 @Override
171 public void run() throws Exception {
172 throw new TestException();
173 }
174 }, new Consumer<Subscription>() {
175 @Override
176 public void accept(Subscription subscription) throws Exception {
177 subscription.request(128);
178 }
179 }, 128);
180
181 assertFalse(subscriber.isDisposed());
182
183 Flowable.<Integer>empty().subscribe(subscriber);
184
185 assertTrue(received.toString(), received.isEmpty());
186
187 assertTrue(subscriber.isDisposed());
188
189 TestHelper.assertUndeliverable(errors, 0, TestException.class);
190 } finally {
191 RxJavaPlugins.reset();
192 }
193 }
194
195 @Test
196 public void onNextThrowsCancelsUpstream() {
197 PublishProcessor<Integer> pp = PublishProcessor.create();
198
199 final List<Throwable> errors = new ArrayList<>();
200
201 BoundedSubscriber<Integer> s = new BoundedSubscriber<>(new Consumer<Integer>() {
202 @Override
203 public void accept(Integer v) throws Exception {
204 throw new TestException();
205 }
206 }, new Consumer<Throwable>() {
207 @Override
208 public void accept(Throwable e) throws Exception {
209 errors.add(e);
210 }
211 }, new Action() {
212 @Override
213 public void run() throws Exception {
214
215 }
216 }, new Consumer<Subscription>() {
217 @Override
218 public void accept(Subscription subscription) throws Exception {
219 subscription.request(128);
220 }
221 }, 128);
222
223 pp.subscribe(s);
224
225 assertTrue("No observers?!", pp.hasSubscribers());
226 assertTrue("Has errors already?!", errors.isEmpty());
227
228 pp.onNext(1);
229
230 assertFalse("Has observers?!", pp.hasSubscribers());
231 assertFalse("No errors?!", errors.isEmpty());
232
233 assertTrue(errors.toString(), errors.get(0) instanceof TestException);
234 }
235
236 @Test
237 public void onSubscribeThrowsCancelsUpstream() {
238 PublishProcessor<Integer> pp = PublishProcessor.create();
239
240 final List<Throwable> errors = new ArrayList<>();
241
242 BoundedSubscriber<Integer> s = new BoundedSubscriber<>(new Consumer<Integer>() {
243 @Override
244 public void accept(Integer v) throws Exception {
245 }
246 }, new Consumer<Throwable>() {
247 @Override
248 public void accept(Throwable e) throws Exception {
249 errors.add(e);
250 }
251 }, new Action() {
252 @Override
253 public void run() throws Exception {
254 }
255 }, new Consumer<Subscription>() {
256 @Override
257 public void accept(Subscription s) throws Exception {
258 throw new TestException();
259 }
260 }, 128);
261
262 pp.subscribe(s);
263
264 assertFalse("Has observers?!", pp.hasSubscribers());
265 assertFalse("No errors?!", errors.isEmpty());
266
267 assertTrue(errors.toString(), errors.get(0) instanceof TestException);
268 }
269
270 @Test
271 public void badSourceOnSubscribe() {
272 Flowable<Integer> source = Flowable.fromPublisher(new Publisher<Integer>() {
273 @Override
274 public void subscribe(Subscriber<? super Integer> s) {
275 BooleanSubscription s1 = new BooleanSubscription();
276 s.onSubscribe(s1);
277 BooleanSubscription s2 = new BooleanSubscription();
278 s.onSubscribe(s2);
279
280 assertFalse(s1.isCancelled());
281 assertTrue(s2.isCancelled());
282
283 s.onNext(1);
284 s.onComplete();
285 }
286 });
287
288 final List<Object> received = new ArrayList<>();
289
290 BoundedSubscriber<Object> subscriber = new BoundedSubscriber<>(new Consumer<Object>() {
291 @Override
292 public void accept(Object v) throws Exception {
293 received.add(v);
294 }
295 }, new Consumer<Throwable>() {
296 @Override
297 public void accept(Throwable e) throws Exception {
298 received.add(e);
299 }
300 }, new Action() {
301 @Override
302 public void run() throws Exception {
303 received.add(100);
304 }
305 }, new Consumer<Subscription>() {
306 @Override
307 public void accept(Subscription s) throws Exception {
308 s.request(128);
309 }
310 }, 128);
311
312 source.subscribe(subscriber);
313
314 assertEquals(Arrays.asList(1, 100), received);
315 }
316
317 @Test
318 @SuppressUndeliverable
319 public void badSourceEmitAfterDone() {
320 Flowable<Integer> source = Flowable.fromPublisher(new Publisher<Integer>() {
321 @Override
322 public void subscribe(Subscriber<? super Integer> s) {
323 BooleanSubscription s1 = new BooleanSubscription();
324 s.onSubscribe(s1);
325
326 s.onNext(1);
327 s.onComplete();
328 s.onNext(2);
329 s.onError(new TestException());
330 s.onComplete();
331 }
332 });
333
334 final List<Object> received = new ArrayList<>();
335
336 BoundedSubscriber<Object> subscriber = new BoundedSubscriber<>(new Consumer<Object>() {
337 @Override
338 public void accept(Object v) throws Exception {
339 received.add(v);
340 }
341 }, new Consumer<Throwable>() {
342 @Override
343 public void accept(Throwable e) throws Exception {
344 received.add(e);
345 }
346 }, new Action() {
347 @Override
348 public void run() throws Exception {
349 received.add(100);
350 }
351 }, new Consumer<Subscription>() {
352 @Override
353 public void accept(Subscription s) throws Exception {
354 s.request(128);
355 }
356 }, 128);
357
358 source.subscribe(subscriber);
359
360 assertEquals(Arrays.asList(1, 100), received);
361 }
362
363 @Test
364 public void onErrorMissingShouldReportNoCustomOnError() {
365 BoundedSubscriber<Integer> subscriber = new BoundedSubscriber<>(Functions.<Integer>emptyConsumer(),
366 Functions.ON_ERROR_MISSING,
367 Functions.EMPTY_ACTION,
368 Functions.<Subscription>boundedConsumer(128), 128);
369
370 assertFalse(subscriber.hasCustomOnError());
371 }
372
373 @Test
374 public void customOnErrorShouldReportCustomOnError() {
375 BoundedSubscriber<Integer> subscriber = new BoundedSubscriber<>(Functions.<Integer>emptyConsumer(),
376 Functions.<Throwable>emptyConsumer(),
377 Functions.EMPTY_ACTION,
378 Functions.<Subscription>boundedConsumer(128), 128);
379
380 assertTrue(subscriber.hasCustomOnError());
381 }
382
383 @Test
384 public void cancel() {
385 BoundedSubscriber<Integer> subscriber = new BoundedSubscriber<>(Functions.<Integer>emptyConsumer(),
386 Functions.<Throwable>emptyConsumer(),
387 Functions.EMPTY_ACTION,
388 Functions.<Subscription>boundedConsumer(128), 128);
389
390 BooleanSubscription bs = new BooleanSubscription();
391 subscriber.onSubscribe(bs);
392
393 subscriber.cancel();
394
395 assertTrue(bs.isCancelled());
396 }
397
398 @Test
399 public void dispose() {
400 BoundedSubscriber<Integer> subscriber = new BoundedSubscriber<>(Functions.<Integer>emptyConsumer(),
401 Functions.<Throwable>emptyConsumer(),
402 Functions.EMPTY_ACTION,
403 Functions.<Subscription>boundedConsumer(128), 128);
404
405 BooleanSubscription bs = new BooleanSubscription();
406 subscriber.onSubscribe(bs);
407
408 assertFalse(subscriber.isDisposed());
409
410 subscriber.dispose();
411
412 assertTrue(bs.isCancelled());
413 assertTrue(subscriber.isDisposed());
414 }
415 }