Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/completable/CompletableDoOnTest.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.completable;
15
16 import static org.junit.Assert.*;
17
18 import java.util.List;
19 import java.util.concurrent.atomic.AtomicBoolean;
20
21 import org.junit.Test;
22
23 import io.reactivex.rxjava3.core.*;
24 import io.reactivex.rxjava3.disposables.*;
25 import io.reactivex.rxjava3.exceptions.*;
26 import io.reactivex.rxjava3.functions.*;
27 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
28 import io.reactivex.rxjava3.testsupport.*;
29
30 public class CompletableDoOnTest extends RxJavaTest {
31
32 @Test
33 public void successAcceptThrows() {
34 Completable.complete().doOnEvent(new Consumer<Throwable>() {
35 @Override
36 public void accept(Throwable e) throws Exception {
37 throw new TestException();
38 }
39 })
40 .test()
41 .assertFailure(TestException.class);
42 }
43
44 @Test
45 public void errorAcceptThrows() {
46 TestObserverEx<Void> to = Completable.error(new TestException("Outer")).doOnEvent(new Consumer<Throwable>() {
47 @Override
48 public void accept(Throwable e) throws Exception {
49 throw new TestException("Inner");
50 }
51 })
52 .to(TestHelper.<Void>testConsumer())
53 .assertFailure(CompositeException.class);
54
55 List<Throwable> errors = TestHelper.compositeList(to.errors().get(0));
56
57 TestHelper.assertError(errors, 0, TestException.class, "Outer");
58 TestHelper.assertError(errors, 1, TestException.class, "Inner");
59 }
60
61 @Test
62 public void doOnDisposeCalled() {
63 final AtomicBoolean atomicBoolean = new AtomicBoolean();
64
65 assertFalse(atomicBoolean.get());
66
67 Completable.complete()
68 .doOnDispose(new Action() {
69 @Override
70 public void run() throws Exception {
71 atomicBoolean.set(true);
72 }
73 })
74 .test()
75 .assertResult()
76 .dispose();
77
78 assertTrue(atomicBoolean.get());
79 }
80
81 @Test
82 public void onSubscribeCrash() {
83 List<Throwable> errors = TestHelper.trackPluginErrors();
84 try {
85 final Disposable bs = Disposable.empty();
86
87 new Completable() {
88 @Override
89 protected void subscribeActual(CompletableObserver observer) {
90 observer.onSubscribe(bs);
91 observer.onError(new TestException("Second"));
92 observer.onComplete();
93 }
94 }
95 .doOnSubscribe(new Consumer<Disposable>() {
96 @Override
97 public void accept(Disposable d) throws Exception {
98 throw new TestException("First");
99 }
100 })
101 .to(TestHelper.<Void>testConsumer())
102 .assertFailureAndMessage(TestException.class, "First");
103
104 assertTrue(bs.isDisposed());
105
106 TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second");
107 } finally {
108 RxJavaPlugins.reset();
109 }
110 }
111 }