| 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.assertTrue; |
| 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.exceptions.*; |
| 25 |
import io.reactivex.rxjava3.functions.Action; |
| 26 |
import io.reactivex.rxjava3.testsupport.*; |
| 27 |
|
| 28 |
public class SingleDoOnTerminateTest extends RxJavaTest { |
| 29 |
|
| 30 |
@Test |
| 31 |
public void doOnTerminateSuccess() { |
| 32 |
final AtomicBoolean atomicBoolean = new AtomicBoolean(); |
| 33 |
|
| 34 |
Single.just(1).doOnTerminate(new Action() { |
| 35 |
@Override |
| 36 |
public void run() throws Exception { |
| 37 |
atomicBoolean.set(true); |
| 38 |
} |
| 39 |
}) |
| 40 |
.test() |
| 41 |
.assertResult(1); |
| 42 |
|
| 43 |
assertTrue(atomicBoolean.get()); |
| 44 |
} |
| 45 |
|
| 46 |
@Test |
| 47 |
public void doOnTerminateError() { |
| 48 |
final AtomicBoolean atomicBoolean = new AtomicBoolean(); |
| 49 |
Single.error(new TestException()).doOnTerminate(new Action() { |
| 50 |
@Override |
| 51 |
public void run() { |
| 52 |
atomicBoolean.set(true); |
| 53 |
} |
| 54 |
}) |
| 55 |
.test() |
| 56 |
.assertFailure(TestException.class); |
| 57 |
|
| 58 |
assertTrue(atomicBoolean.get()); |
| 59 |
} |
| 60 |
|
| 61 |
@Test |
| 62 |
public void doOnTerminateSuccessCrash() { |
| 63 |
Single.just(1).doOnTerminate(new Action() { |
| 64 |
@Override |
| 65 |
public void run() throws Exception { |
| 66 |
throw new TestException(); |
| 67 |
} |
| 68 |
}) |
| 69 |
.test() |
| 70 |
.assertFailure(TestException.class); |
| 71 |
} |
| 72 |
|
| 73 |
@Test |
| 74 |
public void doOnTerminateErrorCrash() { |
| 75 |
TestObserverEx<Object> to = Single.error(new TestException("Outer")).doOnTerminate(new Action() { |
| 76 |
@Override |
| 77 |
public void run() { |
| 78 |
throw new TestException("Inner"); |
| 79 |
} |
| 80 |
}) |
| 81 |
.to(TestHelper.testConsumer()) |
| 82 |
.assertFailure(CompositeException.class); |
| 83 |
|
| 84 |
List<Throwable> errors = TestHelper.compositeList(to.errors().get(0)); |
| 85 |
|
| 86 |
TestHelper.assertError(errors, 0, TestException.class, "Outer"); |
| 87 |
TestHelper.assertError(errors, 1, TestException.class, "Inner"); |
| 88 |
} |
| 89 |
} |