/
githubmirror
/
RxJava
Обзор
Документация
Войти
/
githubmirror
/
RxJava
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
4.x
src/test/java/io/reactivex/rxjava4/internal/observers/FutureSingleObserverTest.java
161 строка
5 KB
David Karnok
4.x: Convert from JUnit 4 to JUnit 6 (#8202)
30 июн 2026, 12:01
Не верифицирован
30 июн 2026, 12:01
531388f
Код
Авторство
О чём код?
/* * 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.internal.observers; import static io.reactivex.rxjava4.internal.util.ExceptionHelper.timeoutMessage; import static org.junit.jupiter.api.Assertions.*; import java.util.concurrent.*; import org.junit.jupiter.api.Test; import io.reactivex.rxjava4.core.*; import io.reactivex.rxjava4.disposables.Disposable; import io.reactivex.rxjava4.exceptions.TestException; import io.reactivex.rxjava4.internal.functions.Functions; import io.reactivex.rxjava4.plugins.RxJavaPlugins; import io.reactivex.rxjava4.subjects.PublishSubject; import io.reactivex.rxjava4.testsupport.TestHelper; public class FutureSingleObserverTest extends RxJavaTest { @Test public void cancel() { final Future<?> f = Single.never().toFuture(); assertFalse(f.isCancelled()); assertFalse(f.isDone()); f.cancel(true); assertTrue(f.isCancelled()); assertTrue(f.isDone()); try { f.get(); fail("Should have thrown!"); } catch (CancellationException ex) { // expected } catch (InterruptedException | ExecutionException ex) { throw new AssertionError(ex); } try { f.get(5, TimeUnit.SECONDS); fail("Should have thrown!"); } catch (CancellationException ex) { // expected } catch (InterruptedException | ExecutionException | TimeoutException ex) { throw new AssertionError(ex); } } @Test public void cancelRace() { for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { final Future<?> f = Single.never().toFuture(); Runnable r = () -> f.cancel(true); TestHelper.race(r, r); } } @Test public void timeout() throws Exception { Future<?> f = Single.never().toFuture(); try { f.get(100, TimeUnit.MILLISECONDS); fail("Should have thrown"); } catch (TimeoutException expected) { assertEquals(timeoutMessage(100, TimeUnit.MILLISECONDS), expected.getMessage()); } } @Test public void dispose() { Future<Integer> f = Single.just(1).toFuture(); ((Disposable)f).dispose(); assertTrue(((Disposable)f).isDisposed()); } @Test public void errorGetWithTimeout() throws Exception { Future<?> f = Single.error(new TestException()).toFuture(); try { f.get(5, TimeUnit.SECONDS); fail("Should have thrown"); } catch (ExecutionException ex) { assertTrue(ex.getCause() instanceof TestException, ex.toString()); } } @Test public void normalGetWitHTimeout() throws Exception { Future<Integer> f = Single.just(1).toFuture(); assertEquals(1, f.get(5, TimeUnit.SECONDS).intValue()); } @Test public void getAwait() throws Exception { Future<Integer> f = Single.just(1).delay(100, TimeUnit.MILLISECONDS).toFuture(); assertEquals(1, f.get(5, TimeUnit.SECONDS).intValue()); } @Test public void onSuccessCancelRace() { for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { final PublishSubject<Integer> ps = PublishSubject.create(); final Future<?> f = ps.single(-99).toFuture(); ps.onNext(1); Runnable r1 = () -> f.cancel(true); Runnable r2 = ps::onComplete; TestHelper.race(r1, r2); } } @Test public void onErrorCancelRace() { RxJavaPlugins.setErrorHandler(Functions.emptyConsumer()); try { for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { final PublishSubject<Integer> ps = PublishSubject.create(); final Future<?> f = ps.single(-99).toFuture(); final TestException ex = new TestException(); Runnable r1 = () -> f.cancel(true); Runnable r2 = () -> ps.onError(ex); TestHelper.race(r1, r2); } } finally { RxJavaPlugins.reset(); } } }