Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/single/SingleDoOnTest.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.single;
15
16 import static org.junit.Assert.*;
17
18 import java.util.List;
19
20 import org.junit.Test;
21
22 import io.reactivex.rxjava3.core.*;
23 import io.reactivex.rxjava3.disposables.*;
24 import io.reactivex.rxjava3.exceptions.*;
25 import io.reactivex.rxjava3.functions.*;
26 import io.reactivex.rxjava3.internal.functions.Functions;
27 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
28 import io.reactivex.rxjava3.subjects.PublishSubject;
29 import io.reactivex.rxjava3.testsupport.*;
30
31 public class SingleDoOnTest extends RxJavaTest {
32
33 @Test
34 public void doOnDispose() {
35 final int[] count = { 0 };
36
37 Single.never().doOnDispose(new Action() {
38 @Override
39 public void run() throws Exception {
40 count[0]++;
41 }
42 }).test(true);
43
44 assertEquals(1, count[0]);
45 }
46
47 @Test
48 public void doOnError() {
49 final Object[] event = { null };
50
51 Single.error(new TestException()).doOnError(new Consumer<Throwable>() {
52 @Override
53 public void accept(Throwable e) throws Exception {
54 event[0] = e;
55 }
56 })
57 .test();
58
59 assertTrue(event[0].toString(), event[0] instanceof TestException);
60 }
61
62 @Test
63 public void doOnSubscribe() {
64 final int[] count = { 0 };
65
66 Single.never().doOnSubscribe(new Consumer<Disposable>() {
67 @Override
68 public void accept(Disposable d) throws Exception {
69 count[0]++;
70 }
71 }).test();
72
73 assertEquals(1, count[0]);
74 }
75
76 @Test
77 public void doOnSuccess() {
78 final Object[] event = { null };
79
80 Single.just(1).doOnSuccess(new Consumer<Integer>() {
81 @Override
82 public void accept(Integer e) throws Exception {
83 event[0] = e;
84 }
85 })
86 .test();
87
88 assertEquals(1, event[0]);
89 }
90
91 @Test
92 public void doOnSubscribeNormal() {
93 final int[] count = { 0 };
94
95 Single.just(1).doOnSubscribe(new Consumer<Disposable>() {
96 @Override
97 public void accept(Disposable d) throws Exception {
98 count[0]++;
99 }
100 })
101 .test()
102 .assertResult(1);
103
104 assertEquals(1, count[0]);
105 }
106
107 @Test
108 public void doOnSubscribeError() {
109 final int[] count = { 0 };
110
111 Single.error(new TestException()).doOnSubscribe(new Consumer<Disposable>() {
112 @Override
113 public void accept(Disposable d) throws Exception {
114 count[0]++;
115 }
116 })
117 .test()
118 .assertFailure(TestException.class);
119
120 assertEquals(1, count[0]);
121 }
122
123 @Test
124 public void doOnSubscribeJustCrash() {
125
126 Single.just(1).doOnSubscribe(new Consumer<Disposable>() {
127 @Override
128 public void accept(Disposable d) throws Exception {
129 throw new TestException();
130 }
131 })
132 .test()
133 .assertFailure(TestException.class);
134 }
135
136 @Test
137 public void doOnSubscribeErrorCrash() {
138 List<Throwable> errors = TestHelper.trackPluginErrors();
139
140 try {
141 Single.error(new TestException("Outer")).doOnSubscribe(new Consumer<Disposable>() {
142 @Override
143 public void accept(Disposable d) throws Exception {
144 throw new TestException("Inner");
145 }
146 })
147 .to(TestHelper.testConsumer())
148 .assertFailureAndMessage(TestException.class, "Inner");
149
150 TestHelper.assertUndeliverable(errors, 0, TestException.class, "Outer");
151 } finally {
152 RxJavaPlugins.reset();
153 }
154
155 }
156
157 @Test
158 public void onErrorSuccess() {
159 final int[] call = { 0 };
160
161 Single.just(1)
162 .doOnError(new Consumer<Throwable>() {
163 @Override
164 public void accept(Throwable v) throws Exception {
165 call[0]++;
166 }
167 })
168 .test()
169 .assertResult(1);
170
171 assertEquals(0, call[0]);
172 }
173
174 @Test
175 public void onErrorCrashes() {
176 TestObserverEx<Object> to = Single.error(new TestException("Outer"))
177 .doOnError(new Consumer<Throwable>() {
178 @Override
179 public void accept(Throwable v) throws Exception {
180 throw new TestException("Inner");
181 }
182 })
183 .to(TestHelper.testConsumer())
184 .assertFailure(CompositeException.class);
185
186 List<Throwable> errors = TestHelper.compositeList(to.errors().get(0));
187
188 TestHelper.assertError(errors, 0, TestException.class, "Outer");
189 TestHelper.assertError(errors, 1, TestException.class, "Inner");
190 }
191
192 @Test
193 public void doOnEventThrowsSuccess() {
194 Single.just(1)
195 .doOnEvent(new BiConsumer<Integer, Throwable>() {
196 @Override
197 public void accept(Integer v, Throwable e) throws Exception {
198 throw new TestException();
199 }
200 })
201 .test()
202 .assertFailure(TestException.class);
203 }
204
205 @Test
206 public void doOnEventThrowsError() {
207 TestObserverEx<Integer> to = Single.<Integer>error(new TestException("Main"))
208 .doOnEvent(new BiConsumer<Integer, Throwable>() {
209 @Override
210 public void accept(Integer v, Throwable e) throws Exception {
211 throw new TestException("Inner");
212 }
213 })
214 .to(TestHelper.<Integer>testConsumer())
215 .assertFailure(CompositeException.class);
216
217 List<Throwable> errors = TestHelper.compositeList(to.errors().get(0));
218
219 TestHelper.assertError(errors, 0, TestException.class, "Main");
220 TestHelper.assertError(errors, 1, TestException.class, "Inner");
221 }
222
223 @Test
224 public void doOnDisposeDispose() {
225 final int[] calls = { 0 };
226 TestHelper.checkDisposed(PublishSubject.create().singleOrError().doOnDispose(new Action() {
227 @Override
228 public void run() throws Exception {
229 calls[0]++;
230 }
231 }));
232
233 assertEquals(1, calls[0]);
234 }
235
236 @Test
237 public void doOnDisposeSuccess() {
238 final int[] calls = { 0 };
239
240 Single.just(1)
241 .doOnDispose(new Action() {
242 @Override
243 public void run() throws Exception {
244 calls[0]++;
245 }
246 })
247 .test()
248 .assertResult(1);
249
250 assertEquals(0, calls[0]);
251 }
252
253 @Test
254 public void doOnDisposeError() {
255 final int[] calls = { 0 };
256
257 Single.error(new TestException())
258 .doOnDispose(new Action() {
259 @Override
260 public void run() throws Exception {
261 calls[0]++;
262 }
263 })
264 .test()
265 .assertFailure(TestException.class);
266
267 assertEquals(0, calls[0]);
268 }
269
270 @Test
271 public void doOnDisposeDoubleOnSubscribe() {
272 TestHelper.checkDoubleOnSubscribeSingle(new Function<Single<Object>, SingleSource<Object>>() {
273 @Override
274 public SingleSource<Object> apply(Single<Object> s) throws Exception {
275 return s.doOnDispose(Functions.EMPTY_ACTION);
276 }
277 });
278 }
279
280 @Test
281 public void doOnDisposeCrash() {
282 List<Throwable> errors = TestHelper.trackPluginErrors();
283 try {
284 PublishSubject<Integer> ps = PublishSubject.create();
285
286 ps.singleOrError().doOnDispose(new Action() {
287 @Override
288 public void run() throws Exception {
289 throw new TestException();
290 }
291 })
292 .test()
293 .dispose();
294
295 TestHelper.assertUndeliverable(errors, 0, TestException.class);
296 } finally {
297 RxJavaPlugins.reset();
298 }
299 }
300
301 @Test
302 public void doOnSuccessErrors() {
303 final int[] call = { 0 };
304
305 Single.error(new TestException())
306 .doOnSuccess(new Consumer<Object>() {
307 @Override
308 public void accept(Object v) throws Exception {
309 call[0]++;
310 }
311 })
312 .test()
313 .assertFailure(TestException.class);
314
315 assertEquals(0, call[0]);
316 }
317
318 @Test
319 public void doOnSuccessCrash() {
320 Single.just(1)
321 .doOnSuccess(new Consumer<Integer>() {
322 @Override
323 public void accept(Integer v) throws Exception {
324 throw new TestException();
325 }
326 })
327 .test()
328 .assertFailure(TestException.class);
329 }
330
331 @Test
332 public void onSubscribeCrash() {
333 List<Throwable> errors = TestHelper.trackPluginErrors();
334 try {
335 final Disposable bs = Disposable.empty();
336
337 new Single<Integer>() {
338 @Override
339 protected void subscribeActual(SingleObserver<? super Integer> observer) {
340 observer.onSubscribe(bs);
341 observer.onError(new TestException("Second"));
342 observer.onSuccess(1);
343 }
344 }
345 .doOnSubscribe(new Consumer<Disposable>() {
346 @Override
347 public void accept(Disposable d) throws Exception {
348 throw new TestException("First");
349 }
350 })
351 .to(TestHelper.<Integer>testConsumer())
352 .assertFailureAndMessage(TestException.class, "First");
353
354 assertTrue(bs.isDisposed());
355
356 TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second");
357 } finally {
358 RxJavaPlugins.reset();
359 }
360 }
361 }