| 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 |
import static org.mockito.Mockito.*; |
| 18 |
|
| 19 |
import org.junit.Test; |
| 20 |
|
| 21 |
import io.reactivex.rxjava3.core.*; |
| 22 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 23 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 24 |
import io.reactivex.rxjava3.functions.*; |
| 25 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 26 |
import io.reactivex.rxjava3.subjects.SingleSubject; |
| 27 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 28 |
|
| 29 |
public class SingleDoOnLifecycleTest extends RxJavaTest { |
| 30 |
|
| 31 |
@Test |
| 32 |
public void success() throws Throwable { |
| 33 |
@SuppressWarnings("unchecked") |
| 34 |
Consumer<? super Disposable> onSubscribe = mock(Consumer.class); |
| 35 |
Action onDispose = mock(Action.class); |
| 36 |
|
| 37 |
Single.just(1) |
| 38 |
.doOnLifecycle(onSubscribe, onDispose) |
| 39 |
.test() |
| 40 |
.assertResult(1); |
| 41 |
|
| 42 |
verify(onSubscribe).accept(any()); |
| 43 |
verify(onDispose, never()).run(); |
| 44 |
} |
| 45 |
|
| 46 |
@Test |
| 47 |
public void error() throws Throwable { |
| 48 |
@SuppressWarnings("unchecked") |
| 49 |
Consumer<? super Disposable> onSubscribe = mock(Consumer.class); |
| 50 |
Action onDispose = mock(Action.class); |
| 51 |
|
| 52 |
Single.error(new TestException()) |
| 53 |
.doOnLifecycle(onSubscribe, onDispose) |
| 54 |
.test() |
| 55 |
.assertFailure(TestException.class); |
| 56 |
|
| 57 |
verify(onSubscribe).accept(any()); |
| 58 |
verify(onDispose, never()).run(); |
| 59 |
} |
| 60 |
|
| 61 |
@Test |
| 62 |
public void onSubscribeCrash() throws Throwable { |
| 63 |
TestHelper.withErrorTracking(errors -> { |
| 64 |
@SuppressWarnings("unchecked") |
| 65 |
Consumer<? super Disposable> onSubscribe = mock(Consumer.class); |
| 66 |
Action onDispose = mock(Action.class); |
| 67 |
|
| 68 |
doThrow(new TestException("First")).when(onSubscribe).accept(any()); |
| 69 |
|
| 70 |
Disposable bs = Disposable.empty(); |
| 71 |
|
| 72 |
new Single<Integer>() { |
| 73 |
@Override |
| 74 |
protected void subscribeActual(SingleObserver<? super Integer> observer) { |
| 75 |
observer.onSubscribe(bs); |
| 76 |
observer.onError(new TestException("Second")); |
| 77 |
observer.onSuccess(1); |
| 78 |
} |
| 79 |
} |
| 80 |
.doOnLifecycle(onSubscribe, onDispose) |
| 81 |
.to(TestHelper.<Integer>testConsumer()) |
| 82 |
.assertFailureAndMessage(TestException.class, "First"); |
| 83 |
|
| 84 |
assertTrue(bs.isDisposed()); |
| 85 |
|
| 86 |
TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second"); |
| 87 |
|
| 88 |
verify(onSubscribe).accept(any()); |
| 89 |
verify(onDispose, never()).run(); |
| 90 |
}); |
| 91 |
} |
| 92 |
|
| 93 |
@Test |
| 94 |
public void onDisposeCrash() throws Throwable { |
| 95 |
TestHelper.withErrorTracking(errors -> { |
| 96 |
@SuppressWarnings("unchecked") |
| 97 |
Consumer<? super Disposable> onSubscribe = mock(Consumer.class); |
| 98 |
Action onDispose = mock(Action.class); |
| 99 |
|
| 100 |
doThrow(new TestException("First")).when(onDispose).run(); |
| 101 |
|
| 102 |
SingleSubject<Integer> ss = SingleSubject.create(); |
| 103 |
|
| 104 |
TestObserver<Integer> to = ss |
| 105 |
.doOnLifecycle(onSubscribe, onDispose) |
| 106 |
.test(); |
| 107 |
|
| 108 |
assertTrue(ss.hasObservers()); |
| 109 |
|
| 110 |
to.dispose(); |
| 111 |
|
| 112 |
assertFalse(ss.hasObservers()); |
| 113 |
|
| 114 |
TestHelper.assertUndeliverable(errors, 0, TestException.class, "First"); |
| 115 |
|
| 116 |
verify(onSubscribe).accept(any()); |
| 117 |
verify(onDispose).run(); |
| 118 |
}); |
| 119 |
} |
| 120 |
|
| 121 |
@Test |
| 122 |
public void dispose() throws Throwable { |
| 123 |
@SuppressWarnings("unchecked") |
| 124 |
Consumer<? super Disposable> onSubscribe = mock(Consumer.class); |
| 125 |
Action onDispose = mock(Action.class); |
| 126 |
|
| 127 |
SingleSubject<Integer> ss = SingleSubject.create(); |
| 128 |
|
| 129 |
TestObserver<Integer> to = ss |
| 130 |
.doOnLifecycle(onSubscribe, onDispose) |
| 131 |
.test(); |
| 132 |
|
| 133 |
assertTrue(ss.hasObservers()); |
| 134 |
|
| 135 |
to.dispose(); |
| 136 |
|
| 137 |
assertFalse(ss.hasObservers()); |
| 138 |
|
| 139 |
verify(onSubscribe).accept(any()); |
| 140 |
verify(onDispose).run(); |
| 141 |
} |
| 142 |
|
| 143 |
@Test |
| 144 |
public void isDisposed() { |
| 145 |
TestHelper.checkDisposed(SingleSubject.create().doOnLifecycle(d -> { }, () -> { })); |
| 146 |
} |
| 147 |
|
| 148 |
@Test |
| 149 |
public void doubleOnSubscribe() { |
| 150 |
TestHelper.checkDoubleOnSubscribeSingle(m -> m.doOnLifecycle(d -> { }, () -> { })); |
| 151 |
} |
| 152 |
} |