Search in sources :

Example 1 with Subscription

use of org.reactivestreams.Subscription in project jersey by jersey.

the class RxFlowableTest method testInvoker.

private void testInvoker(final RxFlowableInvoker rx, final int expectedStatus, final boolean testDedicatedThread) throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Response> responseRef = new AtomicReference<>();
    final AtomicReference<Throwable> errorRef = new AtomicReference<>();
    rx.get().subscribe(new Subscriber<Response>() {

        @Override
        public void onSubscribe(Subscription s) {
            s.request(Long.MAX_VALUE);
        }

        @Override
        public void onComplete() {
            latch.countDown();
        }

        @Override
        public void onError(final Throwable e) {
            errorRef.set(e);
            latch.countDown();
        }

        @Override
        public void onNext(final Response response) {
            responseRef.set(response);
        }
    });
    latch.await();
    if (errorRef.get() == null) {
        testResponse(responseRef.get(), expectedStatus, testDedicatedThread);
    } else {
        throw (Exception) errorRef.get();
    }
}
Also used : Response(javax.ws.rs.core.Response) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Subscription(org.reactivestreams.Subscription) NotFoundException(javax.ws.rs.NotFoundException)

Example 2 with Subscription

use of org.reactivestreams.Subscription in project RxJava by ReactiveX.

the class FutureSubscriber method onError.

@Override
public void onError(Throwable t) {
    for (; ; ) {
        Subscription a = s.get();
        if (a == this || a == SubscriptionHelper.CANCELLED) {
            RxJavaPlugins.onError(t);
            return;
        }
        error = t;
        if (s.compareAndSet(a, this)) {
            countDown();
            return;
        }
    }
}
Also used : Subscription(org.reactivestreams.Subscription)

Example 3 with Subscription

use of org.reactivestreams.Subscription in project RxJava by ReactiveX.

the class SubscriptionArbiter method request.

@Override
public final void request(long n) {
    if (SubscriptionHelper.validate(n)) {
        if (unbounded) {
            return;
        }
        if (get() == 0 && compareAndSet(0, 1)) {
            long r = requested;
            if (r != Long.MAX_VALUE) {
                r = BackpressureHelper.addCap(r, n);
                requested = r;
                if (r == Long.MAX_VALUE) {
                    unbounded = true;
                }
            }
            Subscription a = actual;
            if (decrementAndGet() != 0) {
                drainLoop();
            }
            if (a != null) {
                a.request(n);
            }
            return;
        }
        BackpressureHelper.add(missedRequested, n);
        drain();
    }
}
Also used : Subscription(org.reactivestreams.Subscription)

Example 4 with Subscription

use of org.reactivestreams.Subscription in project RxJava by ReactiveX.

the class FlowableDelaySubscriptionOtherTest method testNoSubscriptionIfOtherErrors.

@Test
public void testNoSubscriptionIfOtherErrors() {
    PublishProcessor<Object> other = PublishProcessor.create();
    TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
    final AtomicInteger subscribed = new AtomicInteger();
    Flowable.<Integer>error(new TestException()).doOnSubscribe(new Consumer<Subscription>() {

        @Override
        public void accept(Subscription s) {
            subscribed.getAndIncrement();
        }
    }).delaySubscription(other).subscribe(ts);
    ts.assertNotComplete();
    ts.assertNoErrors();
    ts.assertNoValues();
    Assert.assertEquals("Premature subscription", 0, subscribed.get());
    other.onError(new TestException());
    Assert.assertEquals("Premature subscription", 0, subscribed.get());
    ts.assertNoValues();
    ts.assertNotComplete();
    ts.assertError(TestException.class);
}
Also used : TestException(io.reactivex.exceptions.TestException) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Subscription(org.reactivestreams.Subscription)

Example 5 with Subscription

use of org.reactivestreams.Subscription in project RxJava by ReactiveX.

the class ObservableOnErrorResumeNextViaFunctionTest method testFunctionThrowsError.

/**
     * Test that when a function throws an exception this is propagated through onError.
     */
@Test
public void testFunctionThrowsError() {
    Subscription s = mock(Subscription.class);
    TestObservable w = new TestObservable(s, "one");
    Function<Throwable, Observable<String>> resume = new Function<Throwable, Observable<String>>() {

        @Override
        public Observable<String> apply(Throwable t1) {
            throw new RuntimeException("exception from function");
        }
    };
    Observable<String> o = Observable.unsafeCreate(w).onErrorResumeNext(resume);
    @SuppressWarnings("unchecked") DefaultObserver<String> observer = mock(DefaultObserver.class);
    o.subscribe(observer);
    try {
        w.t.join();
    } catch (InterruptedException e) {
        fail(e.getMessage());
    }
    // we should get the "one" value before the error
    verify(observer, times(1)).onNext("one");
    // we should have received an onError call on the Observer since the resume function threw an exception
    verify(observer, times(1)).onError(any(Throwable.class));
    verify(observer, times(0)).onComplete();
}
Also used : Function(io.reactivex.functions.Function) Subscription(org.reactivestreams.Subscription)

Aggregations

Subscription (org.reactivestreams.Subscription)627 Test (org.junit.Test)506 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)158 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)131 ArrayList (java.util.ArrayList)117 AtomicReference (java.util.concurrent.atomic.AtomicReference)105 List (java.util.List)80 FluxOperatorTest (reactor.test.publisher.FluxOperatorTest)74 Subscriber (org.reactivestreams.Subscriber)68 AtomicLong (java.util.concurrent.atomic.AtomicLong)56 Assert (org.junit.Assert)56 Arrays (java.util.Arrays)43 BaseSequentialTest (com.oath.cyclops.streams.BaseSequentialTest)42 Vector (cyclops.data.Vector)39 ReactiveSeq (cyclops.reactive.ReactiveSeq)39 Executor (java.util.concurrent.Executor)38 Spouts (cyclops.reactive.Spouts)36 Arrays.asList (java.util.Arrays.asList)36 Executors (java.util.concurrent.Executors)35 StepVerifier (reactor.test.StepVerifier)34