Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/observers/ObservableConsumersTest.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 /*
15 * Copyright 2016-2019 David Karnok
16 *
17 * Licensed under the Apache License, Version 2.0 (the "License");
18 * you may not use this file except in compliance with the License.
19 * You may obtain a copy of the License at
20 *
21 * http://www.apache.org/licenses/LICENSE-2.0
22 *
23 * Unless required by applicable law or agreed to in writing, software
24 * distributed under the License is distributed on an "AS IS" BASIS,
25 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 * See the License for the specific language governing permissions and
27 * limitations under the License.
28 */
29
30 package io.reactivex.rxjava3.internal.observers;
31
32 import static org.junit.Assert.*;
33
34 import java.io.IOException;
35 import java.util.*;
36
37 import org.junit.Test;
38
39 import io.reactivex.rxjava3.core.Observable;
40 import io.reactivex.rxjava3.core.Observer;
41 import io.reactivex.rxjava3.disposables.*;
42 import io.reactivex.rxjava3.exceptions.*;
43 import io.reactivex.rxjava3.functions.*;
44 import io.reactivex.rxjava3.internal.functions.Functions;
45 import io.reactivex.rxjava3.observers.LambdaConsumerIntrospection;
46 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
47 import io.reactivex.rxjava3.subjects.PublishSubject;
48 import io.reactivex.rxjava3.testsupport.TestHelper;
49
50 public class ObservableConsumersTest implements Consumer<Object>, Action {
51
52 final CompositeDisposable composite = new CompositeDisposable();
53
54 final PublishSubject<Integer> processor = PublishSubject.create();
55
56 final List<Object> events = new ArrayList<>();
57
58 @Override
59 public void run() throws Exception {
60 events.add("OnComplete");
61 }
62
63 @Override
64 public void accept(Object t) throws Exception {
65 events.add(t);
66 }
67
68 static <T> Disposable subscribeAutoDispose(Observable<T> source, CompositeDisposable composite,
69 Consumer<? super T> onNext, Consumer<? super Throwable> onError, Action onComplete) {
70 return source.subscribe(onNext, onError, onComplete, composite);
71 }
72
73 @Test
74 public void onNextNormal() {
75
76 Disposable d = subscribeAutoDispose(processor, composite, this, Functions.ON_ERROR_MISSING, () -> { });
77
78 assertFalse(d.getClass().toString(), ((LambdaConsumerIntrospection)d).hasCustomOnError());
79
80 assertTrue(composite.size() > 0);
81
82 assertTrue(events.toString(), events.isEmpty());
83
84 processor.onNext(1);
85
86 assertTrue(composite.size() > 0);
87
88 assertEquals(Arrays.<Object>asList(1), events);
89
90 processor.onComplete();
91
92 assertEquals(Arrays.<Object>asList(1), events);
93
94 assertEquals(0, composite.size());
95 }
96
97 @Test
98 public void onErrorNormal() {
99
100 subscribeAutoDispose(processor, composite, this, Functions.ON_ERROR_MISSING, () -> { });
101
102 assertTrue(composite.size() > 0);
103
104 assertTrue(events.toString(), events.isEmpty());
105
106 processor.onNext(1);
107
108 assertTrue(composite.size() > 0);
109
110 assertEquals(Arrays.<Object>asList(1), events);
111
112 processor.onComplete();
113
114 assertEquals(Arrays.<Object>asList(1), events);
115
116 assertEquals(0, composite.size());
117 }
118
119 @Test
120 public void onErrorError() {
121
122 Disposable d = subscribeAutoDispose(processor, composite, this, this, this);
123
124 assertTrue(d.getClass().toString(), ((LambdaConsumerIntrospection)d).hasCustomOnError());
125
126 assertTrue(composite.size() > 0);
127
128 assertTrue(events.toString(), events.isEmpty());
129
130 processor.onNext(1);
131
132 assertTrue(composite.size() > 0);
133
134 assertEquals(Arrays.<Object>asList(1), events);
135
136 processor.onError(new IOException());
137
138 assertEquals(events.toString(), 1, events.get(0));
139 assertTrue(events.toString(), events.get(1) instanceof IOException);
140
141 assertEquals(0, composite.size());
142 }
143
144 @Test
145 public void onCompleteNormal() {
146
147 subscribeAutoDispose(processor, composite, this, this, this);
148
149 assertTrue(composite.size() > 0);
150
151 assertTrue(events.toString(), events.isEmpty());
152
153 processor.onNext(1);
154
155 assertTrue(composite.size() > 0);
156
157 assertEquals(Arrays.<Object>asList(1), events);
158
159 processor.onComplete();
160
161 assertEquals(Arrays.<Object>asList(1, "OnComplete"), events);
162
163 assertEquals(0, composite.size());
164 }
165
166 @Test
167 public void onCompleteError() {
168
169 subscribeAutoDispose(processor, composite, this, this, this);
170
171 assertTrue(composite.size() > 0);
172
173 assertTrue(events.toString(), events.isEmpty());
174
175 processor.onNext(1);
176
177 assertTrue(composite.size() > 0);
178
179 assertEquals(Arrays.<Object>asList(1), events);
180
181 processor.onError(new IOException());
182
183 assertEquals(events.toString(), 1, events.get(0));
184 assertTrue(events.toString(), events.get(1) instanceof IOException);
185
186 assertEquals(0, composite.size());
187 }
188
189 @Test
190 public void onCompleteDispose() {
191
192 Disposable d = subscribeAutoDispose(processor, composite, this, this, this);
193
194 assertTrue(composite.size() > 0);
195
196 assertTrue(events.toString(), events.isEmpty());
197
198 assertFalse(d.isDisposed());
199
200 d.dispose();
201 d.dispose();
202
203 assertTrue(d.isDisposed());
204
205 assertEquals(0, composite.size());
206
207 assertFalse(processor.hasObservers());
208 }
209
210 @Test
211 public void onNextCrash() {
212 List<Throwable> errors = TestHelper.trackPluginErrors();
213 try {
214 subscribeAutoDispose(processor, composite, new Consumer<Object>() {
215 @Override
216 public void accept(Object t) throws Exception {
217 throw new IOException();
218 }
219 }, this, this);
220
221 processor.onNext(1);
222
223 assertTrue(errors.toString(), errors.isEmpty());
224
225 assertTrue(events.toString(), events.get(0) instanceof IOException);
226 } finally {
227 RxJavaPlugins.reset();
228 }
229 }
230
231 @Test
232 public void onNextCrashOnError() {
233 List<Throwable> errors = TestHelper.trackPluginErrors();
234 try {
235 subscribeAutoDispose(processor, composite, this, new Consumer<Throwable>() {
236 @Override
237 public void accept(Throwable t) throws Exception {
238 throw new IOException(t);
239 }
240 }, this);
241
242 processor.onError(new IllegalArgumentException());
243
244 assertTrue(events.toString(), events.isEmpty());
245
246 TestHelper.assertError(errors, 0, CompositeException.class);
247 List<Throwable> inners = TestHelper.compositeList(errors.get(0));
248 TestHelper.assertError(inners, 0, IllegalArgumentException.class);
249 TestHelper.assertError(inners, 1, IOException.class);
250 } finally {
251 RxJavaPlugins.reset();
252 }
253 }
254
255 @Test
256 public void onNextCrashNoError() {
257 List<Throwable> errors = TestHelper.trackPluginErrors();
258 try {
259 subscribeAutoDispose(processor, composite, t -> {
260 throw new IOException();
261 }, Functions.ON_ERROR_MISSING, () -> { });
262
263 processor.onNext(1);
264
265 assertTrue(events.toString(), events.isEmpty());
266
267 TestHelper.assertError(errors, 0, OnErrorNotImplementedException.class);
268 assertTrue(errors.get(0).getCause() instanceof IOException);
269 } finally {
270 RxJavaPlugins.reset();
271 }
272 }
273
274 @Test
275 public void onCompleteCrash() {
276 List<Throwable> errors = TestHelper.trackPluginErrors();
277 try {
278 subscribeAutoDispose(processor, composite, this, this, new Action() {
279 @Override
280 public void run() throws Exception {
281 throw new IOException();
282 }
283 });
284
285 processor.onNext(1);
286 processor.onComplete();
287
288 assertEquals(Arrays.asList(1), events);
289
290 TestHelper.assertUndeliverable(errors, 0, IOException.class);
291 } finally {
292 RxJavaPlugins.reset();
293 }
294 }
295
296 @Test
297 public void badSource() {
298 List<Throwable> errors = TestHelper.trackPluginErrors();
299 try {
300 subscribeAutoDispose(
301 new Observable<Integer>() {
302 @Override
303 protected void subscribeActual(
304 Observer<? super Integer> observer) {
305 observer.onSubscribe(Disposable.empty());
306 observer.onNext(1);
307 observer.onComplete();
308
309 observer.onSubscribe(Disposable.empty());
310 observer.onNext(2);
311 observer.onComplete();
312 observer.onError(new IOException());
313 }
314 }, composite, this, this, this
315 );
316
317 assertEquals(Arrays.<Object>asList(1, "OnComplete"), events);
318
319 TestHelper.assertUndeliverable(errors, 0, IOException.class);
320 } finally {
321 RxJavaPlugins.reset();
322 }
323 }
324 }