use of java.util.concurrent.Flow.Subscription in project servicetalk by apple.
the class JdkFlowAdaptersTest method toFlowCancel.
@Test
void toFlowCancel() {
TestPublisher<Integer> stPublisher = new TestPublisher<>();
Subscriber<Integer> subscriber = toFlowPublisherAndSubscribe(stPublisher);
TestSubscription subscription = new TestSubscription();
stPublisher.onSubscribe(subscription);
assertThat("Source not subscribed.", stPublisher.isSubscribed(), is(true));
ArgumentCaptor<Subscription> subscriptionCaptor = ArgumentCaptor.forClass(Subscription.class);
verify(subscriber).onSubscribe(subscriptionCaptor.capture());
subscriptionCaptor.getValue().cancel();
assertThat("Subscription not cancelled.", subscription.isCancelled(), is(true));
}
use of java.util.concurrent.Flow.Subscription in project jenetics by jenetics.
the class StreamPublisherTest method publishClosingPublisher.
@Test
public void publishClosingPublisher() throws InterruptedException {
final int generations = 20;
final var publisher = new StreamPublisher<EvolutionResult<IntegerGene, Integer>>();
final var stream = _engine.stream();
final var lock = new ReentrantLock();
final var finished = lock.newCondition();
final AtomicBoolean running = new AtomicBoolean(true);
final AtomicBoolean completed = new AtomicBoolean(false);
final AtomicInteger count = new AtomicInteger();
publisher.subscribe(new Subscriber<>() {
private Subscription _subscription;
@Override
public void onSubscribe(final Subscription subscription) {
_subscription = requireNonNull(subscription);
_subscription.request(1);
}
@Override
public void onNext(final EvolutionResult<IntegerGene, Integer> er) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
throw new CancellationException();
}
count.incrementAndGet();
lock.lock();
try {
running.set(er.generation() < generations);
finished.signal();
} finally {
lock.unlock();
}
_subscription.request(1);
}
@Override
public void onComplete() {
lock.lock();
try {
completed.set(true);
finished.signalAll();
} finally {
lock.unlock();
}
}
@Override
public void onError(final Throwable throwable) {
}
});
publisher.attach(stream);
lock.lock();
try {
while (running.get()) {
finished.await();
}
} finally {
lock.unlock();
}
publisher.close();
lock.lock();
try {
while (!completed.get()) {
finished.await();
}
} finally {
lock.unlock();
}
assertThat(count.get()).isGreaterThanOrEqualTo(generations);
Assert.assertTrue(completed.get());
}
use of java.util.concurrent.Flow.Subscription in project jenetics by jenetics.
the class StreamPublisherTest method creation.
@Test
public void creation() throws InterruptedException {
final var lock = new ReentrantLock();
final var finished = lock.newCondition();
final var running = new AtomicBoolean(true);
final var generation = new AtomicLong();
final Stream<Long> stream = _engine.stream().limit(33).map(EvolutionResult::generation);
try (var publisher = new StreamPublisher<Long>()) {
publisher.subscribe(new Subscriber<>() {
private Subscription _subscription;
@Override
public void onSubscribe(final Subscription subscription) {
_subscription = subscription;
_subscription.request(1);
}
@Override
public void onNext(final Long g) {
generation.set(g);
_subscription.request(1);
}
@Override
public void onError(final Throwable throwable) {
}
@Override
public void onComplete() {
lock.lock();
try {
running.set(false);
finished.signal();
} finally {
lock.unlock();
}
}
});
publisher.attach(stream);
lock.lock();
try {
while (running.get()) {
finished.await();
}
} finally {
lock.unlock();
}
}
Assert.assertEquals(generation.get(), 33);
}
use of java.util.concurrent.Flow.Subscription in project helidon by oracle.
the class TestPublisher method subscribe.
@Override
public void subscribe(Subscriber<? super T> subscriber) {
subscribed = true;
subscriber.onSubscribe(new Subscription() {
@Override
public void request(long n) {
if (n > 0) {
Queue<T> items = new LinkedList<>(Arrays.asList(TestPublisher.this.items));
for (; n > 0 && !items.isEmpty(); n--) {
subscriber.onNext(items.poll());
}
if (items.isEmpty()) {
subscriber.onComplete();
}
}
}
@Override
public void cancel() {
}
});
}
use of java.util.concurrent.Flow.Subscription in project helidon by oracle.
the class MultiPartDecoder method subscribe.
@Override
public void subscribe(Subscriber<? super ReadableBodyPart> subscriber) {
Objects.requireNonNull(subscriber);
if (!halfInit(UPSTREAM_INIT)) {
Multi.<ReadableBodyPart>error(new IllegalStateException("Only one Subscriber allowed")).subscribe(subscriber);
return;
}
this.downstream = subscriber;
// contenders < 0, so any part request will be deferred
// drain() will not request anything from upstream until the second deferredInit() invocation witnesses
// that upstream is set
downstream.onSubscribe(new Subscription() {
@Override
public void request(long n) {
long curr = n <= 0 ? partsRequested.getAndSet(-1) : partsRequested.getAndUpdate(v -> Long.MAX_VALUE - v > n ? v + n : v < 0 ? v : Long.MAX_VALUE);
if (curr == 0) {
drain();
}
}
@Override
public void cancel() {
cancelled = true;
if (partsRequested.getAndSet(-1) == 0) {
drain();
}
}
});
deferredInit();
}
Aggregations