/* * 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.rxjava3.internal.util; import static org.junit.Assert.*; import java.util.List; import io.reactivex.rxjava3.disposables.Disposable; import org.junit.Test; import io.reactivex.rxjava3.core.*; import io.reactivex.rxjava3.exceptions.TestException; import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; import io.reactivex.rxjava3.observers.TestObserver; import io.reactivex.rxjava3.plugins.RxJavaPlugins; import io.reactivex.rxjava3.subscribers.TestSubscriber; import io.reactivex.rxjava3.testsupport.TestHelper; public class AtomicThrowableTest extends RxJavaTest { @Test public void isTerminated() { AtomicThrowable ex = new AtomicThrowable(); assertFalse(ex.isTerminated()); assertNull(ex.terminate()); assertTrue(ex.isTerminated()); } @Test public void tryTerminateAndReportNull() { List errors = TestHelper.trackPluginErrors(); try { AtomicThrowable ex = new AtomicThrowable(); ex.tryTerminateAndReport(); assertTrue("" + errors, errors.isEmpty()); } finally { RxJavaPlugins.reset(); } } @Test public void tryTerminateAndReportAlreadyTerminated() { List errors = TestHelper.trackPluginErrors(); try { AtomicThrowable ex = new AtomicThrowable(); ex.terminate(); ex.tryTerminateAndReport(); assertTrue("" + errors, errors.isEmpty()); } finally { RxJavaPlugins.reset(); } } @Test public void tryTerminateAndReportHasError() { List errors = TestHelper.trackPluginErrors(); try { AtomicThrowable ex = new AtomicThrowable(); ex.set(new TestException()); ex.tryTerminateAndReport(); TestHelper.assertUndeliverable(errors, 0, TestException.class); assertEquals(1, errors.size()); } finally { RxJavaPlugins.reset(); } } @Test public void tryTerminateConsumerSubscriberNoError() { TestSubscriber ts = new TestSubscriber<>(); ts.onSubscribe(new BooleanSubscription()); AtomicThrowable ex = new AtomicThrowable(); ex.tryTerminateConsumer(ts); ts.assertResult(); } @Test public void tryTerminateConsumerSubscriberError() { TestSubscriber ts = new TestSubscriber<>(); ts.onSubscribe(new BooleanSubscription()); AtomicThrowable ex = new AtomicThrowable(); ex.set(new TestException()); ex.tryTerminateConsumer(ts); ts.assertFailure(TestException.class); } @Test public void tryTerminateConsumerSubscriberTerminated() { TestSubscriber ts = new TestSubscriber<>(); ts.onSubscribe(new BooleanSubscription()); AtomicThrowable ex = new AtomicThrowable(); ex.terminate(); ex.tryTerminateConsumer(ts); ts.assertEmpty(); } @Test public void tryTerminateConsumerObserverNoError() { TestObserver to = new TestObserver<>(); to.onSubscribe(Disposable.empty()); AtomicThrowable ex = new AtomicThrowable(); ex.tryTerminateConsumer((Observer)to); to.assertResult(); } @Test public void tryTerminateConsumerObserverError() { TestObserver to = new TestObserver<>(); to.onSubscribe(Disposable.empty()); AtomicThrowable ex = new AtomicThrowable(); ex.set(new TestException()); ex.tryTerminateConsumer((Observer)to); to.assertFailure(TestException.class); } @Test public void tryTerminateConsumerObserverTerminated() { TestObserver to = new TestObserver<>(); to.onSubscribe(Disposable.empty()); AtomicThrowable ex = new AtomicThrowable(); ex.terminate(); ex.tryTerminateConsumer((Observer)to); to.assertEmpty(); } @Test public void tryTerminateConsumerMaybeObserverNoError() { TestObserver to = new TestObserver<>(); to.onSubscribe(Disposable.empty()); AtomicThrowable ex = new AtomicThrowable(); ex.tryTerminateConsumer((MaybeObserver)to); to.assertResult(); } @Test public void tryTerminateConsumerMaybeObserverError() { TestObserver to = new TestObserver<>(); to.onSubscribe(Disposable.empty()); AtomicThrowable ex = new AtomicThrowable(); ex.set(new TestException()); ex.tryTerminateConsumer((MaybeObserver)to); to.assertFailure(TestException.class); } @Test public void tryTerminateConsumerMaybeObserverTerminated() { TestObserver to = new TestObserver<>(); to.onSubscribe(Disposable.empty()); AtomicThrowable ex = new AtomicThrowable(); ex.terminate(); ex.tryTerminateConsumer((MaybeObserver)to); to.assertEmpty(); } @Test public void tryTerminateConsumerSingleNoError() { TestObserver to = new TestObserver<>(); to.onSubscribe(Disposable.empty()); AtomicThrowable ex = new AtomicThrowable(); ex.tryTerminateConsumer((SingleObserver)to); to.assertEmpty(); } @Test public void tryTerminateConsumerSingleError() { TestObserver to = new TestObserver<>(); to.onSubscribe(Disposable.empty()); AtomicThrowable ex = new AtomicThrowable(); ex.set(new TestException()); ex.tryTerminateConsumer((SingleObserver)to); to.assertFailure(TestException.class); } @Test public void tryTerminateConsumerSingleTerminated() { TestObserver to = new TestObserver<>(); to.onSubscribe(Disposable.empty()); AtomicThrowable ex = new AtomicThrowable(); ex.terminate(); ex.tryTerminateConsumer((SingleObserver)to); to.assertEmpty(); } @Test public void tryTerminateConsumerCompletableObserverNoError() { TestObserver to = new TestObserver<>(); to.onSubscribe(Disposable.empty()); AtomicThrowable ex = new AtomicThrowable(); ex.tryTerminateConsumer((CompletableObserver)to); to.assertResult(); } @Test public void tryTerminateConsumerCompletableObserverError() { TestObserver to = new TestObserver<>(); to.onSubscribe(Disposable.empty()); AtomicThrowable ex = new AtomicThrowable(); ex.set(new TestException()); ex.tryTerminateConsumer((CompletableObserver)to); to.assertFailure(TestException.class); } @Test public void tryTerminateConsumerCompletableObserverTerminated() { TestObserver to = new TestObserver<>(); to.onSubscribe(Disposable.empty()); AtomicThrowable ex = new AtomicThrowable(); ex.terminate(); ex.tryTerminateConsumer((CompletableObserver)to); to.assertEmpty(); } static Emitter wrapToEmitter(final Observer observer) { return new Emitter() { @Override public void onNext(T value) { observer.onNext(value); } @Override public void onError(Throwable error) { observer.onError(error); } @Override public void onComplete() { observer.onComplete(); } }; } @Test public void tryTerminateConsumerEmitterNoError() { TestObserver to = new TestObserver<>(); to.onSubscribe(Disposable.empty()); AtomicThrowable ex = new AtomicThrowable(); ex.tryTerminateConsumer(wrapToEmitter(to)); to.assertResult(); } @Test public void tryTerminateConsumerEmitterError() { TestObserver to = new TestObserver<>(); to.onSubscribe(Disposable.empty()); AtomicThrowable ex = new AtomicThrowable(); ex.set(new TestException()); ex.tryTerminateConsumer(wrapToEmitter(to)); to.assertFailure(TestException.class); } @Test public void tryTerminateConsumerEmitterTerminated() { TestObserver to = new TestObserver<>(); to.onSubscribe(Disposable.empty()); AtomicThrowable ex = new AtomicThrowable(); ex.terminate(); ex.tryTerminateConsumer(wrapToEmitter(to)); to.assertEmpty(); } @Test public void tryAddThrowableOrReportNull() { List errors = TestHelper.trackPluginErrors(); try { AtomicThrowable ex = new AtomicThrowable(); ex.tryAddThrowableOrReport(new TestException()); assertTrue("" + errors, errors.isEmpty()); } finally { RxJavaPlugins.reset(); } } @Test public void tryAddThrowableOrReportTerminated() { List errors = TestHelper.trackPluginErrors(); try { AtomicThrowable ex = new AtomicThrowable(); ex.terminate(); assertFalse(ex.tryAddThrowableOrReport(new TestException())); assertFalse("" + errors, errors.isEmpty()); TestHelper.assertUndeliverable(errors, 0, TestException.class); } finally { RxJavaPlugins.reset(); } } }