Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/observers/MaybeConsumersTest.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.*;
40 import io.reactivex.rxjava3.disposables.*;
41 import io.reactivex.rxjava3.exceptions.CompositeException;
42 import io.reactivex.rxjava3.functions.*;
43 import io.reactivex.rxjava3.internal.functions.Functions;
44 import io.reactivex.rxjava3.observers.LambdaConsumerIntrospection;
45 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
46 import io.reactivex.rxjava3.subjects.MaybeSubject;
47 import io.reactivex.rxjava3.testsupport.TestHelper;
48
49 public class MaybeConsumersTest implements Consumer<Object>, Action {
50
51 final CompositeDisposable composite = new CompositeDisposable();
52
53 final MaybeSubject<Integer> processor = MaybeSubject.create();
54
55 final List<Object> events = new ArrayList<>();
56
57 @Override
58 public void run() throws Exception {
59 events.add("OnComplete");
60 }
61
62 @Override
63 public void accept(Object t) throws Exception {
64 events.add(t);
65 }
66
67 static <T> Disposable subscribeAutoDispose(Maybe<T> source, CompositeDisposable composite,
68 Consumer<? super T> onSuccess, Consumer<? super Throwable> onError, Action onComplete) {
69 return source.subscribe(onSuccess, onError, onComplete, composite);
70 }
71
72 @Test
73 public void onSuccessNormal() {
74
75 Disposable d = subscribeAutoDispose(processor, composite, this, Functions.ON_ERROR_MISSING, () -> { });
76
77 assertFalse(d.getClass().toString(), ((LambdaConsumerIntrospection)d).hasCustomOnError());
78
79 assertTrue(composite.size() > 0);
80
81 assertTrue(events.toString(), events.isEmpty());
82
83 processor.onSuccess(1);
84
85 assertEquals(0, composite.size());
86
87 assertEquals(Arrays.<Object>asList(1), events);
88
89 }
90
91 @Test
92 public void onErrorNormal() {
93
94 subscribeAutoDispose(processor, composite, this, this, this);
95
96 assertTrue(composite.size() > 0);
97
98 assertTrue(events.toString(), events.isEmpty());
99
100 processor.onSuccess(1);
101
102 assertEquals(0, composite.size());
103
104 assertEquals(Arrays.<Object>asList(1), events);
105
106 }
107
108 @Test
109 public void onErrorError() {
110
111 Disposable d = subscribeAutoDispose(processor, composite, this, this, this);
112
113 assertTrue(d.getClass().toString(), ((LambdaConsumerIntrospection)d).hasCustomOnError());
114
115 assertTrue(composite.size() > 0);
116
117 assertTrue(events.toString(), events.isEmpty());
118
119 processor.onError(new IOException());
120
121 assertTrue(events.toString(), events.get(0) instanceof IOException);
122
123 assertEquals(0, composite.size());
124 }
125
126 @Test
127 public void onCompleteNormal() {
128
129 subscribeAutoDispose(processor, composite, this, this, this);
130
131 assertTrue(composite.size() > 0);
132
133 assertTrue(events.toString(), events.isEmpty());
134
135 processor.onComplete();
136
137 assertEquals(0, composite.size());
138
139 assertEquals(Arrays.<Object>asList("OnComplete"), events);
140
141 }
142
143 @Test
144 public void onCompleteError() {
145
146 subscribeAutoDispose(processor, composite, this, this, this);
147
148 assertTrue(composite.size() > 0);
149
150 assertTrue(events.toString(), events.isEmpty());
151
152 processor.onError(new IOException());
153
154 assertTrue(events.toString(), events.get(0) instanceof IOException);
155
156 assertEquals(0, composite.size());
157 }
158
159 @Test
160 public void onCompleteDispose() {
161
162 Disposable d = subscribeAutoDispose(processor, composite, this, this, this);
163
164 assertTrue(composite.size() > 0);
165
166 assertTrue(events.toString(), events.isEmpty());
167
168 assertFalse(d.isDisposed());
169
170 d.dispose();
171 d.dispose();
172
173 assertTrue(d.isDisposed());
174
175 assertEquals(0, composite.size());
176
177 assertFalse(processor.hasObservers());
178 }
179
180 @Test
181 public void onSuccessCrash() {
182 List<Throwable> errors = TestHelper.trackPluginErrors();
183 try {
184 subscribeAutoDispose(processor, composite, new Consumer<Object>() {
185 @Override
186 public void accept(Object t) throws Exception {
187 throw new IOException();
188 }
189 }, this, this);
190
191 processor.onSuccess(1);
192
193 assertTrue(events.toString(), events.isEmpty());
194
195 TestHelper.assertUndeliverable(errors, 0, IOException.class);
196 } finally {
197 RxJavaPlugins.reset();
198 }
199 }
200
201 @Test
202 public void onErrorCrash() {
203 List<Throwable> errors = TestHelper.trackPluginErrors();
204 try {
205 subscribeAutoDispose(processor, composite, this, new Consumer<Throwable>() {
206 @Override
207 public void accept(Throwable t) throws Exception {
208 throw new IOException(t);
209 }
210 }, this);
211
212 processor.onError(new IllegalArgumentException());
213
214 assertTrue(events.toString(), events.isEmpty());
215
216 TestHelper.assertError(errors, 0, CompositeException.class);
217 List<Throwable> inners = TestHelper.compositeList(errors.get(0));
218 TestHelper.assertError(inners, 0, IllegalArgumentException.class);
219 TestHelper.assertError(inners, 1, IOException.class);
220 } finally {
221 RxJavaPlugins.reset();
222 }
223 }
224
225 @Test
226 public void onCompleteCrash() {
227 List<Throwable> errors = TestHelper.trackPluginErrors();
228 try {
229 subscribeAutoDispose(processor, composite, this, this, new Action() {
230 @Override
231 public void run() throws Exception {
232 throw new IOException();
233 }
234 });
235
236 processor.onComplete();
237
238 assertTrue(events.toString(), events.isEmpty());
239
240 TestHelper.assertUndeliverable(errors, 0, IOException.class);
241 } finally {
242 RxJavaPlugins.reset();
243 }
244 }
245
246 @Test
247 public void badSource() {
248 List<Throwable> errors = TestHelper.trackPluginErrors();
249 try {
250 subscribeAutoDispose(
251 new Maybe<Integer>() {
252 @Override
253 protected void subscribeActual(
254 MaybeObserver<? super Integer> observer) {
255 observer.onSubscribe(Disposable.empty());
256 observer.onComplete();
257
258 observer.onSubscribe(Disposable.empty());
259 observer.onSuccess(2);
260 observer.onComplete();
261 observer.onError(new IOException());
262 }
263 }, composite, this, this, this
264 );
265
266 assertEquals(Arrays.<Object>asList("OnComplete"), events);
267
268 TestHelper.assertUndeliverable(errors, 0, IOException.class);
269 } finally {
270 RxJavaPlugins.reset();
271 }
272 }
273 }