Search in sources :

Example 26 with Publisher

use of io.servicetalk.concurrent.api.Publisher in project servicetalk by apple.

the class NettyPipelinedConnectionTest method writeSubscribeThrowsLetsSubsequentRequestsThrough.

@Test
void writeSubscribeThrowsLetsSubsequentRequestsThrough() {
    AtomicBoolean firstReadOperation = new AtomicBoolean();
    TestPublisher<Integer> mockReadPublisher1 = new TestPublisher<>();
    TestPublisher<Integer> mockReadPublisher2 = new TestPublisher<>();
    @SuppressWarnings("unchecked") NettyConnection<Integer, Integer> mockConnection = mock(NettyConnection.class);
    doAnswer((Answer<Publisher<Integer>>) invocation -> firstReadOperation.compareAndSet(false, true) ? mockReadPublisher1 : mockReadPublisher2).when(mockConnection).read();
    doAnswer((Answer<Completable>) invocation -> new Completable() {

        @Override
        protected void handleSubscribe(final CompletableSource.Subscriber subscriber) {
            throw DELIBERATE_EXCEPTION;
        }
    }).when(mockConnection).write(eq(writePublisher1), any(), any());
    doAnswer((Answer<Completable>) invocation -> {
        Publisher<Integer> writePub = invocation.getArgument(0);
        return writePub.ignoreElements();
    }).when(mockConnection).write(eq(writePublisher2), 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(mockReadPublisher1.isSubscribed());
    mockReadPublisher1.onError(newSecondException());
    assertThat(readSubscriber.awaitOnError(), is(DELIBERATE_EXCEPTION));
    assertFalse(writePublisher1.isSubscribed());
    verifySecondRequestProcessed(mockReadPublisher2, mockConnection);
}
Also used : UNSUPPORTED_PROTOCOL_CLOSE_HANDLER(io.servicetalk.transport.netty.internal.CloseHandler.UNSUPPORTED_PROTOCOL_CLOSE_HANDLER) BeforeEach(org.junit.jupiter.api.BeforeEach) Protocol(io.servicetalk.transport.api.ConnectionInfo.Protocol) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) MAX_VALUE(java.lang.Integer.MAX_VALUE) Future(java.util.concurrent.Future) HttpExecutionStrategies.defaultStrategy(io.servicetalk.http.api.HttpExecutionStrategies.defaultStrategy) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Executor(io.servicetalk.concurrent.api.Executor) Executors.immediate(io.servicetalk.concurrent.api.Executors.immediate) TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) CyclicBarrier(java.util.concurrent.CyclicBarrier) SynchronousQueue(java.util.concurrent.SynchronousQueue) PublisherSource(io.servicetalk.concurrent.PublisherSource) Collection(java.util.Collection) DefaultNettyConnection(io.servicetalk.transport.netty.internal.DefaultNettyConnection) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) SubscriberUtils.deliverCompleteFromSource(io.servicetalk.concurrent.internal.SubscriberUtils.deliverCompleteFromSource) RetryableException(io.servicetalk.transport.api.RetryableException) WriteDemandEstimators(io.servicetalk.transport.netty.internal.WriteDemandEstimators) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) FlushStrategies.defaultFlushStrategy(io.servicetalk.transport.netty.internal.FlushStrategies.defaultFlushStrategy) Matchers.is(org.hamcrest.Matchers.is) CloseHandler(io.servicetalk.transport.netty.internal.CloseHandler) NoopConnectionObserver(io.servicetalk.transport.netty.internal.NoopTransportObserver.NoopConnectionObserver) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) FlushStrategy(io.servicetalk.transport.netty.internal.FlushStrategy) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) DEFAULT_ALLOCATOR(io.servicetalk.buffer.netty.BufferAllocators.DEFAULT_ALLOCATOR) TestPublisher(io.servicetalk.concurrent.api.TestPublisher) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Publisher(io.servicetalk.concurrent.api.Publisher) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Answer(org.mockito.stubbing.Answer) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) DELIBERATE_EXCEPTION(io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION) ExecutorService(java.util.concurrent.ExecutorService) ClosedChannelException(java.nio.channels.ClosedChannelException) Single(io.servicetalk.concurrent.api.Single) Completable(io.servicetalk.concurrent.api.Completable) EmbeddedDuplexChannel(io.servicetalk.transport.netty.internal.EmbeddedDuplexChannel) Mockito.when(org.mockito.Mockito.when) Subscription(io.servicetalk.concurrent.PublisherSource.Subscription) SourceAdapters.toSource(io.servicetalk.concurrent.api.SourceAdapters.toSource) CompletableSource(io.servicetalk.concurrent.CompletableSource) Mockito.verify(org.mockito.Mockito.verify) Mockito.never(org.mockito.Mockito.never) WriteDemandEstimator(io.servicetalk.transport.netty.internal.WriteDemandEstimator) Completable.completed(io.servicetalk.concurrent.api.Completable.completed) Executors(io.servicetalk.concurrent.api.Executors) NettyConnection(io.servicetalk.transport.netty.internal.NettyConnection) SECONDS(java.util.concurrent.TimeUnit.SECONDS) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Completable(io.servicetalk.concurrent.api.Completable) TestPublisher(io.servicetalk.concurrent.api.TestPublisher) TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) TestPublisher(io.servicetalk.concurrent.api.TestPublisher) Publisher(io.servicetalk.concurrent.api.Publisher) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) Subscription(io.servicetalk.concurrent.PublisherSource.Subscription) Test(org.junit.jupiter.api.Test)

