Search in sources :

Example 56 with Subscription

use of org.reactivestreams.Subscription in project reactor-core by reactor.

the class EmitterProcessorTest method testColdIdentityProcessor.

@Test
public void testColdIdentityProcessor() throws InterruptedException {
    final int elements = 10;
    CountDownLatch latch = new CountDownLatch(elements + 1);
    Processor<Integer, Integer> processor = EmitterProcessor.create(16);
    List<Integer> list = new ArrayList<>();
    processor.subscribe(new CoreSubscriber<Integer>() {

        Subscription s;

        @Override
        public void onSubscribe(Subscription s) {
            this.s = s;
            s.request(1);
        }

        @Override
        public void onNext(Integer integer) {
            synchronized (list) {
                list.add(integer);
            }
            latch.countDown();
            if (latch.getCount() > 0) {
                s.request(1);
            }
        }

        @Override
        public void onError(Throwable t) {
            t.printStackTrace();
        }

        @Override
        public void onComplete() {
            System.out.println("completed!");
            latch.countDown();
        }
    });
    Flux.range(1, 10).subscribe(processor);
    // stream.broadcastComplete();
    latch.await(8, TimeUnit.SECONDS);
    long count = latch.getCount();
    org.junit.Assert.assertTrue("Count > 0 : " + count + " (" + list + ")  , Running on " + Schedulers.DEFAULT_POOL_SIZE + " CPU", latch.getCount() == 0);
}
Also used : ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Example 57 with Subscription

use of org.reactivestreams.Subscription in project reactor-core by reactor.

the class EmitterProcessorTest method state.

@Test
public void state() {
    EmitterProcessor<Integer> tp = EmitterProcessor.create();
    assertThat(tp.getPending()).isEqualTo(0);
    assertThat(tp.getBufferSize()).isEqualTo(Queues.SMALL_BUFFER_SIZE);
    assertThat(tp.isCancelled()).isFalse();
    assertThat(tp.inners()).isEmpty();
    Disposable d1 = tp.subscribe();
    assertThat(tp.inners()).hasSize(1);
    FluxSink<Integer> s = tp.sink();
    s.next(2);
    s.next(3);
    s.next(4);
    assertThat(tp.getPending()).isEqualTo(0);
    AtomicReference<Subscription> d2 = new AtomicReference<>();
    tp.subscribe(new CoreSubscriber<Integer>() {

        @Override
        public void onSubscribe(Subscription s) {
            d2.set(s);
        }

        @Override
        public void onNext(Integer integer) {
        }

        @Override
        public void onError(Throwable t) {
        }

        @Override
        public void onComplete() {
        }
    });
    s.next(5);
    s.next(6);
    s.next(7);
    assertThat(tp.scan(BUFFERED)).isEqualTo(3);
    assertThat(tp.isTerminated()).isFalse();
    s.complete();
    assertThat(tp.isTerminated()).isFalse();
    d1.dispose();
    d2.get().cancel();
    assertThat(tp.isTerminated()).isTrue();
    StepVerifier.create(tp).verifyComplete();
    // noop
    tp.onNext(8);
    EmitterProcessor<Void> empty = EmitterProcessor.create();
    empty.onComplete();
    assertThat(empty.isTerminated()).isTrue();
}
Also used : Disposable(reactor.core.Disposable) AtomicReference(java.util.concurrent.atomic.AtomicReference) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Example 58 with Subscription

use of org.reactivestreams.Subscription in project reactor-core by reactor.

the class StrictSubscriber method request.

@Override
public void request(long n) {
    if (n <= 0) {
        cancel();
        onError(new IllegalArgumentException("ยง3.9 violated: positive request amount required but it was " + n));
        return;
    }
    Subscription a = s;
    if (a != null) {
        a.request(n);
    } else {
        Operators.addCap(REQUESTED, this, n);
        a = s;
        if (a != null) {
            long r = REQUESTED.getAndSet(this, 0L);
            if (r != 0L) {
                a.request(n);
            }
        }
    }
}
Also used : Subscription(org.reactivestreams.Subscription)

Example 59 with Subscription

use of org.reactivestreams.Subscription in project reactor-core by reactor.

the class DefaultTestPublisherTests method normalIgnoresMultipleTerminations.

@Test
public void normalIgnoresMultipleTerminations() {
    TestPublisher<String> publisher = TestPublisher.create();
    AtomicLong count = new AtomicLong();
    Subscriber<String> subscriber = new CoreSubscriber<String>() {

        @Override
        public void onSubscribe(Subscription s) {
        }

        @Override
        public void onNext(String s) {
        }

        @Override
        public void onError(Throwable t) {
            count.incrementAndGet();
        }

        @Override
        public void onComplete() {
            count.incrementAndGet();
        }
    };
    publisher.subscribe(subscriber);
    publisher.complete().emit("A", "B", "C").error(new IllegalStateException("boom"));
    assertThat(count.get()).isEqualTo(1);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) CoreSubscriber(reactor.core.CoreSubscriber) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Example 60 with Subscription

use of org.reactivestreams.Subscription in project reactor-core by reactor.

the class StrictSubscriberTest method cancelNotDelayed.

@Test
public void cancelNotDelayed() {
    AtomicBoolean state1 = new AtomicBoolean();
    AtomicBoolean state2 = new AtomicBoolean();
    AtomicReference<Throwable> e = new AtomicReference<>();
    Flux.just(1).doOnCancel(() -> state2.set(state1.get())).subscribe(new CoreSubscriber<Integer>() {

        @Override
        public void onSubscribe(Subscription s) {
            s.cancel();
            state1.set(true);
        }

        @Override
        public void onNext(Integer t) {
        }

        @Override
        public void onError(Throwable t) {
            e.set(t);
        }

        @Override
        public void onComplete() {
        }
    });
    Assert.assertNull("Error: " + e.get(), e.get());
    Assert.assertFalse("Cancel executed before onSubscribe finished", state2.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

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