Debian Codegraph @

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