Debian Codegraph @

src/test/java/io/reactivex/rxjava3/processors/AsyncProcessorTest.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.processors;
15
16 import static org.junit.Assert.*;
17 import static org.mockito.ArgumentMatchers.*;
18 import static org.mockito.Mockito.*;
19
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.atomic.AtomicReference;
22
23 import org.junit.Test;
24 import org.mockito.*;
25 import org.reactivestreams.Subscriber;
26
27 import io.reactivex.rxjava3.exceptions.TestException;
28 import io.reactivex.rxjava3.functions.Consumer;
29 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
30 import io.reactivex.rxjava3.operators.QueueFuseable;
31 import io.reactivex.rxjava3.subscribers.TestSubscriber;
32 import io.reactivex.rxjava3.testsupport.*;
33
34 public class AsyncProcessorTest extends FlowableProcessorTest<Object> {
35
36 private final Throwable testException = new Throwable();
37
38 @Override
39 protected FlowableProcessor<Object> create() {
40 return AsyncProcessor.create();
41 }
42
43 @Test
44 public void neverCompleted() {
45 AsyncProcessor<String> processor = AsyncProcessor.create();
46
47 Subscriber<String> subscriber = TestHelper.mockSubscriber();
48 processor.subscribe(subscriber);
49
50 processor.onNext("one");
51 processor.onNext("two");
52 processor.onNext("three");
53
54 verify(subscriber, Mockito.never()).onNext(anyString());
55 verify(subscriber, Mockito.never()).onError(testException);
56 verify(subscriber, Mockito.never()).onComplete();
57 }
58
59 @Test
60 public void completed() {
61 AsyncProcessor<String> processor = AsyncProcessor.create();
62
63 Subscriber<String> subscriber = TestHelper.mockSubscriber();
64 processor.subscribe(subscriber);
65
66 processor.onNext("one");
67 processor.onNext("two");
68 processor.onNext("three");
69 processor.onComplete();
70
71 verify(subscriber, times(1)).onNext("three");
72 verify(subscriber, Mockito.never()).onError(any(Throwable.class));
73 verify(subscriber, times(1)).onComplete();
74 }
75
76 @Test
77 public void subscribeAfterCompleted() {
78 AsyncProcessor<String> processor = AsyncProcessor.create();
79
80 Subscriber<String> subscriber = TestHelper.mockSubscriber();
81
82 processor.onNext("one");
83 processor.onNext("two");
84 processor.onNext("three");
85 processor.onComplete();
86
87 processor.subscribe(subscriber);
88
89 verify(subscriber, times(1)).onNext("three");
90 verify(subscriber, Mockito.never()).onError(any(Throwable.class));
91 verify(subscriber, times(1)).onComplete();
92 }
93
94 @Test
95 public void subscribeAfterError() {
96 AsyncProcessor<String> processor = AsyncProcessor.create();
97
98 Subscriber<String> subscriber = TestHelper.mockSubscriber();
99
100 processor.onNext("one");
101 processor.onNext("two");
102 processor.onNext("three");
103
104 RuntimeException re = new RuntimeException("failed");
105 processor.onError(re);
106
107 processor.subscribe(subscriber);
108
109 verify(subscriber, times(1)).onError(re);
110 verify(subscriber, Mockito.never()).onNext(any(String.class));
111 verify(subscriber, Mockito.never()).onComplete();
112 }
113
114 @Test
115 @SuppressUndeliverable
116 public void error() {
117 AsyncProcessor<String> processor = AsyncProcessor.create();
118
119 Subscriber<String> subscriber = TestHelper.mockSubscriber();
120 processor.subscribe(subscriber);
121
122 processor.onNext("one");
123 processor.onNext("two");
124 processor.onNext("three");
125 processor.onError(testException);
126 processor.onNext("four");
127 processor.onError(new Throwable());
128 processor.onComplete();
129
130 verify(subscriber, Mockito.never()).onNext(anyString());
131 verify(subscriber, times(1)).onError(testException);
132 verify(subscriber, Mockito.never()).onComplete();
133 }
134
135 @Test
136 public void unsubscribeBeforeCompleted() {
137 AsyncProcessor<String> processor = AsyncProcessor.create();
138
139 Subscriber<String> subscriber = TestHelper.mockSubscriber();
140 TestSubscriber<String> ts = new TestSubscriber<>(subscriber);
141 processor.subscribe(ts);
142
143 processor.onNext("one");
144 processor.onNext("two");
145
146 ts.cancel();
147
148 verify(subscriber, Mockito.never()).onNext(anyString());
149 verify(subscriber, Mockito.never()).onError(any(Throwable.class));
150 verify(subscriber, Mockito.never()).onComplete();
151
152 processor.onNext("three");
153 processor.onComplete();
154
155 verify(subscriber, Mockito.never()).onNext(anyString());
156 verify(subscriber, Mockito.never()).onError(any(Throwable.class));
157 verify(subscriber, Mockito.never()).onComplete();
158 }
159
160 @Test
161 public void emptySubjectCompleted() {
162 AsyncProcessor<String> processor = AsyncProcessor.create();
163
164 Subscriber<String> subscriber = TestHelper.mockSubscriber();
165 processor.subscribe(subscriber);
166
167 processor.onComplete();
168
169 InOrder inOrder = inOrder(subscriber);
170 inOrder.verify(subscriber, never()).onNext(null);
171 inOrder.verify(subscriber, never()).onNext(any(String.class));
172 inOrder.verify(subscriber, times(1)).onComplete();
173 inOrder.verifyNoMoreInteractions();
174 }
175
176 /**
177 * Can receive timeout if subscribe never receives an onError/onComplete ... which reveals a race condition.
178 */
179 @Test
180 public void subscribeCompletionRaceCondition() {
181 /*
182 * With non-threadsafe code this fails most of the time on my dev laptop and is non-deterministic enough
183 * to act as a unit test to the race conditions.
184 *
185 * With the synchronization code in place I can not get this to fail on my laptop.
186 */
187 for (int i = 0; i < 50; i++) {
188 final AsyncProcessor<String> processor = AsyncProcessor.create();
189 final AtomicReference<String> value1 = new AtomicReference<>();
190
191 processor.subscribe(new Consumer<String>() {
192
193 @Override
194 public void accept(String t1) {
195 try {
196 // simulate a slow observer
197 Thread.sleep(50);
198 } catch (InterruptedException e) {
199 e.printStackTrace();
200 }
201 value1.set(t1);
202 }
203
204 });
205
206 Thread t1 = new Thread(new Runnable() {
207
208 @Override
209 public void run() {
210 processor.onNext("value");
211 processor.onComplete();
212 }
213 });
214
215 SubjectSubscriberThread t2 = new SubjectSubscriberThread(processor);
216 SubjectSubscriberThread t3 = new SubjectSubscriberThread(processor);
217 SubjectSubscriberThread t4 = new SubjectSubscriberThread(processor);
218 SubjectSubscriberThread t5 = new SubjectSubscriberThread(processor);
219
220 t2.start();
221 t3.start();
222 t1.start();
223 t4.start();
224 t5.start();
225 try {
226 t1.join();
227 t2.join();
228 t3.join();
229 t4.join();
230 t5.join();
231 } catch (InterruptedException e) {
232 throw new RuntimeException(e);
233 }
234
235 assertEquals("value", value1.get());
236 assertEquals("value", t2.value.get());
237 assertEquals("value", t3.value.get());
238 assertEquals("value", t4.value.get());
239 assertEquals("value", t5.value.get());
240 }
241
242 }
243
244 private static class SubjectSubscriberThread extends Thread {
245
246 private final AsyncProcessor<String> processor;
247 private final AtomicReference<String> value = new AtomicReference<>();
248
249 SubjectSubscriberThread(AsyncProcessor<String> processor) {
250 this.processor = processor;
251 }
252
253 @Override
254 public void run() {
255 try {
256 // a timeout exception will happen if we don't get a terminal state
257 String v = processor.timeout(2000, TimeUnit.MILLISECONDS).blockingSingle();
258 value.set(v);
259 } catch (Exception e) {
260 e.printStackTrace();
261 }
262 }
263 }
264
265 @Test
266 public void currentStateMethodsNormal() {
267 AsyncProcessor<Object> as = AsyncProcessor.create();
268
269 assertFalse(as.hasValue());
270 assertFalse(as.hasThrowable());
271 assertFalse(as.hasComplete());
272 assertNull(as.getValue());
273 assertNull(as.getThrowable());
274
275 as.onNext(1);
276
277 assertFalse(as.hasValue()); // AP no longer reports it has a value until it is terminated
278 assertFalse(as.hasThrowable());
279 assertFalse(as.hasComplete());
280 assertNull(as.getValue()); // AP no longer reports it has a value until it is terminated
281 assertNull(as.getThrowable());
282
283 as.onComplete();
284 assertTrue(as.hasValue());
285 assertFalse(as.hasThrowable());
286 assertTrue(as.hasComplete());
287 assertEquals(1, as.getValue());
288 assertNull(as.getThrowable());
289 }
290
291 @Test
292 public void currentStateMethodsEmpty() {
293 AsyncProcessor<Object> as = AsyncProcessor.create();
294
295 assertFalse(as.hasValue());
296 assertFalse(as.hasThrowable());
297 assertFalse(as.hasComplete());
298 assertNull(as.getValue());
299 assertNull(as.getThrowable());
300
301 as.onComplete();
302
303 assertFalse(as.hasValue());
304 assertFalse(as.hasThrowable());
305 assertTrue(as.hasComplete());
306 assertNull(as.getValue());
307 assertNull(as.getThrowable());
308 }
309
310 @Test
311 public void currentStateMethodsError() {
312 AsyncProcessor<Object> as = AsyncProcessor.create();
313
314 assertFalse(as.hasValue());
315 assertFalse(as.hasThrowable());
316 assertFalse(as.hasComplete());
317 assertNull(as.getValue());
318 assertNull(as.getThrowable());
319
320 as.onError(new TestException());
321
322 assertFalse(as.hasValue());
323 assertTrue(as.hasThrowable());
324 assertFalse(as.hasComplete());
325 assertNull(as.getValue());
326 assertTrue(as.getThrowable() instanceof TestException);
327 }
328
329 @Test
330 public void fusionLive() {
331 AsyncProcessor<Integer> ap = new AsyncProcessor<>();
332
333 TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY);
334
335 ap.subscribe(ts);
336
337 ts
338 .assertFuseable()
339 .assertFusionMode(QueueFuseable.ASYNC);
340
341 ts.assertNoValues().assertNoErrors().assertNotComplete();
342
343 ap.onNext(1);
344
345 ts.assertNoValues().assertNoErrors().assertNotComplete();
346
347 ap.onComplete();
348
349 ts.assertResult(1);
350 }
351
352 @Test
353 public void fusionOfflie() {
354 AsyncProcessor<Integer> ap = new AsyncProcessor<>();
355 ap.onNext(1);
356 ap.onComplete();
357
358 TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY);
359
360 ap.subscribe(ts);
361
362 ts
363 .assertFuseable()
364 .assertFusionMode(QueueFuseable.ASYNC)
365 .assertResult(1);
366 }
367
368 @Test
369 public void onSubscribeAfterDone() {
370 AsyncProcessor<Object> p = AsyncProcessor.create();
371
372 BooleanSubscription bs = new BooleanSubscription();
373 p.onSubscribe(bs);
374
375 assertFalse(bs.isCancelled());
376
377 p.onComplete();
378
379 bs = new BooleanSubscription();
380 p.onSubscribe(bs);
381
382 assertTrue(bs.isCancelled());
383
384 p.test().assertResult();
385 }
386
387 @Test
388 public void cancelUpfront() {
389 AsyncProcessor<Object> p = AsyncProcessor.create();
390
391 assertFalse(p.hasSubscribers());
392
393 p.test().assertEmpty();
394 p.test().assertEmpty();
395
396 p.test(0L, true).assertEmpty();
397
398 assertTrue(p.hasSubscribers());
399 }
400
401 @Test
402 public void cancelRace() {
403 AsyncProcessor<Object> p = AsyncProcessor.create();
404
405 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
406 final TestSubscriber<Object> ts1 = p.test();
407 final TestSubscriber<Object> ts2 = p.test();
408
409 Runnable r1 = new Runnable() {
410 @Override
411 public void run() {
412 ts1.cancel();
413 }
414 };
415
416 Runnable r2 = new Runnable() {
417 @Override
418 public void run() {
419 ts2.cancel();
420 }
421 };
422
423 TestHelper.race(r1, r2);
424 }
425 }
426
427 @Test
428 @SuppressUndeliverable
429 public void onErrorCancelRace() {
430
431 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
432 final AsyncProcessor<Object> p = AsyncProcessor.create();
433
434 final TestSubscriberEx<Object> ts1 = p.to(TestHelper.<Object>testConsumer());
435
436 Runnable r1 = new Runnable() {
437 @Override
438 public void run() {
439 ts1.cancel();
440 }
441 };
442
443 final TestException ex = new TestException();
444
445 Runnable r2 = new Runnable() {
446 @Override
447 public void run() {
448 p.onError(ex);
449 }
450 };
451
452 TestHelper.race(r1, r2);
453
454 if (ts1.errors().size() != 0) {
455 ts1.assertFailure(TestException.class);
456 } else {
457 ts1.assertEmpty();
458 }
459 }
460 }
461
462 @Test
463 public void onNextCrossCancel() {
464 AsyncProcessor<Object> p = AsyncProcessor.create();
465
466 final TestSubscriber<Object> ts2 = new TestSubscriber<>();
467 TestSubscriber<Object> ts1 = new TestSubscriber<Object>() {
468 @Override
469 public void onNext(Object t) {
470 ts2.cancel();
471 super.onNext(t);
472 }
473 };
474
475 p.subscribe(ts1);
476 p.subscribe(ts2);
477
478 p.onNext(1);
479 p.onComplete();
480
481 ts1.assertResult(1);
482 ts2.assertEmpty();
483 }
484
485 @Test
486 @SuppressUndeliverable
487 public void onErrorCrossCancel() {
488 AsyncProcessor<Object> p = AsyncProcessor.create();
489
490 final TestSubscriber<Object> ts2 = new TestSubscriber<>();
491 TestSubscriber<Object> ts1 = new TestSubscriber<Object>() {
492 @Override
493 public void onError(Throwable t) {
494 ts2.cancel();
495 super.onError(t);
496 }
497 };
498
499 p.subscribe(ts1);
500 p.subscribe(ts2);
501
502 p.onError(new TestException());
503
504 ts1.assertFailure(TestException.class);
505 ts2.assertEmpty();
506 }
507
508 @Test
509 public void onCompleteCrossCancel() {
510 AsyncProcessor<Object> p = AsyncProcessor.create();
511
512 final TestSubscriber<Object> ts2 = new TestSubscriber<>();
513 TestSubscriber<Object> ts1 = new TestSubscriber<Object>() {
514 @Override
515 public void onComplete() {
516 ts2.cancel();
517 super.onComplete();
518 }
519 };
520
521 p.subscribe(ts1);
522 p.subscribe(ts2);
523
524 p.onComplete();
525
526 ts1.assertResult();
527 ts2.assertEmpty();
528 }
529
530 @Test
531 public void cancel() {
532 TestHelper.checkDisposed(AsyncProcessor.create());
533 }
534 }