Example 27 with Publisher

use of io.servicetalk.concurrent.api.Publisher in project servicetalk by apple.

the class NettyPipelinedConnectionTest method readThrowsClosesConnection.

@Test
void readThrowsClosesConnection() {
    @SuppressWarnings("unchecked") NettyConnection<Integer, Integer> mockConnection = mock(NettyConnection.class);
    doAnswer((Answer<Publisher<Integer>>) invocation -> {
        throw DELIBERATE_EXCEPTION;
    }).when(mockConnection).read();
    doAnswer((Answer<Completable>) invocation -> {
        Publisher<Integer> writePub = invocation.getArgument(0);
        return writePub.ignoreElements();
    }).when(mockConnection).write(any(), any(), any());
    when(mockConnection.closeAsync()).thenReturn(completed());
    requester = new NettyPipelinedConnection<>(mockConnection, 2);
    toSource(requester.write(writePublisher1)).subscribe(readSubscriber);
    Subscription readSubscription = readSubscriber.awaitSubscription();
    readSubscription.request(1);
    writePublisher1.onError(newSecondException());
    assertThat(readSubscriber.awaitOnError(), is(DELIBERATE_EXCEPTION));
    assertTrue(writePublisher1.isSubscribed());
    verify(mockConnection).closeAsync();
}
Also used : UNSUPPORTED_PROTOCOL_CLOSE_HANDLER(io.servicetalk.transport.netty.internal.CloseHandler.UNSUPPORTED_PROTOCOL_CLOSE_HANDLER) BeforeEach(org.junit.jupiter.api.BeforeEach) Protocol(io.servicetalk.transport.api.ConnectionInfo.Protocol) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) MAX_VALUE(java.lang.Integer.MAX_VALUE) Future(java.util.concurrent.Future) HttpExecutionStrategies.defaultStrategy(io.servicetalk.http.api.HttpExecutionStrategies.defaultStrategy) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Executor(io.servicetalk.concurrent.api.Executor) Executors.immediate(io.servicetalk.concurrent.api.Executors.immediate) TestPublisherSubscriber(io.servicetalk.concurrent.test.internal.TestPublisherSubscriber) CyclicBarrier(java.util.concurrent.CyclicBarrier) SynchronousQueue(java.util.concurrent.SynchronousQueue) PublisherSource(io.servicetalk.concurrent.PublisherSource) Collection(java.util.Collection) DefaultNettyConnection(io.servicetalk.transport.netty.internal.DefaultNettyConnection) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) SubscriberUtils.deliverCompleteFromSource(io.servicetalk.concurrent.internal.SubscriberUtils.deliverCompleteFromSource) RetryableException(io.servicetalk.transport.api.RetryableException) WriteDemandEstimators(io.servicetalk.transport.netty.internal.WriteDemandEstimators) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) FlushStrategies.defaultFlushStrategy(io.servicetalk.transport.netty.internal.FlushStrategies.defaultFlushStrategy) Matchers.is(org.hamcrest.Matchers.is) CloseHandler(io.servicetalk.transport.netty.internal.CloseHandler) NoopConnectionObserver(io.servicetalk.transport.netty.internal.NoopTransportObserver.NoopConnectionObserver) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) FlushStrategy(io.servicetalk.transport.netty.internal.FlushStrategy) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) DEFAULT_ALLOCATOR(io.servicetalk.buffer.netty.BufferAllocators.DEFAULT_ALLOCATOR) TestPublisher(io.servicetalk.concurrent.api.TestPublisher) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Publisher(io.servicetalk.concurrent.api.Publisher) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Answer(org.mockito.stubbing.Answer) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) DELIBERATE_EXCEPTION(io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION) ExecutorService(java.util.concurrent.ExecutorService) ClosedChannelException(java.nio.channels.ClosedChannelException) Single(io.servicetalk.concurrent.api.Single) Completable(io.servicetalk.concurrent.api.Completable) EmbeddedDuplexChannel(io.servicetalk.transport.netty.internal.EmbeddedDuplexChannel) Mockito.when(org.mockito.Mockito.when) Subscription(io.servicetalk.concurrent.PublisherSource.Subscription) SourceAdapters.toSource(io.servicetalk.concurrent.api.SourceAdapters.toSource) CompletableSource(io.servicetalk.concurrent.CompletableSource) Mockito.verify(org.mockito.Mockito.verify) Mockito.never(org.mockito.Mockito.never) WriteDemandEstimator(io.servicetalk.transport.netty.internal.WriteDemandEstimator) Completable.completed(io.servicetalk.concurrent.api.Completable.completed) Executors(io.servicetalk.concurrent.api.Executors) NettyConnection(io.servicetalk.transport.netty.internal.NettyConnection) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Completable(io.servicetalk.concurrent.api.Completable) TestPublisher(io.servicetalk.concurrent.api.TestPublisher) Publisher(io.servicetalk.concurrent.api.Publisher) TestSubscription(io.servicetalk.concurrent.api.TestSubscription) Subscription(io.servicetalk.concurrent.PublisherSource.Subscription) Test(org.junit.jupiter.api.Test)

