use of java.util.concurrent.Flow.Subscription in project helidon by oracle.
the class SingleToOptionalFuture method onNext.
@Override
public void onNext(T item) {
// we expect exactly one item
if (!this.item.compareAndSet(null, item)) {
super.completeExceptionally(new IllegalStateException("Received more than one value for a single."));
Subscription s = ref.getAndSet(null);
s.cancel();
}
}
use of java.util.concurrent.Flow.Subscription in project helidon by oracle.
the class SingleToOptionalFuture method onSubscribe.
@Override
public void onSubscribe(Subscription next) {
Subscription current = ref.getAndSet(next);
Objects.requireNonNull(next, "Subscription cannot be null");
if (current != null) {
next.cancel();
current.cancel();
} else {
next.request(Long.MAX_VALUE);
}
}
use of java.util.concurrent.Flow.Subscription in project helidon by oracle.
the class ReadableByteChannelPublisherTest method testCancelled.
@Test
void testCancelled() throws Exception {
PeriodicalChannel pc = createChannelWithNoAvailableData(5, 1);
ReadableByteChannelPublisher publisher = new ReadableByteChannelPublisher(pc, RetrySchema.constant(100));
AtomicReference<Subscription> subscriptionRef = new AtomicReference<>();
final CountDownLatch onNextCalled = new CountDownLatch(1);
AtomicReference<Throwable> failure = new AtomicReference<>();
AtomicBoolean completeCalled = new AtomicBoolean();
publisher.subscribe(new Subscriber<>() {
@Override
public void onSubscribe(Subscription subscription) {
subscriptionRef.set(subscription);
subscription.request(1);
}
@Override
public void onNext(DataChunk item) {
onNextCalled.countDown();
}
@Override
public void onError(Throwable throwable) {
failure.set(throwable);
}
@Override
public void onComplete() {
completeCalled.set(true);
}
});
onNextCalled.await(5, TimeUnit.SECONDS);
subscriptionRef.get().cancel();
assertThat("Should not complete", completeCalled.get(), is(false));
assertThat("Exception should be null", failure.get(), is(nullValue()));
LazyValue<ScheduledExecutorService> executor = publisher.executor();
assertThat("Executor should have been used", executor.isLoaded(), is(true));
assertThat("Executor should have been shut down", executor.get().isShutdown(), is(true));
}
Aggregations