Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableConcatWithCompletableTest.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.flowable;
15
16 import static org.junit.Assert.*;
17
18 import java.util.List;
19
20 import org.junit.Test;
21 import org.reactivestreams.Subscriber;
22
23 import io.reactivex.rxjava3.core.*;
24 import io.reactivex.rxjava3.exceptions.*;
25 import io.reactivex.rxjava3.functions.Action;
26 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
27 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
28 import io.reactivex.rxjava3.subjects.CompletableSubject;
29 import io.reactivex.rxjava3.subscribers.TestSubscriber;
30 import io.reactivex.rxjava3.testsupport.TestHelper;
31
32 public class FlowableConcatWithCompletableTest extends RxJavaTest {
33
34 @Test
35 public void normal() {
36 final TestSubscriber<Integer> ts = new TestSubscriber<>();
37
38 Flowable.range(1, 5)
39 .concatWith(Completable.fromAction(new Action() {
40 @Override
41 public void run() throws Exception {
42 ts.onNext(100);
43 }
44 }))
45 .subscribe(ts);
46
47 ts.assertResult(1, 2, 3, 4, 5, 100);
48 }
49
50 @Test
51 public void mainError() {
52 final TestSubscriber<Integer> ts = new TestSubscriber<>();
53
54 Flowable.<Integer>error(new TestException())
55 .concatWith(Completable.fromAction(new Action() {
56 @Override
57 public void run() throws Exception {
58 ts.onNext(100);
59 }
60 }))
61 .subscribe(ts);
62
63 ts.assertFailure(TestException.class);
64 }
65
66 @Test
67 public void otherError() {
68 final TestSubscriber<Integer> ts = new TestSubscriber<>();
69
70 Flowable.range(1, 5)
71 .concatWith(Completable.error(new TestException()))
72 .subscribe(ts);
73
74 ts.assertFailure(TestException.class, 1, 2, 3, 4, 5);
75 }
76
77 @Test
78 public void takeMain() {
79 final TestSubscriber<Integer> ts = new TestSubscriber<>();
80
81 Flowable.range(1, 5)
82 .concatWith(Completable.fromAction(new Action() {
83 @Override
84 public void run() throws Exception {
85 ts.onNext(100);
86 }
87 }))
88 .take(3)
89 .subscribe(ts);
90
91 ts.assertResult(1, 2, 3);
92 }
93
94 @Test
95 public void cancelOther() {
96 CompletableSubject other = CompletableSubject.create();
97
98 TestSubscriber<Object> ts = Flowable.empty()
99 .concatWith(other)
100 .test();
101
102 assertTrue(other.hasObservers());
103
104 ts.cancel();
105
106 assertFalse(other.hasObservers());
107 }
108
109 @Test
110 public void badSource() {
111 List<Throwable> errors = TestHelper.trackPluginErrors();
112 try {
113 new Flowable<Integer>() {
114 @Override
115 protected void subscribeActual(Subscriber<? super Integer> s) {
116 BooleanSubscription bs1 = new BooleanSubscription();
117 s.onSubscribe(bs1);
118
119 BooleanSubscription bs2 = new BooleanSubscription();
120 s.onSubscribe(bs2);
121
122 assertFalse(bs1.isCancelled());
123 assertTrue(bs2.isCancelled());
124
125 s.onComplete();
126 }
127 }.concatWith(Completable.complete())
128 .test()
129 .assertResult();
130
131 TestHelper.assertError(errors, 0, ProtocolViolationException.class);
132 } finally {
133 RxJavaPlugins.reset();
134 }
135 }
136 }