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);
}
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();
}
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);
}
}
}
}
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);
}
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());
}
Aggregations