| 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.assertEquals; |
| 17 |
|
| 18 |
import java.util.List; |
| 19 |
|
| 20 |
import org.junit.Test; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.core.*; |
| 23 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 24 |
import io.reactivex.rxjava3.functions.*; |
| 25 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 26 |
import io.reactivex.rxjava3.subjects.PublishSubject; |
| 27 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 28 |
|
| 29 |
public class SingleDoFinallyTest extends RxJavaTest implements Action { |
| 30 |
|
| 31 |
int calls; |
| 32 |
|
| 33 |
@Override |
| 34 |
public void run() throws Exception { |
| 35 |
calls++; |
| 36 |
} |
| 37 |
|
| 38 |
@Test |
| 39 |
public void normalJust() { |
| 40 |
Single.just(1) |
| 41 |
.doFinally(this) |
| 42 |
.test() |
| 43 |
.assertResult(1); |
| 44 |
|
| 45 |
assertEquals(1, calls); |
| 46 |
} |
| 47 |
|
| 48 |
@Test |
| 49 |
public void normalError() { |
| 50 |
Single.error(new TestException()) |
| 51 |
.doFinally(this) |
| 52 |
.test() |
| 53 |
.assertFailure(TestException.class); |
| 54 |
|
| 55 |
assertEquals(1, calls); |
| 56 |
} |
| 57 |
|
| 58 |
@Test |
| 59 |
public void doubleOnSubscribe() { |
| 60 |
TestHelper.checkDoubleOnSubscribeSingle(new Function<Single<Object>, Single<Object>>() { |
| 61 |
@Override |
| 62 |
public Single<Object> apply(Single<Object> f) throws Exception { |
| 63 |
return f.doFinally(SingleDoFinallyTest.this); |
| 64 |
} |
| 65 |
}); |
| 66 |
} |
| 67 |
|
| 68 |
@Test |
| 69 |
public void actionThrows() { |
| 70 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 71 |
try { |
| 72 |
Single.just(1) |
| 73 |
.doFinally(new Action() { |
| 74 |
@Override |
| 75 |
public void run() throws Exception { |
| 76 |
throw new TestException(); |
| 77 |
} |
| 78 |
}) |
| 79 |
.test() |
| 80 |
.assertResult(1) |
| 81 |
.dispose(); |
| 82 |
|
| 83 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 84 |
} finally { |
| 85 |
RxJavaPlugins.reset(); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
@Test |
| 90 |
public void disposed() { |
| 91 |
TestHelper.checkDisposed(PublishSubject.create().singleOrError().doFinally(this)); |
| 92 |
} |
| 93 |
} |