use of io.servicetalk.concurrent.api.Completable in project servicetalk by apple.
the class NettyPipelinedConnectionTest method readSubscribeThrowsWritesStillProcessed.
@Test
void readSubscribeThrowsWritesStillProcessed() {
AtomicBoolean thrownError = new AtomicBoolean();
Publisher<Integer> mockReadPublisher = new Publisher<Integer>() {
@Override
protected void handleSubscribe(final PublisherSource.Subscriber<? super Integer> subscriber) {
if (thrownError.compareAndSet(false, true)) {
throw DELIBERATE_EXCEPTION;
} else {
deliverCompleteFromSource(subscriber);
}
}
};
@SuppressWarnings("unchecked") NettyConnection<Integer, Integer> mockConnection = mock(NettyConnection.class);
when(mockConnection.read()).thenReturn(mockReadPublisher);
doAnswer((Answer<Completable>) invocation -> {
Publisher<Integer> writePub = invocation.getArgument(0);
return writePub.ignoreElements();
}).when(mockConnection).write(any(), any(), any());
requester = new NettyPipelinedConnection<>(mockConnection, 2);
toSource(requester.write(writePublisher1)).subscribe(readSubscriber);
toSource(requester.write(writePublisher2)).subscribe(readSubscriber2);
Subscription readSubscription = readSubscriber.awaitSubscription();
readSubscription.request(1);
assertTrue(writePublisher1.isSubscribed());
writePublisher1.onError(newSecondException());
assertThat(readSubscriber.awaitOnError(), is(DELIBERATE_EXCEPTION));
readSubscriber2.awaitSubscription();
assertTrue(writePublisher2.isSubscribed());
writePublisher2.onComplete();
readSubscriber2.awaitOnComplete();
verify(mockConnection, never()).closeAsync();
}
use of io.servicetalk.concurrent.api.Completable in project servicetalk by apple.
the class NettyPipelinedConnectionTest method multiThreadedWritesAllComplete.
@Test
void multiThreadedWritesAllComplete() throws Exception {
// Avoid using EmbeddedChannel because it is not thread safe. This test writes/reads from multiple threads.
@SuppressWarnings("unchecked") NettyConnection<Integer, Integer> connection = mock(NettyConnection.class);
Executor connectionExecutor = Executors.newCachedThreadExecutor();
try {
doAnswer((Answer<Completable>) invocation -> {
Publisher<Integer> writeStream = invocation.getArgument(0);
return writeStream.ignoreElements().concat(connectionExecutor.submit(() -> {
}));
}).when(connection).write(any(), any(), any());
doAnswer((Answer<Publisher<Integer>>) invocation -> connectionExecutor.submit(() -> {
}).concat(Publisher.from(1))).when(connection).read();
final int concurrentRequestCount = 300;
NettyPipelinedConnection<Integer, Integer> pipelinedConnection = new NettyPipelinedConnection<>(connection, concurrentRequestCount);
CyclicBarrier requestStartBarrier = new CyclicBarrier(concurrentRequestCount);
List<Future<Collection<Integer>>> futures = new ArrayList<>(concurrentRequestCount);
ExecutorService executor = new ThreadPoolExecutor(0, concurrentRequestCount, 1, SECONDS, new SynchronousQueue<>());
try {
for (int i = 0; i < concurrentRequestCount; ++i) {
final int finalI = i;
futures.add(executor.submit(() -> {
try {
requestStartBarrier.await();
} catch (Exception e) {
return Single.<Collection<Integer>>failed(new AssertionError("failure during request " + finalI, e)).toFuture().get();
}
return pipelinedConnection.write(Publisher.from(finalI)).toFuture().get();
}));
}
for (Future<Collection<Integer>> future : futures) {
assertThat(future.get(), hasSize(1));
}
} finally {
executor.shutdown();
}
} finally {
connectionExecutor.closeAsync().subscribe();
}
}
use of io.servicetalk.concurrent.api.Completable in project servicetalk by apple.
the class ChannelSetTest method closeAsyncGracefullyWithoutNettyConnectionChannelHandler.
@Test
void closeAsyncGracefullyWithoutNettyConnectionChannelHandler() {
when(mockClosableAttribute.getAndSet(any())).thenReturn(null);
Completable completable = closeAsyncGracefully(fixture, 100, SECONDS);
verify(channel, never()).close();
TestCompletableSubscriber subscriber = new TestCompletableSubscriber();
toSource(completable).subscribe(subscriber);
verify(channel).close();
subscriber.awaitOnComplete();
}
use of io.servicetalk.concurrent.api.Completable in project servicetalk by apple.
the class ChannelSetTest method testCloseAsyncGracefullyTwice.
@Test
void testCloseAsyncGracefullyTwice() throws Exception {
Completable gracefulCompletable1 = closeAsyncGracefully(fixture, 60, SECONDS);
Completable gracefulCompletable2 = closeAsyncGracefully(fixture, 60, SECONDS);
TestCompletableSubscriber subscriber = new TestCompletableSubscriber();
toSource(gracefulCompletable1).subscribe(subscriber);
verify(nettyConnection).closeAsyncGracefullyNoOffload();
TestCompletableSubscriber subscriber2 = new TestCompletableSubscriber();
toSource(gracefulCompletable2).subscribe(subscriber2);
verify(nettyConnection, times(1)).closeAsyncGracefullyNoOffload();
assertThat(subscriber.pollTerminal(10, MILLISECONDS), is(nullValue()));
closeAsyncGracefullyCompletable.onComplete();
assertThat(subscriber.pollTerminal(10, MILLISECONDS), is(nullValue()));
listener.operationComplete(channelCloseFuture);
fixture.onClose().toFuture().get();
subscriber.awaitOnComplete();
subscriber2.awaitOnComplete();
}
use of io.servicetalk.concurrent.api.Completable in project servicetalk by apple.
the class ChannelSetTest method testCloseAsyncThenCloseAsyncGracefully.
@Test
void testCloseAsyncThenCloseAsyncGracefully() throws Exception {
Completable closeCompletable = fixture.closeAsync();
Completable gracefulCompletable = closeAsyncGracefully(fixture, 100, SECONDS);
TestCompletableSubscriber subscriber = new TestCompletableSubscriber();
toSource(closeCompletable).subscribe(subscriber);
verify(channel).close();
TestCompletableSubscriber subscriber2 = new TestCompletableSubscriber();
toSource(gracefulCompletable).subscribe(subscriber2);
fixture.onClose().toFuture().get();
subscriber.awaitOnComplete();
subscriber2.awaitOnComplete();
}
Aggregations