Example 28 with Publisher

use of io.servicetalk.concurrent.api.Publisher in project servicetalk by apple.

the class AfterErrorTest method testCallbackThrowsError.

@Override
@Test
void testCallbackThrowsError() {
    DeliberateException srcEx = new DeliberateException();
    doError(Publisher.<String>failed(srcEx), t -> {
        throw DELIBERATE_EXCEPTION;
    }).subscribe(subscriber);
    assertThat(subscriber.awaitOnError(), sameInstance(srcEx));
}
Also used : Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) Publisher(io.servicetalk.concurrent.api.Publisher) PublisherSource(io.servicetalk.concurrent.PublisherSource) DeliberateException(io.servicetalk.concurrent.internal.DeliberateException) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DELIBERATE_EXCEPTION(io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION) SourceAdapters.toSource(io.servicetalk.concurrent.api.SourceAdapters.toSource) DeliberateException(io.servicetalk.concurrent.internal.DeliberateException) Test(org.junit.jupiter.api.Test)

Example 29 with Publisher

use of io.servicetalk.concurrent.api.Publisher in project servicetalk by apple.

the class TestMulti method multiGenerated.

@Test
void multiGenerated() throws ExecutionException, InterruptedException {
    TesterService service = new TesterService() {

        @Override
        public Publisher<TestReply> testBiDiStream(final GrpcServiceContext ctx, final Publisher<TestRequest> request) {
            return Publisher.from(TestReply.newBuilder().build());
        }

        @Override
        public Single<TestReply> testRequestStream(final GrpcServiceContext ctx, final Publisher<TestRequest> request) {
            return Single.succeeded(TestReply.newBuilder().build());
        }

        @Override
        public Publisher<TestReply> testResponseStream(final GrpcServiceContext ctx, final TestRequest request) {
            return Publisher.from(TestReply.newBuilder().build());
        }

        @Override
        public Single<TestReply> test(final GrpcServiceContext ctx, final TestRequest request) {
            return Single.succeeded(TestReply.newBuilder().build());
        }
    };
    Tester2Service service2 = new Tester2Service() {

        @Override
        public Publisher<Untils> testUntils(final GrpcServiceContext ctx, final Generic.Empty request) {
            return Publisher.from(Untils.newBuilder().build());
        }

        @Override
        public Single<SharedReply> testShared(final GrpcServiceContext ctx, final TestRequest request) {
            return Single.succeeded(SharedReply.newBuilder().build());
        }
    };
    service.closeAsync().toFuture().get();
    service2.closeAsync().toFuture().get();
}
Also used : Tester2Service(io.servicetalk.grpc.protoc.test.multi.Tester2.Tester2Service) TestReply(io.servicetalk.grpc.protoc.test.multi.TestReply) Untils(test.shared.TestShared.Untils) SharedReply(test.shared.TestShared.SharedReply) GrpcServiceContext(io.servicetalk.grpc.api.GrpcServiceContext) Publisher(io.servicetalk.concurrent.api.Publisher) TesterService(io.servicetalk.grpc.protoc.test.multi.Tester.TesterService) TestRequest(io.servicetalk.grpc.protoc.test.multi.TestRequest) Test(org.junit.jupiter.api.Test)

