/
githubmirror
/
RxJava
Обзор
Документация
Войти
/
githubmirror
/
RxJava
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
4.x
src/test/java/io/reactivex/rxjava4/exceptions/ExceptionsTest.java
198 строк
6 KB
David Karnok
4.x: Streamable + range, fromArray, take, error, defer (#8206)
01 июл 2026, 10:11
Не верифицирован
01 июл 2026, 10:11
93d6ddd
Код
Авторство
О чём код?
/* * Copyright (c) 2016-present, RxJava Contributors. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License is * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See * the License for the specific language governing permissions and limitations under the License. */ package io.reactivex.rxjava4.exceptions; import static org.junit.jupiter.api.Assertions.*; import java.io.IOException; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import org.junit.jupiter.api.Test; import io.reactivex.rxjava4.core.*; import io.reactivex.rxjava4.disposables.Disposable; import io.reactivex.rxjava4.internal.util.ExceptionHelper; import io.reactivex.rxjava4.plugins.RxJavaPlugins; import io.reactivex.rxjava4.subjects.PublishSubject; import io.reactivex.rxjava4.testsupport.TestHelper; public class ExceptionsTest extends RxJavaTest { @Test public void constructorShouldBePrivate() { TestHelper.checkUtilityClass(ExceptionHelper.class); } @Test public void onErrorNotImplementedIsThrown() { List<Throwable> errors = TestHelper.trackPluginErrors(); Observable.just(1, 2, 3).subscribe(_ -> { throw new RuntimeException("hello"); }); TestHelper.assertError(errors, 0, RuntimeException.class); assertTrue(errors.getFirst().getMessage().contains("hello"), errors.getFirst().toString()); RxJavaPlugins.reset(); } @Test public void stackOverflowWouldOccur() { final PublishSubject<Integer> a = PublishSubject.create(); final PublishSubject<Integer> b = PublishSubject.create(); final int MAX_STACK_DEPTH = 800; final AtomicInteger depth = new AtomicInteger(); a.subscribe(new Observer<>() /* NFI */ { @Override public void onSubscribe(Disposable d) { } @Override public void onComplete() { } @Override public void onError(Throwable e) { e.printStackTrace(); } @Override public void onNext(Integer n) { b.onNext(n + 1); } }); b.subscribe(new Observer<>() /* NFI */ { @Override public void onSubscribe(Disposable d) { // TODO Auto-generated method stub } @Override public void onComplete() { } @Override public void onError(Throwable e) { e.printStackTrace(); } @Override public void onNext(Integer n) { if (depth.get() < MAX_STACK_DEPTH) { depth.set(Thread.currentThread().getStackTrace().length); a.onNext(n + 1); } } }); a.onNext(1); assertTrue(depth.get() >= MAX_STACK_DEPTH); } @Test public void stackOverflowErrorIsThrown() { assertThrows(StackOverflowError.class, () -> { Observable.just(1).subscribe(new Observer<>() /* NFI */ { @Override public void onSubscribe(Disposable d) { } @Override public void onComplete() { } @Override public void onError(Throwable e) { e.printStackTrace(); } @Override public void onNext(Integer t) { throw new StackOverflowError(); } }); }); } @Test public void utilityClass() { TestHelper.checkUtilityClass(Exceptions.class); } @Test public void manualPropagate() { try { Exceptions.propagate(new InternalError()); fail("Didn't throw exception"); } catch (InternalError ex) { // expected } try { throw Exceptions.propagate(new IllegalArgumentException()); } catch (IllegalArgumentException ex) { // expected } try { throw ExceptionHelper.wrapOrThrow(new IOException()); } catch (RuntimeException ex) { if (!(ex.getCause() instanceof IOException)) { fail(ex.toString() + ": should have thrown RuntimeException(IOException)"); } } } @Test public void errorNotImplementedNull1() { OnErrorNotImplementedException ex = new OnErrorNotImplementedException(null); assertTrue(ex.getCause() instanceof NullPointerException, "" + ex.getCause()); } @Test public void errorNotImplementedNull2() { OnErrorNotImplementedException ex = new OnErrorNotImplementedException("Message", null); assertTrue(ex.getCause() instanceof NullPointerException, "" + ex.getCause()); } @Test public void errorNotImplementedWithCause() { OnErrorNotImplementedException ex = new OnErrorNotImplementedException("Message", new TestException("Forced failure")); assertTrue(ex.getCause() instanceof TestException, "" + ex.getCause()); assertEquals("Forced failure", ex.getCause().getMessage(), "" + ex.getCause()); } @Test public void throwIfFatalLinkageError() { assertThrows(LinkageError.class, () -> { Exceptions.throwIfFatal(new LinkageError()); }); } }