| 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.exceptions; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.io.IOException; |
| 19 |
import java.util.List; |
| 20 |
import java.util.concurrent.atomic.AtomicInteger; |
| 21 |
|
| 22 |
import org.junit.Test; |
| 23 |
|
| 24 |
import io.reactivex.rxjava3.core.*; |
| 25 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 26 |
import io.reactivex.rxjava3.functions.Consumer; |
| 27 |
import io.reactivex.rxjava3.internal.util.ExceptionHelper; |
| 28 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 29 |
import io.reactivex.rxjava3.subjects.PublishSubject; |
| 30 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 31 |
|
| 32 |
public class ExceptionsTest extends RxJavaTest { |
| 33 |
|
| 34 |
@Test |
| 35 |
public void constructorShouldBePrivate() { |
| 36 |
TestHelper.checkUtilityClass(ExceptionHelper.class); |
| 37 |
} |
| 38 |
|
| 39 |
@Test |
| 40 |
public void onErrorNotImplementedIsThrown() { |
| 41 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 42 |
|
| 43 |
Observable.just(1, 2, 3).subscribe(new Consumer<Integer>() { |
| 44 |
|
| 45 |
@Override |
| 46 |
public void accept(Integer t1) { |
| 47 |
throw new RuntimeException("hello"); |
| 48 |
} |
| 49 |
|
| 50 |
}); |
| 51 |
|
| 52 |
TestHelper.assertError(errors, 0, RuntimeException.class); |
| 53 |
assertTrue(errors.get(0).toString(), errors.get(0).getMessage().contains("hello")); |
| 54 |
RxJavaPlugins.reset(); |
| 55 |
} |
| 56 |
|
| 57 |
@Test |
| 58 |
public void stackOverflowWouldOccur() { |
| 59 |
final PublishSubject<Integer> a = PublishSubject.create(); |
| 60 |
final PublishSubject<Integer> b = PublishSubject.create(); |
| 61 |
final int MAX_STACK_DEPTH = 800; |
| 62 |
final AtomicInteger depth = new AtomicInteger(); |
| 63 |
|
| 64 |
a.subscribe(new Observer<Integer>() { |
| 65 |
|
| 66 |
@Override |
| 67 |
public void onSubscribe(Disposable d) { |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public void onComplete() { |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public void onError(Throwable e) { |
| 78 |
e.printStackTrace(); |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public void onNext(Integer n) { |
| 83 |
b.onNext(n + 1); |
| 84 |
} |
| 85 |
}); |
| 86 |
b.subscribe(new Observer<Integer>() { |
| 87 |
|
| 88 |
@Override |
| 89 |
public void onSubscribe(Disposable d) { |
| 90 |
// TODO Auto-generated method stub |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public void onComplete() { |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
@Override |
| 100 |
public void onError(Throwable e) { |
| 101 |
e.printStackTrace(); |
| 102 |
} |
| 103 |
|
| 104 |
@Override |
| 105 |
public void onNext(Integer n) { |
| 106 |
if (depth.get() < MAX_STACK_DEPTH) { |
| 107 |
depth.set(Thread.currentThread().getStackTrace().length); |
| 108 |
a.onNext(n + 1); |
| 109 |
} |
| 110 |
} |
| 111 |
}); |
| 112 |
a.onNext(1); |
| 113 |
assertTrue(depth.get() >= MAX_STACK_DEPTH); |
| 114 |
} |
| 115 |
|
| 116 |
@Test(expected = StackOverflowError.class) |
| 117 |
public void stackOverflowErrorIsThrown() { |
| 118 |
Observable.just(1).subscribe(new Observer<Integer>() { |
| 119 |
|
| 120 |
@Override |
| 121 |
public void onSubscribe(Disposable d) { |
| 122 |
|
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public void onComplete() { |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
@Override |
| 131 |
public void onError(Throwable e) { |
| 132 |
e.printStackTrace(); |
| 133 |
} |
| 134 |
|
| 135 |
@Override |
| 136 |
public void onNext(Integer t) { |
| 137 |
throw new StackOverflowError(); |
| 138 |
} |
| 139 |
|
| 140 |
}); |
| 141 |
} |
| 142 |
|
| 143 |
@Test(expected = ThreadDeath.class) |
| 144 |
public void threadDeathIsThrown() { |
| 145 |
Observable.just(1).subscribe(new Observer<Integer>() { |
| 146 |
|
| 147 |
@Override |
| 148 |
public void onSubscribe(Disposable d) { |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
@Override |
| 153 |
public void onComplete() { |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
@Override |
| 158 |
public void onError(Throwable e) { |
| 159 |
e.printStackTrace(); |
| 160 |
} |
| 161 |
|
| 162 |
@Override |
| 163 |
public void onNext(Integer t) { |
| 164 |
throw new ThreadDeath(); |
| 165 |
} |
| 166 |
|
| 167 |
}); |
| 168 |
} |
| 169 |
|
| 170 |
@Test |
| 171 |
public void utilityClass() { |
| 172 |
TestHelper.checkUtilityClass(Exceptions.class); |
| 173 |
} |
| 174 |
|
| 175 |
@Test |
| 176 |
public void manualThrowIfFatal() { |
| 177 |
|
| 178 |
try { |
| 179 |
Exceptions.throwIfFatal(new ThreadDeath()); |
| 180 |
fail("Didn't throw fatal exception"); |
| 181 |
} catch (ThreadDeath ex) { |
| 182 |
// expected |
| 183 |
} |
| 184 |
|
| 185 |
try { |
| 186 |
Exceptions.throwIfFatal(new LinkageError()); |
| 187 |
fail("Didn't throw fatal error"); |
| 188 |
} catch (LinkageError ex) { |
| 189 |
// expected |
| 190 |
} |
| 191 |
|
| 192 |
try { |
| 193 |
ExceptionHelper.wrapOrThrow(new LinkageError()); |
| 194 |
fail("Didn't propagate Error"); |
| 195 |
} catch (LinkageError ex) { |
| 196 |
// expected |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
@Test |
| 201 |
public void manualPropagate() { |
| 202 |
|
| 203 |
try { |
| 204 |
Exceptions.propagate(new InternalError()); |
| 205 |
fail("Didn't throw exception"); |
| 206 |
} catch (InternalError ex) { |
| 207 |
// expected |
| 208 |
} |
| 209 |
|
| 210 |
try { |
| 211 |
throw Exceptions.propagate(new IllegalArgumentException()); |
| 212 |
} catch (IllegalArgumentException ex) { |
| 213 |
// expected |
| 214 |
} |
| 215 |
|
| 216 |
try { |
| 217 |
throw ExceptionHelper.wrapOrThrow(new IOException()); |
| 218 |
} catch (RuntimeException ex) { |
| 219 |
if (!(ex.getCause() instanceof IOException)) { |
| 220 |
fail(ex.toString() + ": should have thrown RuntimeException(IOException)"); |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
@Test |
| 226 |
public void errorNotImplementedNull1() { |
| 227 |
OnErrorNotImplementedException ex = new OnErrorNotImplementedException(null); |
| 228 |
|
| 229 |
assertTrue("" + ex.getCause(), ex.getCause() instanceof NullPointerException); |
| 230 |
} |
| 231 |
|
| 232 |
@Test |
| 233 |
public void errorNotImplementedNull2() { |
| 234 |
OnErrorNotImplementedException ex = new OnErrorNotImplementedException("Message", null); |
| 235 |
|
| 236 |
assertTrue("" + ex.getCause(), ex.getCause() instanceof NullPointerException); |
| 237 |
} |
| 238 |
|
| 239 |
@Test |
| 240 |
public void errorNotImplementedWithCause() { |
| 241 |
OnErrorNotImplementedException ex = new OnErrorNotImplementedException("Message", new TestException("Forced failure")); |
| 242 |
|
| 243 |
assertTrue("" + ex.getCause(), ex.getCause() instanceof TestException); |
| 244 |
|
| 245 |
assertEquals("" + ex.getCause(), "Forced failure", ex.getCause().getMessage()); |
| 246 |
} |
| 247 |
} |