Example 30 with Publisher

use of io.servicetalk.concurrent.api.Publisher in project servicetalk by apple.

the class TestSingle method singleGenerated.

@Test
void singleGenerated() throws ExecutionException, InterruptedException {
    GreeterService service = new GreeterService() {

        @Override
        public Single<HelloReply> sayHelloFromMany(final GrpcServiceContext ctx, final Publisher<UserRequest> request) {
            return Single.succeeded(HelloReply.newBuilder().build());
        }

        @Override
        public Single<HelloReply> sayHello(final GrpcServiceContext ctx, final UserRequest request) {
            return Single.succeeded(HelloReply.newBuilder().build());
        }

        @Override
        public Publisher<HelloReply> sayHelloToFromMany(final GrpcServiceContext ctx, final Publisher<UserRequest> request) {
            return Publisher.from(HelloReply.newBuilder().build());
        }

        @Override
        public Publisher<HelloReply> sayHelloToMany(final GrpcServiceContext ctx, final UserRequest request) {
            return Publisher.from(HelloReply.newBuilder().build());
        }
    };
    FarewellerService service2 = new FarewellerService() {

        @Override
        public Publisher<Untils> getAllUntils(final GrpcServiceContext ctx, final Generic.Empty request) {
            return Publisher.from(Untils.newBuilder().build());
        }

        @Override
        public Single<SharedReply> sayGoodbye(final GrpcServiceContext ctx, final UserRequest request) {
            return Single.succeeded(SharedReply.newBuilder().build());
        }
    };
    service.closeAsync().toFuture().get();
    service2.closeAsync().toFuture().get();
}
Also used : Untils(test.shared.TestShared.Untils) SharedReply(test.shared.TestShared.SharedReply) FarewellerService(io.servicetalk.grpc.protoc.test.single.HelloWorldProto.Fareweller.FarewellerService) GreeterService(io.servicetalk.grpc.protoc.test.single.HelloWorldProto.Greeter.GreeterService) GrpcServiceContext(io.servicetalk.grpc.api.GrpcServiceContext) Publisher(io.servicetalk.concurrent.api.Publisher) HelloReply(io.servicetalk.grpc.protoc.test.single.HelloWorldProto.HelloReply) UserRequest(io.servicetalk.grpc.protoc.test.single.HelloWorldProto.UserRequest) Test(org.junit.jupiter.api.Test)

Aggregations

Publisher (io.servicetalk.concurrent.api.Publisher)30 Buffer (io.servicetalk.buffer.api.Buffer)16 Single (io.servicetalk.concurrent.api.Single)15 Test (org.junit.jupiter.api.Test)15 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)13 Completable (io.servicetalk.concurrent.api.Completable)12 PublisherSource (io.servicetalk.concurrent.PublisherSource)11 Collection (java.util.Collection)11 SourceAdapters.toSource (io.servicetalk.concurrent.api.SourceAdapters.toSource)10 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)10 ArrayList (java.util.ArrayList)10 List (java.util.List)10 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)10 Matchers.is (org.hamcrest.Matchers.is)10 Subscription (io.servicetalk.concurrent.PublisherSource.Subscription)9 StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)9 DEFAULT_ALLOCATOR (io.servicetalk.buffer.netty.BufferAllocators.DEFAULT_ALLOCATOR)8 Completable.completed (io.servicetalk.concurrent.api.Completable.completed)8 Executor (io.servicetalk.concurrent.api.Executor)8 HttpHeaders (io.servicetalk.http.api.HttpHeaders)8