| 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.maybe; |
| 15 |
|
| 16 |
import static org.junit.Assert.assertTrue; |
| 17 |
import static org.mockito.ArgumentMatchers.any; |
| 18 |
import static org.mockito.Mockito.*; |
| 19 |
|
| 20 |
import java.io.IOException; |
| 21 |
|
| 22 |
import org.junit.Test; |
| 23 |
import org.mockito.InOrder; |
| 24 |
|
| 25 |
import io.reactivex.rxjava3.annotations.NonNull; |
| 26 |
import io.reactivex.rxjava3.core.*; |
| 27 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 28 |
import io.reactivex.rxjava3.exceptions.*; |
| 29 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 30 |
|
| 31 |
public class MaybeSafeSubscribeTest { |
| 32 |
|
| 33 |
@Test |
| 34 |
public void normalSuccess() throws Throwable { |
| 35 |
TestHelper.withErrorTracking(errors -> { |
| 36 |
@SuppressWarnings("unchecked") |
| 37 |
MaybeObserver<Integer> consumer = mock(MaybeObserver.class); |
| 38 |
|
| 39 |
Maybe.just(1) |
| 40 |
.safeSubscribe(consumer); |
| 41 |
|
| 42 |
InOrder order = inOrder(consumer); |
| 43 |
order.verify(consumer).onSubscribe(any(Disposable.class)); |
| 44 |
order.verify(consumer).onSuccess(1); |
| 45 |
order.verifyNoMoreInteractions(); |
| 46 |
|
| 47 |
assertTrue("" + errors, errors.isEmpty()); |
| 48 |
}); |
| 49 |
} |
| 50 |
|
| 51 |
@Test |
| 52 |
public void normalError() throws Throwable { |
| 53 |
TestHelper.withErrorTracking(errors -> { |
| 54 |
@SuppressWarnings("unchecked") |
| 55 |
MaybeObserver<Integer> consumer = mock(MaybeObserver.class); |
| 56 |
|
| 57 |
Maybe.<Integer>error(new TestException()) |
| 58 |
.safeSubscribe(consumer); |
| 59 |
|
| 60 |
InOrder order = inOrder(consumer); |
| 61 |
order.verify(consumer).onSubscribe(any(Disposable.class)); |
| 62 |
order.verify(consumer).onError(any(TestException.class)); |
| 63 |
order.verifyNoMoreInteractions(); |
| 64 |
|
| 65 |
assertTrue("" + errors, errors.isEmpty()); |
| 66 |
}); |
| 67 |
} |
| 68 |
|
| 69 |
@Test |
| 70 |
public void normalEmpty() throws Throwable { |
| 71 |
TestHelper.withErrorTracking(errors -> { |
| 72 |
@SuppressWarnings("unchecked") |
| 73 |
MaybeObserver<Integer> consumer = mock(MaybeObserver.class); |
| 74 |
|
| 75 |
Maybe.<Integer>empty() |
| 76 |
.safeSubscribe(consumer); |
| 77 |
|
| 78 |
InOrder order = inOrder(consumer); |
| 79 |
order.verify(consumer).onSubscribe(any(Disposable.class)); |
| 80 |
order.verify(consumer).onComplete(); |
| 81 |
order.verifyNoMoreInteractions(); |
| 82 |
}); |
| 83 |
} |
| 84 |
|
| 85 |
@Test |
| 86 |
public void onSubscribeCrash() throws Throwable { |
| 87 |
TestHelper.withErrorTracking(errors -> { |
| 88 |
@SuppressWarnings("unchecked") |
| 89 |
MaybeObserver<Integer> consumer = mock(MaybeObserver.class); |
| 90 |
doThrow(new TestException()).when(consumer).onSubscribe(any()); |
| 91 |
|
| 92 |
Disposable d = Disposable.empty(); |
| 93 |
|
| 94 |
new Maybe<Integer>() { |
| 95 |
@Override |
| 96 |
protected void subscribeActual(@NonNull MaybeObserver<? super Integer> observer) { |
| 97 |
observer.onSubscribe(d); |
| 98 |
// none of the following should arrive at the consumer |
| 99 |
observer.onSuccess(1); |
| 100 |
observer.onError(new IOException()); |
| 101 |
observer.onComplete(); |
| 102 |
} |
| 103 |
} |
| 104 |
.safeSubscribe(consumer); |
| 105 |
|
| 106 |
InOrder order = inOrder(consumer); |
| 107 |
order.verify(consumer).onSubscribe(any(Disposable.class)); |
| 108 |
order.verifyNoMoreInteractions(); |
| 109 |
|
| 110 |
assertTrue(d.isDisposed()); |
| 111 |
|
| 112 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 113 |
TestHelper.assertUndeliverable(errors, 1, IOException.class); |
| 114 |
}); |
| 115 |
} |
| 116 |
|
| 117 |
@Test |
| 118 |
public void onSuccessCrash() throws Throwable { |
| 119 |
TestHelper.withErrorTracking(errors -> { |
| 120 |
@SuppressWarnings("unchecked") |
| 121 |
MaybeObserver<Integer> consumer = mock(MaybeObserver.class); |
| 122 |
doThrow(new TestException()).when(consumer).onSuccess(any()); |
| 123 |
|
| 124 |
new Maybe<Integer>() { |
| 125 |
@Override |
| 126 |
protected void subscribeActual(@NonNull MaybeObserver<? super Integer> observer) { |
| 127 |
observer.onSubscribe(Disposable.empty()); |
| 128 |
observer.onSuccess(1); |
| 129 |
} |
| 130 |
} |
| 131 |
.safeSubscribe(consumer); |
| 132 |
|
| 133 |
InOrder order = inOrder(consumer); |
| 134 |
order.verify(consumer).onSubscribe(any(Disposable.class)); |
| 135 |
order.verify(consumer).onSuccess(1); |
| 136 |
order.verifyNoMoreInteractions(); |
| 137 |
|
| 138 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 139 |
}); |
| 140 |
} |
| 141 |
|
| 142 |
@Test |
| 143 |
public void onErrorCrash() throws Throwable { |
| 144 |
TestHelper.withErrorTracking(errors -> { |
| 145 |
@SuppressWarnings("unchecked") |
| 146 |
MaybeObserver<Integer> consumer = mock(MaybeObserver.class); |
| 147 |
doThrow(new TestException()).when(consumer).onError(any()); |
| 148 |
|
| 149 |
new Maybe<Integer>() { |
| 150 |
@Override |
| 151 |
protected void subscribeActual(@NonNull MaybeObserver<? super Integer> observer) { |
| 152 |
observer.onSubscribe(Disposable.empty()); |
| 153 |
// none of the following should arrive at the consumer |
| 154 |
observer.onError(new IOException()); |
| 155 |
} |
| 156 |
} |
| 157 |
.safeSubscribe(consumer); |
| 158 |
|
| 159 |
InOrder order = inOrder(consumer); |
| 160 |
order.verify(consumer).onSubscribe(any(Disposable.class)); |
| 161 |
order.verify(consumer).onError(any(IOException.class)); |
| 162 |
order.verifyNoMoreInteractions(); |
| 163 |
|
| 164 |
TestHelper.assertError(errors, 0, CompositeException.class); |
| 165 |
|
| 166 |
CompositeException compositeException = (CompositeException)errors.get(0); |
| 167 |
TestHelper.assertError(compositeException.getExceptions(), 0, IOException.class); |
| 168 |
TestHelper.assertError(compositeException.getExceptions(), 1, TestException.class); |
| 169 |
}); |
| 170 |
} |
| 171 |
|
| 172 |
@Test |
| 173 |
public void onCompleteCrash() throws Throwable { |
| 174 |
TestHelper.withErrorTracking(errors -> { |
| 175 |
@SuppressWarnings("unchecked") |
| 176 |
MaybeObserver<Integer> consumer = mock(MaybeObserver.class); |
| 177 |
doThrow(new TestException()).when(consumer).onComplete(); |
| 178 |
|
| 179 |
new Maybe<Integer>() { |
| 180 |
@Override |
| 181 |
protected void subscribeActual(@NonNull MaybeObserver<? super Integer> observer) { |
| 182 |
observer.onSubscribe(Disposable.empty()); |
| 183 |
// none of the following should arrive at the consumer |
| 184 |
observer.onComplete(); |
| 185 |
} |
| 186 |
} |
| 187 |
.safeSubscribe(consumer); |
| 188 |
|
| 189 |
InOrder order = inOrder(consumer); |
| 190 |
order.verify(consumer).onSubscribe(any(Disposable.class)); |
| 191 |
order.verify(consumer).onComplete(); |
| 192 |
order.verifyNoMoreInteractions(); |
| 193 |
|
| 194 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 195 |
}); |
| 196 |
} |
| 197 |
} |