use of io.servicetalk.concurrent.internal.DelayedSubscription in project servicetalk by apple.
the class PublisherBufferTest method originalSourceIsRetriedIfSubscriberThrows.
@Test
void originalSourceIsRetriedIfSubscriberThrows() {
TestPublisher<Accumulator<Integer, Integer>> bPublisher = new TestPublisher<>();
DelayedSubscription bSubscription = new DelayedSubscription();
AtomicReference<TerminalNotification> terminal = new AtomicReference<>();
BlockingQueue<Integer> items = new LinkedBlockingDeque<>();
BlockingQueue<Integer> buffers = new LinkedBlockingDeque<>();
AtomicInteger counter = new AtomicInteger();
toSource(defer(() -> from(counter.incrementAndGet())).whenOnNext(items::add).retry((i, t) -> i < 3 && t == DELIBERATE_EXCEPTION).buffer(new TestBufferStrategy(bPublisher, 1))).subscribe(new Subscriber<Integer>() {
@Override
public void onSubscribe(Subscription s) {
bSubscription.delayedSubscription(s);
bSubscription.request(1);
}
@Override
public void onNext(@Nullable Integer integer) {
assert integer != null;
buffers.add(integer);
throw DELIBERATE_EXCEPTION;
}
@Override
public void onError(Throwable t) {
terminal.set(error(t));
}
@Override
public void onComplete() {
terminal.set(complete());
}
});
// it will generate a new boundary on each accumulation
bPublisher.onNext(new SumAccumulator(bPublisher));
assertThat(items, hasSize(1));
assertThat(items, contains(1));
assertThat(buffers, hasSize(1));
assertThat(buffers, contains(1));
bSubscription.request(MAX_VALUE);
assertThat(items, hasSize(3));
assertThat(items, contains(1, 2, 3));
assertThat(buffers, hasSize(3));
assertThat(buffers, contains(1, 2, 3));
assertThat(terminal.get().cause(), is(DELIBERATE_EXCEPTION));
}
use of io.servicetalk.concurrent.internal.DelayedSubscription in project servicetalk by apple.
the class BufferStrategiesTest method forCountOneDemandIsRespected.
@Test
void forCountOneDemandIsRespected() {
DelayedSubscription subscription = new DelayedSubscription();
AtomicReference<TerminalNotification> terminated = new AtomicReference<>();
BlockingQueue<Iterable<Integer>> accumulations = new LinkedBlockingDeque<>();
toSource(range(1, 6).buffer(forCountOrTime(2, ofDays(1)))).subscribe(new Subscriber<Iterable<Integer>>() {
@Override
public void onSubscribe(final Subscription s) {
subscription.delayedSubscription(s);
subscription.request(1);
}
@Override
public void onNext(@Nullable final Iterable<Integer> integers) {
assert integers != null;
accumulations.add(integers);
}
@Override
public void onError(final Throwable t) {
terminated.set(error(t));
}
@Override
public void onComplete() {
terminated.set(complete());
}
});
assertThat("Unexpected number of emitted accumulators", accumulations, hasSize(1));
assertThat("Unexpected accumulators", accumulations, contains(asList(1, 2)));
assertThat("Unexpected termination", terminated.get(), is(nullValue()));
subscription.request(1);
assertThat("Unexpected number of emitted accumulators", accumulations, hasSize(2));
assertThat("Unexpected accumulators", accumulations, contains(asList(1, 2), asList(3, 4)));
assertThat("Unexpected termination", terminated.get(), is(nullValue()));
subscription.request(1);
assertThat("Unexpected number of emitted accumulators", accumulations, hasSize(3));
assertThat("Unexpected accumulators", accumulations, contains(asList(1, 2), asList(3, 4), singletonList(5)));
assertThat("Unexpected termination", terminated.get(), is(complete()));
}
use of io.servicetalk.concurrent.internal.DelayedSubscription in project servicetalk by apple.
the class PublisherProcessor method handleSubscribe.
@Override
protected void handleSubscribe(final Subscriber<? super T> subscriber) {
final DelayedSubscription delayedSubscription = new DelayedSubscription();
try {
subscriber.onSubscribe(delayedSubscription);
} catch (Throwable t) {
handleExceptionFromOnSubscribe(subscriber, t);
return;
}
if (consumerUpdater.compareAndSet(this, null, new SubscriberProcessorSignalsConsumer<>(subscriber))) {
try {
delayedSubscription.delayedSubscription(this);
tryEmitSignals();
} catch (Throwable t) {
LOGGER.error("Unexpected error while delivering signals to the subscriber {}", subscriber, t);
}
} else {
ProcessorSignalsConsumer<? super T> existingConsumer = this.consumer;
assert existingConsumer != null;
@SuppressWarnings("unchecked") final Subscriber<? super T> existingSubscriber = existingConsumer instanceof PublisherProcessor.SubscriberProcessorSignalsConsumer ? ((SubscriberProcessorSignalsConsumer<T>) existingConsumer).subscriber : null;
safeOnError(subscriber, new DuplicateSubscribeException(existingSubscriber, subscriber));
}
}
Aggregations