Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/single/SingleTakeUntilTest.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.operators.single;
15
16 import static org.junit.Assert.*;
17
18 import java.util.List;
19 import java.util.concurrent.CancellationException;
20
21 import org.junit.Test;
22 import org.reactivestreams.Subscriber;
23
24 import io.reactivex.rxjava3.core.*;
25 import io.reactivex.rxjava3.exceptions.TestException;
26 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
27 import io.reactivex.rxjava3.observers.TestObserver;
28 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
29 import io.reactivex.rxjava3.processors.PublishProcessor;
30 import io.reactivex.rxjava3.subjects.*;
31 import io.reactivex.rxjava3.testsupport.TestHelper;
32
33 public class SingleTakeUntilTest extends RxJavaTest {
34
35 @Test
36 public void mainSuccessPublisher() {
37 PublishProcessor<Integer> pp = PublishProcessor.create();
38 PublishProcessor<Integer> source = PublishProcessor.create();
39
40 TestObserver<Integer> to = source.single(-99).takeUntil(pp)
41 .test();
42
43 source.onNext(1);
44 source.onComplete();
45
46 to.assertResult(1);
47 }
48
49 @Test
50 public void mainSuccessSingle() {
51 PublishProcessor<Integer> pp = PublishProcessor.create();
52 PublishProcessor<Integer> source = PublishProcessor.create();
53
54 TestObserver<Integer> to = source.single(-99).takeUntil(pp.single(-99))
55 .test();
56
57 source.onNext(1);
58 source.onComplete();
59
60 to.assertResult(1);
61 }
62
63 @Test
64 public void mainSuccessCompletable() {
65 PublishProcessor<Integer> pp = PublishProcessor.create();
66 PublishProcessor<Integer> source = PublishProcessor.create();
67
68 TestObserver<Integer> to = source.single(-99).takeUntil(pp.ignoreElements())
69 .test();
70
71 source.onNext(1);
72 source.onComplete();
73
74 to.assertResult(1);
75 }
76
77 @Test
78 public void mainErrorPublisher() {
79 PublishProcessor<Integer> pp = PublishProcessor.create();
80 PublishProcessor<Integer> source = PublishProcessor.create();
81
82 TestObserver<Integer> to = source.single(-99).takeUntil(pp)
83 .test();
84
85 source.onError(new TestException());
86
87 to.assertFailure(TestException.class);
88 }
89
90 @Test
91 public void mainErrorSingle() {
92 PublishProcessor<Integer> pp = PublishProcessor.create();
93 PublishProcessor<Integer> source = PublishProcessor.create();
94
95 TestObserver<Integer> to = source.single(-99).takeUntil(pp.single(-99))
96 .test();
97
98 source.onError(new TestException());
99
100 to.assertFailure(TestException.class);
101 }
102
103 @Test
104 public void mainErrorCompletable() {
105 PublishProcessor<Integer> pp = PublishProcessor.create();
106 PublishProcessor<Integer> source = PublishProcessor.create();
107
108 TestObserver<Integer> to = source.single(-99).takeUntil(pp.ignoreElements())
109 .test();
110
111 source.onError(new TestException());
112
113 to.assertFailure(TestException.class);
114 }
115
116 @Test
117 public void otherOnNextPublisher() {
118 PublishProcessor<Integer> pp = PublishProcessor.create();
119 PublishProcessor<Integer> source = PublishProcessor.create();
120
121 TestObserver<Integer> to = source.single(-99).takeUntil(pp)
122 .test();
123
124 pp.onNext(1);
125
126 to.assertFailure(CancellationException.class);
127 }
128
129 @Test
130 public void otherOnNextSingle() {
131 PublishProcessor<Integer> pp = PublishProcessor.create();
132 PublishProcessor<Integer> source = PublishProcessor.create();
133
134 TestObserver<Integer> to = source.single(-99).takeUntil(pp.single(-99))
135 .test();
136
137 pp.onNext(1);
138 pp.onComplete();
139
140 to.assertFailure(CancellationException.class);
141 }
142
143 @Test
144 public void otherOnNextCompletable() {
145 PublishProcessor<Integer> pp = PublishProcessor.create();
146 PublishProcessor<Integer> source = PublishProcessor.create();
147
148 TestObserver<Integer> to = source.single(-99).takeUntil(pp.ignoreElements())
149 .test();
150
151 pp.onNext(1);
152 pp.onComplete();
153
154 to.assertFailure(CancellationException.class);
155 }
156
157 @Test
158 public void otherOnCompletePublisher() {
159 PublishProcessor<Integer> pp = PublishProcessor.create();
160 PublishProcessor<Integer> source = PublishProcessor.create();
161
162 TestObserver<Integer> to = source.single(-99).takeUntil(pp)
163 .test();
164
165 pp.onComplete();
166
167 to.assertFailure(CancellationException.class);
168 }
169
170 @Test
171 public void otherOnCompleteCompletable() {
172 PublishProcessor<Integer> pp = PublishProcessor.create();
173 PublishProcessor<Integer> source = PublishProcessor.create();
174
175 TestObserver<Integer> to = source.single(-99).takeUntil(pp.ignoreElements())
176 .test();
177
178 pp.onComplete();
179
180 to.assertFailure(CancellationException.class);
181 }
182
183 @Test
184 public void otherErrorPublisher() {
185 PublishProcessor<Integer> pp = PublishProcessor.create();
186 PublishProcessor<Integer> source = PublishProcessor.create();
187
188 TestObserver<Integer> to = source.single(-99).takeUntil(pp)
189 .test();
190
191 pp.onError(new TestException());
192
193 to.assertFailure(TestException.class);
194 }
195
196 @Test
197 public void otherErrorSingle() {
198 PublishProcessor<Integer> pp = PublishProcessor.create();
199 PublishProcessor<Integer> source = PublishProcessor.create();
200
201 TestObserver<Integer> to = source.single(-99).takeUntil(pp.single(-99))
202 .test();
203
204 pp.onError(new TestException());
205
206 to.assertFailure(TestException.class);
207 }
208
209 @Test
210 public void otherErrorCompletable() {
211 PublishProcessor<Integer> pp = PublishProcessor.create();
212 PublishProcessor<Integer> source = PublishProcessor.create();
213
214 TestObserver<Integer> to = source.single(-99).takeUntil(pp.ignoreElements())
215 .test();
216
217 pp.onError(new TestException());
218
219 to.assertFailure(TestException.class);
220 }
221
222 @Test
223 public void withPublisherDispose() {
224 TestHelper.checkDisposed(Single.never().takeUntil(Flowable.never()));
225 }
226
227 @Test
228 public void onErrorRace() {
229 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
230 List<Throwable> errors = TestHelper.trackPluginErrors();
231
232 try {
233 final PublishProcessor<Integer> pp1 = PublishProcessor.create();
234 final PublishProcessor<Integer> pp2 = PublishProcessor.create();
235
236 TestObserver<Integer> to = pp1.singleOrError().takeUntil(pp2).test();
237
238 final TestException ex = new TestException();
239
240 Runnable r1 = new Runnable() {
241 @Override
242 public void run() {
243 pp1.onError(ex);
244 }
245 };
246
247 Runnable r2 = new Runnable() {
248 @Override
249 public void run() {
250 pp2.onError(ex);
251 }
252 };
253
254 TestHelper.race(r1, r2);
255
256 to.assertFailure(TestException.class);
257
258 if (!errors.isEmpty()) {
259 TestHelper.assertUndeliverable(errors, 0, TestException.class);
260 }
261 } finally {
262 RxJavaPlugins.reset();
263 }
264 }
265 }
266
267 @Test
268 public void otherSignalsAndCompletes() {
269 List<Throwable> errors = TestHelper.trackPluginErrors();
270 try {
271 Single.just(1).takeUntil(Flowable.just(1).take(1))
272 .test()
273 .assertFailure(CancellationException.class);
274
275 assertTrue(errors.toString(), errors.isEmpty());
276 } finally {
277 RxJavaPlugins.reset();
278 }
279 }
280
281 @Test
282 public void flowableCancelDelayed() {
283 Single.never()
284 .takeUntil(new Flowable<Integer>() {
285 @Override
286 protected void subscribeActual(Subscriber<? super Integer> s) {
287 s.onSubscribe(new BooleanSubscription());
288 s.onNext(1);
289 s.onNext(2);
290 }
291 })
292 .test()
293 .assertFailure(CancellationException.class);
294 }
295
296 @Test
297 public void untilSingleMainSuccess() {
298 SingleSubject<Integer> main = SingleSubject.create();
299 SingleSubject<Integer> other = SingleSubject.create();
300
301 TestObserver<Integer> to = main.takeUntil(other).test();
302
303 assertTrue("Main no observers?", main.hasObservers());
304 assertTrue("Other no observers?", other.hasObservers());
305
306 main.onSuccess(1);
307
308 assertFalse("Main has observers?", main.hasObservers());
309 assertFalse("Other has observers?", other.hasObservers());
310
311 to.assertResult(1);
312 }
313
314 @Test
315 public void untilSingleMainError() {
316 SingleSubject<Integer> main = SingleSubject.create();
317 SingleSubject<Integer> other = SingleSubject.create();
318
319 TestObserver<Integer> to = main.takeUntil(other).test();
320
321 assertTrue("Main no observers?", main.hasObservers());
322 assertTrue("Other no observers?", other.hasObservers());
323
324 main.onError(new TestException());
325
326 assertFalse("Main has observers?", main.hasObservers());
327 assertFalse("Other has observers?", other.hasObservers());
328
329 to.assertFailure(TestException.class);
330 }
331
332 @Test
333 public void untilSingleOtherSuccess() {
334 SingleSubject<Integer> main = SingleSubject.create();
335 SingleSubject<Integer> other = SingleSubject.create();
336
337 TestObserver<Integer> to = main.takeUntil(other).test();
338
339 assertTrue("Main no observers?", main.hasObservers());
340 assertTrue("Other no observers?", other.hasObservers());
341
342 other.onSuccess(1);
343
344 assertFalse("Main has observers?", main.hasObservers());
345 assertFalse("Other has observers?", other.hasObservers());
346
347 to.assertFailure(CancellationException.class);
348 }
349
350 @Test
351 public void untilSingleOtherError() {
352 SingleSubject<Integer> main = SingleSubject.create();
353 SingleSubject<Integer> other = SingleSubject.create();
354
355 TestObserver<Integer> to = main.takeUntil(other).test();
356
357 assertTrue("Main no observers?", main.hasObservers());
358 assertTrue("Other no observers?", other.hasObservers());
359
360 other.onError(new TestException());
361
362 assertFalse("Main has observers?", main.hasObservers());
363 assertFalse("Other has observers?", other.hasObservers());
364
365 to.assertFailure(TestException.class);
366 }
367
368 @Test
369 public void untilSingleDispose() {
370 SingleSubject<Integer> main = SingleSubject.create();
371 SingleSubject<Integer> other = SingleSubject.create();
372
373 TestObserver<Integer> to = main.takeUntil(other).test();
374
375 assertTrue("Main no observers?", main.hasObservers());
376 assertTrue("Other no observers?", other.hasObservers());
377
378 to.dispose();
379
380 assertFalse("Main has observers?", main.hasObservers());
381 assertFalse("Other has observers?", other.hasObservers());
382
383 to.assertEmpty();
384 }
385
386 @Test
387 public void untilPublisherMainSuccess() {
388 SingleSubject<Integer> main = SingleSubject.create();
389 PublishProcessor<Integer> other = PublishProcessor.create();
390
391 TestObserver<Integer> to = main.takeUntil(other).test();
392
393 assertTrue("Main no observers?", main.hasObservers());
394 assertTrue("Other no observers?", other.hasSubscribers());
395
396 main.onSuccess(1);
397
398 assertFalse("Main has observers?", main.hasObservers());
399 assertFalse("Other has observers?", other.hasSubscribers());
400
401 to.assertResult(1);
402 }
403
404 @Test
405 public void untilPublisherMainError() {
406 SingleSubject<Integer> main = SingleSubject.create();
407 PublishProcessor<Integer> other = PublishProcessor.create();
408
409 TestObserver<Integer> to = main.takeUntil(other).test();
410
411 assertTrue("Main no observers?", main.hasObservers());
412 assertTrue("Other no observers?", other.hasSubscribers());
413
414 main.onError(new TestException());
415
416 assertFalse("Main has observers?", main.hasObservers());
417 assertFalse("Other has observers?", other.hasSubscribers());
418
419 to.assertFailure(TestException.class);
420 }
421
422 @Test
423 public void untilPublisherOtherOnNext() {
424 SingleSubject<Integer> main = SingleSubject.create();
425 PublishProcessor<Integer> other = PublishProcessor.create();
426
427 TestObserver<Integer> to = main.takeUntil(other).test();
428
429 assertTrue("Main no observers?", main.hasObservers());
430 assertTrue("Other no observers?", other.hasSubscribers());
431
432 other.onNext(1);
433
434 assertFalse("Main has observers?", main.hasObservers());
435 assertFalse("Other has observers?", other.hasSubscribers());
436
437 to.assertFailure(CancellationException.class);
438 }
439
440 @Test
441 public void untilPublisherOtherOnComplete() {
442 SingleSubject<Integer> main = SingleSubject.create();
443 PublishProcessor<Integer> other = PublishProcessor.create();
444
445 TestObserver<Integer> to = main.takeUntil(other).test();
446
447 assertTrue("Main no observers?", main.hasObservers());
448 assertTrue("Other no observers?", other.hasSubscribers());
449
450 other.onComplete();
451
452 assertFalse("Main has observers?", main.hasObservers());
453 assertFalse("Other has observers?", other.hasSubscribers());
454
455 to.assertFailure(CancellationException.class);
456 }
457
458 @Test
459 public void untilPublisherOtherError() {
460 SingleSubject<Integer> main = SingleSubject.create();
461 PublishProcessor<Integer> other = PublishProcessor.create();
462
463 TestObserver<Integer> to = main.takeUntil(other).test();
464
465 assertTrue("Main no observers?", main.hasObservers());
466 assertTrue("Other no observers?", other.hasSubscribers());
467
468 other.onError(new TestException());
469
470 assertFalse("Main has observers?", main.hasObservers());
471 assertFalse("Other has observers?", other.hasSubscribers());
472
473 to.assertFailure(TestException.class);
474 }
475
476 @Test
477 public void untilPublisherDispose() {
478 SingleSubject<Integer> main = SingleSubject.create();
479 PublishProcessor<Integer> other = PublishProcessor.create();
480
481 TestObserver<Integer> to = main.takeUntil(other).test();
482
483 assertTrue("Main no observers?", main.hasObservers());
484 assertTrue("Other no observers?", other.hasSubscribers());
485
486 to.dispose();
487
488 assertFalse("Main has observers?", main.hasObservers());
489 assertFalse("Other has observers?", other.hasSubscribers());
490
491 to.assertEmpty();
492 }
493
494 @Test
495 public void untilCompletableMainSuccess() {
496 SingleSubject<Integer> main = SingleSubject.create();
497 CompletableSubject other = CompletableSubject.create();
498
499 TestObserver<Integer> to = main.takeUntil(other).test();
500
501 assertTrue("Main no observers?", main.hasObservers());
502 assertTrue("Other no observers?", other.hasObservers());
503
504 main.onSuccess(1);
505
506 assertFalse("Main has observers?", main.hasObservers());
507 assertFalse("Other has observers?", other.hasObservers());
508
509 to.assertResult(1);
510 }
511
512 @Test
513 public void untilCompletableMainError() {
514 SingleSubject<Integer> main = SingleSubject.create();
515 CompletableSubject other = CompletableSubject.create();
516
517 TestObserver<Integer> to = main.takeUntil(other).test();
518
519 assertTrue("Main no observers?", main.hasObservers());
520 assertTrue("Other no observers?", other.hasObservers());
521
522 main.onError(new TestException());
523
524 assertFalse("Main has observers?", main.hasObservers());
525 assertFalse("Other has observers?", other.hasObservers());
526
527 to.assertFailure(TestException.class);
528 }
529
530 @Test
531 public void untilCompletableOtherOnComplete() {
532 SingleSubject<Integer> main = SingleSubject.create();
533 CompletableSubject other = CompletableSubject.create();
534
535 TestObserver<Integer> to = main.takeUntil(other).test();
536
537 assertTrue("Main no observers?", main.hasObservers());
538 assertTrue("Other no observers?", other.hasObservers());
539
540 other.onComplete();
541
542 assertFalse("Main has observers?", main.hasObservers());
543 assertFalse("Other has observers?", other.hasObservers());
544
545 to.assertFailure(CancellationException.class);
546 }
547
548 @Test
549 public void untilCompletableOtherError() {
550 SingleSubject<Integer> main = SingleSubject.create();
551 CompletableSubject other = CompletableSubject.create();
552
553 TestObserver<Integer> to = main.takeUntil(other).test();
554
555 assertTrue("Main no observers?", main.hasObservers());
556 assertTrue("Other no observers?", other.hasObservers());
557
558 other.onError(new TestException());
559
560 assertFalse("Main has observers?", main.hasObservers());
561 assertFalse("Other has observers?", other.hasObservers());
562
563 to.assertFailure(TestException.class);
564 }
565
566 @Test
567 public void untilCompletableDispose() {
568 SingleSubject<Integer> main = SingleSubject.create();
569 CompletableSubject other = CompletableSubject.create();
570
571 TestObserver<Integer> to = main.takeUntil(other).test();
572
573 assertTrue("Main no observers?", main.hasObservers());
574 assertTrue("Other no observers?", other.hasObservers());
575
576 to.dispose();
577
578 assertFalse("Main has observers?", main.hasObservers());
579 assertFalse("Other has observers?", other.hasObservers());
580
581 to.assertEmpty();
582 }
583 }