Search in sources :

Example 1 with ForwardingCompletableObserver

use of retrofit2.adapter.rxjava2.CompletableThrowingTest.ForwardingCompletableObserver in project retrofit by square.

the class AsyncTest method throwingInOnCompleteDeliveredToPlugin.

@Test
public void throwingInOnCompleteDeliveredToPlugin() throws InterruptedException {
    server.enqueue(new MockResponse());
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> errorRef = new AtomicReference<>();
    RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {

        @Override
        public void accept(Throwable throwable) throws Exception {
            if (!errorRef.compareAndSet(null, throwable)) {
                // Don't swallow secondary errors!
                throw Exceptions.propagate(throwable);
            }
            latch.countDown();
        }
    });
    TestObserver<Void> observer = new TestObserver<>();
    final RuntimeException e = new RuntimeException();
    service.completable().subscribe(new ForwardingCompletableObserver(observer) {

        @Override
        public void onComplete() {
            throw e;
        }
    });
    latch.await(1, SECONDS);
    assertThat(errorRef.get()).isSameAs(e);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) CompositeException(io.reactivex.exceptions.CompositeException) IOException(java.io.IOException) TestObserver(io.reactivex.observers.TestObserver) ForwardingCompletableObserver(retrofit2.adapter.rxjava2.CompletableThrowingTest.ForwardingCompletableObserver) Test(org.junit.Test)

Example 2 with ForwardingCompletableObserver

use of retrofit2.adapter.rxjava2.CompletableThrowingTest.ForwardingCompletableObserver in project retrofit by square.

the class AsyncTest method bodyThrowingInOnErrorDeliveredToPlugin.

@Test
public void bodyThrowingInOnErrorDeliveredToPlugin() throws InterruptedException {
    server.enqueue(new MockResponse().setResponseCode(404));
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> pluginRef = new AtomicReference<>();
    RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {

        @Override
        public void accept(Throwable throwable) throws Exception {
            if (!pluginRef.compareAndSet(null, throwable)) {
                // Don't swallow secondary errors!
                throw Exceptions.propagate(throwable);
            }
            latch.countDown();
        }
    });
    TestObserver<Void> observer = new TestObserver<>();
    final RuntimeException e = new RuntimeException();
    final AtomicReference<Throwable> errorRef = new AtomicReference<>();
    service.completable().subscribe(new ForwardingCompletableObserver(observer) {

        @Override
        public void onError(Throwable throwable) {
            errorRef.set(throwable);
            throw e;
        }
    });
    latch.await(1, SECONDS);
    // noinspection ThrowableResultOfMethodCallIgnored
    CompositeException composite = (CompositeException) pluginRef.get();
    assertThat(composite.getExceptions()).containsExactly(errorRef.get(), e);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) CompositeException(io.reactivex.exceptions.CompositeException) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) CompositeException(io.reactivex.exceptions.CompositeException) IOException(java.io.IOException) TestObserver(io.reactivex.observers.TestObserver) ForwardingCompletableObserver(retrofit2.adapter.rxjava2.CompletableThrowingTest.ForwardingCompletableObserver) Test(org.junit.Test)

Aggregations

CompositeException (io.reactivex.exceptions.CompositeException)2 TestObserver (io.reactivex.observers.TestObserver)2 IOException (java.io.IOException)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 Test (org.junit.Test)2 ForwardingCompletableObserver (retrofit2.adapter.rxjava2.CompletableThrowingTest.ForwardingCompletableObserver)2