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