use of java.util.concurrent.Flow.Subscription in project jenetics by jenetics.
the class StreamPublisherTest method publishLimitedStream.
@Test
public void publishLimitedStream() throws InterruptedException {
final int generations = 20;
final var publisher = new StreamPublisher<EvolutionResult<IntegerGene, Integer>>();
final var stream = _engine.stream().limit(generations);
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) {
count.incrementAndGet();
_subscription.request(1);
}
@Override
public void onComplete() {
lock.lock();
try {
running.set(false);
completed.set(true);
finished.signal();
} 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();
Assert.assertEquals(count.get(), generations);
Assert.assertTrue(completed.get());
}
use of java.util.concurrent.Flow.Subscription in project servicetalk by apple.
the class JdkFlowAdaptersTest method fromFlowCancel.
@Test
void fromFlowCancel() {
AtomicReference<Subscription> receivedSubscription = new AtomicReference<>();
Publisher<Integer> flowPublisher = newMockFlowPublisher((__, subscription) -> receivedSubscription.set(subscription));
fromFlowPublisher(flowPublisher).firstOrElse(() -> null).toFuture().cancel(true);
Subscription subscription = receivedSubscription.get();
assertThat("Subscription not received.", subscription, is(notNullValue()));
verify(subscription).cancel();
}
use of java.util.concurrent.Flow.Subscription in project julian-http-client by ljtfreitas.
the class ByteArrayHTTPMessageCodecTest method write.
@Test
void write() {
byte[] expected = "response body".getBytes();
HTTPRequestBody httpRequestBody = codec.write(expected, UTF_8);
assertTrue(httpRequestBody.contentType().isEmpty());
httpRequestBody.serialize().subscribe(new Subscriber<>() {
@Override
public void onSubscribe(Subscription subscription) {
subscription.request(1);
}
@Override
public void onNext(ByteBuffer item) {
assertArrayEquals(expected, item.array());
}
@Override
public void onError(Throwable throwable) {
fail(throwable);
}
@Override
public void onComplete() {
}
});
}
use of java.util.concurrent.Flow.Subscription in project ignite-3 by apache.
the class ItInternalTableScanTest method testExceptionRowScan.
/**
* Checks that exception from storage cursor creation properly propagates to subscriber.
*/
@Test
public void testExceptionRowScan() throws Exception {
// The latch that allows to await Subscriber.onError() before asserting test invariants.
CountDownLatch gotExceptionLatch = new CountDownLatch(1);
AtomicReference<Throwable> gotException = new AtomicReference<>();
when(mockStorage.scan(any())).thenThrow(new StorageException("Some storage exception"));
internalTbl.scan(0, null).subscribe(new Subscriber<>() {
@Override
public void onSubscribe(Subscription subscription) {
subscription.request(1);
}
@Override
public void onNext(BinaryRow item) {
fail("Should never get here.");
}
@Override
public void onError(Throwable throwable) {
gotException.set(throwable);
gotExceptionLatch.countDown();
}
@Override
public void onComplete() {
fail("Should never get here.");
}
});
gotExceptionLatch.await();
assertEquals(gotException.get().getCause().getClass(), StorageException.class);
}
use of java.util.concurrent.Flow.Subscription in project helidon by oracle.
the class SingleTest method testToFutureDoubleOnNext.
@Test
public void testToFutureDoubleOnNext() throws InterruptedException, ExecutionException {
Single<String> single = new CompletionSingle<String>() {
@Override
public void subscribe(Subscriber<? super String> subscriber) {
subscriber.onSubscribe(new Subscription() {
@Override
public void request(long n) {
subscriber.onNext("foo");
subscriber.onNext("bar");
subscriber.onComplete();
}
@Override
public void cancel() {
}
});
}
};
Future<String> future = single.toStage().toCompletableFuture();
assertThat(future.isDone(), is(equalTo(true)));
assertThat(future.get(), is(equalTo("foo")));
}
Aggregations