Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/subscribers/FlowableConsumersTest.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.subscribers;
31
32 import static org.junit.Assert.*;
33
34 import java.io.IOException;
35 import java.util.*;
36
37 import org.junit.Test;
38 import org.reactivestreams.Subscriber;
39
40 import io.reactivex.rxjava3.core.*;
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.internal.subscriptions.BooleanSubscription;
46 import io.reactivex.rxjava3.observers.LambdaConsumerIntrospection;
47 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
48 import io.reactivex.rxjava3.processors.PublishProcessor;
49 import io.reactivex.rxjava3.testsupport.TestHelper;
50
51 public class FlowableConsumersTest implements Consumer<Object>, Action {
52
53 final CompositeDisposable composite = new CompositeDisposable();
54
55 final PublishProcessor<Integer> processor = PublishProcessor.create();
56
57 final List<Object> events = new ArrayList<>();
58
59 @Override
60 public void run() throws Exception {
61 events.add("OnComplete");
62 }
63
64 @Override
65 public void accept(Object t) throws Exception {
66 events.add(t);
67 }
68
69 static <T> Disposable subscribeAutoDispose(Flowable<T> source, CompositeDisposable composite,
70 Consumer<? super T> onNext, Consumer<? super Throwable> onError, Action onComplete) {
71 return source.subscribe(onNext, onError, onComplete, composite);
72 }
73
74 @Test
75 public void onNextNormal() {
76
77 Disposable d = subscribeAutoDispose(processor, composite, this, Functions.ON_ERROR_MISSING, () -> { });
78
79 assertFalse(d.getClass().toString(), ((LambdaConsumerIntrospection)d).hasCustomOnError());
80
81 assertTrue(composite.size() > 0);
82
83 assertTrue(events.toString(), events.isEmpty());
84
85 processor.onNext(1);
86
87 assertTrue(composite.size() > 0);
88
89 assertEquals(Arrays.<Object>asList(1), events);
90
91 processor.onComplete();
92
93 assertEquals(Arrays.<Object>asList(1), events);
94
95 assertEquals(0, composite.size());
96 }
97
98 @Test
99 public void onErrorNormal() {
100
101 subscribeAutoDispose(processor, composite, this, this, () -> { });
102
103 assertTrue(composite.size() > 0);
104
105 assertTrue(events.toString(), events.isEmpty());
106
107 processor.onNext(1);
108
109 assertTrue(composite.size() > 0);
110
111 assertEquals(Arrays.<Object>asList(1), events);
112
113 processor.onComplete();
114
115 assertEquals(Arrays.<Object>asList(1), events);
116
117 assertEquals(0, composite.size());
118 }
119
120 @Test
121 public void onErrorError() {
122
123 Disposable d = subscribeAutoDispose(processor, composite, this, this, this);
124
125 assertTrue(d.getClass().toString(), ((LambdaConsumerIntrospection)d).hasCustomOnError());
126
127 assertTrue(composite.size() > 0);
128
129 assertTrue(events.toString(), events.isEmpty());
130
131 processor.onNext(1);
132
133 assertTrue(composite.size() > 0);
134
135 assertEquals(Arrays.<Object>asList(1), events);
136
137 processor.onError(new IOException());
138
139 assertEquals(events.toString(), 1, events.get(0));
140 assertTrue(events.toString(), events.get(1) instanceof IOException);
141
142 assertEquals(0, composite.size());
143 }
144
145 @Test
146 public void onCompleteNormal() {
147
148 subscribeAutoDispose(processor, composite, this, this, this);
149
150 assertTrue(composite.size() > 0);
151
152 assertTrue(events.toString(), events.isEmpty());
153
154 processor.onNext(1);
155
156 assertTrue(composite.size() > 0);
157
158 assertEquals(Arrays.<Object>asList(1), events);
159
160 processor.onComplete();
161
162 assertEquals(Arrays.<Object>asList(1, "OnComplete"), events);
163
164 assertEquals(0, composite.size());
165 }
166
167 @Test
168 public void onCompleteError() {
169
170 subscribeAutoDispose(processor, composite, this, this, this);
171
172 assertTrue(composite.size() > 0);
173
174 assertTrue(events.toString(), events.isEmpty());
175
176 processor.onNext(1);
177
178 assertTrue(composite.size() > 0);
179
180 assertEquals(Arrays.<Object>asList(1), events);
181
182 processor.onError(new IOException());
183
184 assertEquals(events.toString(), 1, events.get(0));
185 assertTrue(events.toString(), events.get(1) instanceof IOException);
186
187 assertEquals(0, composite.size());
188 }
189
190 @Test
191 public void onCompleteDispose() {
192
193 Disposable d = subscribeAutoDispose(processor, composite, this, this, this);
194
195 assertTrue(composite.size() > 0);
196
197 assertTrue(events.toString(), events.isEmpty());
198
199 assertFalse(d.isDisposed());
200
201 d.dispose();
202 d.dispose();
203
204 assertTrue(d.isDisposed());
205
206 assertEquals(0, composite.size());
207
208 assertFalse(processor.hasSubscribers());
209 }
210
211 @Test
212 public void onNextCrash() {
213 List<Throwable> errors = TestHelper.trackPluginErrors();
214 try {
215 subscribeAutoDispose(processor, composite, new Consumer<Object>() {
216 @Override
217 public void accept(Object t) throws Exception {
218 throw new IOException();
219 }
220 }, this, this);
221
222 processor.onNext(1);
223
224 assertTrue(errors.toString(), errors.isEmpty());
225
226 assertTrue(events.toString(), events.get(0) instanceof IOException);
227 } finally {
228 RxJavaPlugins.reset();
229 }
230 }
231
232 @Test
233 public void onNextCrashOnError() {
234 List<Throwable> errors = TestHelper.trackPluginErrors();
235 try {
236 subscribeAutoDispose(processor, composite, this, new Consumer<Throwable>() {
237 @Override
238 public void accept(Throwable t) throws Exception {
239 throw new IOException(t);
240 }
241 }, this);
242
243 processor.onError(new IllegalArgumentException());
244
245 assertTrue(events.toString(), events.isEmpty());
246
247 TestHelper.assertError(errors, 0, CompositeException.class);
248 List<Throwable> inners = TestHelper.compositeList(errors.get(0));
249 TestHelper.assertError(inners, 0, IllegalArgumentException.class);
250 TestHelper.assertError(inners, 1, IOException.class);
251 } finally {
252 RxJavaPlugins.reset();
253 }
254 }
255
256 @Test
257 public void onNextCrashNoError() {
258 List<Throwable> errors = TestHelper.trackPluginErrors();
259 try {
260 subscribeAutoDispose(processor, composite, new Consumer<Object>() {
261 @Override
262 public void accept(Object t) throws Exception {
263 throw new IOException();
264 }
265 }, Functions.ON_ERROR_MISSING, () -> { });
266
267 processor.onNext(1);
268
269 assertTrue(events.toString(), events.isEmpty());
270
271 TestHelper.assertError(errors, 0, OnErrorNotImplementedException.class);
272 assertTrue(errors.get(0).getCause() instanceof IOException);
273 } finally {
274 RxJavaPlugins.reset();
275 }
276 }
277
278 @Test
279 public void onCompleteCrash() {
280 List<Throwable> errors = TestHelper.trackPluginErrors();
281 try {
282 subscribeAutoDispose(processor, composite, this, this, new Action() {
283 @Override
284 public void run() throws Exception {
285 throw new IOException();
286 }
287 });
288
289 processor.onNext(1);
290 processor.onComplete();
291
292 assertEquals(Arrays.asList(1), events);
293
294 TestHelper.assertUndeliverable(errors, 0, IOException.class);
295 } finally {
296 RxJavaPlugins.reset();
297 }
298 }
299
300 @Test
301 public void badSource() {
302 List<Throwable> errors = TestHelper.trackPluginErrors();
303 try {
304 subscribeAutoDispose(
305 new Flowable<Integer>() {
306 @Override
307 protected void subscribeActual(
308 Subscriber<? super Integer> s) {
309 s.onSubscribe(new BooleanSubscription());
310 s.onNext(1);
311 s.onComplete();
312
313 s.onSubscribe(new BooleanSubscription());
314 s.onNext(2);
315 s.onComplete();
316 s.onError(new IOException());
317 }
318 }, composite, this, this, this
319 );
320
321 assertEquals(Arrays.<Object>asList(1, "OnComplete"), events);
322
323 TestHelper.assertUndeliverable(errors, 0, IOException.class);
324 } finally {
325 RxJavaPlugins.reset();
326 }
327 }
328 }