Search in sources :

Example 1 with AsyncCompletableSubscriber

use of rx.observers.AsyncCompletableSubscriber 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.getInstance().registerErrorHandler(new RxJavaErrorHandler() {

        @Override
        public void handleError(Throwable throwable) {
            if (!errorRef.compareAndSet(null, throwable)) {
                // Don't swallow secondary errors!
                throw Exceptions.propagate(throwable);
            }
            latch.countDown();
        }
    });
    final TestSubscriber<Void> subscriber = new TestSubscriber<>();
    final RuntimeException e = new RuntimeException();
    service.completable().unsafeSubscribe(new AsyncCompletableSubscriber() {

        @Override
        public void onCompleted() {
            throw e;
        }

        @Override
        public void onError(Throwable t) {
            subscriber.onError(t);
        }
    });
    latch.await(1, SECONDS);
    assertThat(errorRef.get()).isSameAs(e);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RxJavaErrorHandler(rx.plugins.RxJavaErrorHandler) AsyncCompletableSubscriber(rx.observers.AsyncCompletableSubscriber) TestSubscriber(rx.observers.TestSubscriber) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with AsyncCompletableSubscriber

use of rx.observers.AsyncCompletableSubscriber 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.getInstance().registerErrorHandler(new RxJavaErrorHandler() {

        @Override
        public void handleError(Throwable throwable) {
            if (!pluginRef.compareAndSet(null, throwable)) {
                // Don't swallow secondary errors!
                throw Exceptions.propagate(throwable);
            }
            latch.countDown();
        }
    });
    final TestSubscriber<Void> subscriber = new TestSubscriber<>();
    final RuntimeException e = new RuntimeException();
    final AtomicReference<Throwable> errorRef = new AtomicReference<>();
    service.completable().unsafeSubscribe(new AsyncCompletableSubscriber() {

        @Override
        public void onCompleted() {
            subscriber.onCompleted();
        }

        @Override
        public void onError(Throwable t) {
            errorRef.set(t);
            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) RxJavaErrorHandler(rx.plugins.RxJavaErrorHandler) AsyncCompletableSubscriber(rx.observers.AsyncCompletableSubscriber) CompositeException(rx.exceptions.CompositeException) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) TestSubscriber(rx.observers.TestSubscriber) Test(org.junit.Test)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 Test (org.junit.Test)2 AsyncCompletableSubscriber (rx.observers.AsyncCompletableSubscriber)2 TestSubscriber (rx.observers.TestSubscriber)2 RxJavaErrorHandler (rx.plugins.RxJavaErrorHandler)2 CompositeException (rx.exceptions.CompositeException)1