Search in sources :

Example 11 with ServiceDescriptor

use of io.grpc.ServiceDescriptor in project grpc-java by grpc.

the class ServerImplTest method testClientCancelTriggersContextCancellation.

@Test
public void testClientCancelTriggersContextCancellation() throws Exception {
    createAndStartServer(NO_FILTERS);
    final AtomicBoolean contextCancelled = new AtomicBoolean(false);
    callListener = new ServerCall.Listener<String>() {

        @Override
        public void onReady() {
            Context.current().addListener(new Context.CancellationListener() {

                @Override
                public void cancelled(Context context) {
                    contextCancelled.set(true);
                }
            }, MoreExecutors.directExecutor());
        }
    };
    final AtomicReference<ServerCall<String, Integer>> callReference = new AtomicReference<ServerCall<String, Integer>>();
    MethodDescriptor<String, Integer> method = MethodDescriptor.<String, Integer>newBuilder().setType(MethodDescriptor.MethodType.UNKNOWN).setFullMethodName("Waiter/serve").setRequestMarshaller(STRING_MARSHALLER).setResponseMarshaller(INTEGER_MARSHALLER).build();
    mutableFallbackRegistry.addService(ServerServiceDefinition.builder(new ServiceDescriptor("Waiter", method)).addMethod(method, new ServerCallHandler<String, Integer>() {

        @Override
        public ServerCall.Listener<String> startCall(ServerCall<String, Integer> call, Metadata headers) {
            callReference.set(call);
            return callListener;
        }
    }).build());
    ServerTransportListener transportListener = transportServer.registerNewServerTransport(new SimpleServerTransport());
    Metadata requestHeaders = new Metadata();
    StatsTraceContext statsTraceCtx = transportListener.methodDetermined("Waiter/serve", requestHeaders);
    assertNotNull(statsTraceCtx);
    when(stream.statsTraceContext()).thenReturn(statsTraceCtx);
    transportListener.streamCreated(stream, "Waiter/serve", requestHeaders);
    verify(stream).setListener(streamListenerCaptor.capture());
    ServerStreamListener streamListener = streamListenerCaptor.getValue();
    assertNotNull(streamListener);
    streamListener.onReady();
    streamListener.closed(Status.CANCELLED);
    assertEquals(1, executor.runDueTasks());
    assertTrue(contextCancelled.get());
}
Also used : Context(io.grpc.Context) StatsContext(com.google.instrumentation.stats.StatsContext) JumpToApplicationThreadServerStreamListener(io.grpc.internal.ServerImpl.JumpToApplicationThreadServerStreamListener) Metadata(io.grpc.Metadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServerCall(io.grpc.ServerCall) ServiceDescriptor(io.grpc.ServiceDescriptor) JumpToApplicationThreadServerStreamListener(io.grpc.internal.ServerImpl.JumpToApplicationThreadServerStreamListener) Test(org.junit.Test)

Example 12 with ServiceDescriptor

use of io.grpc.ServiceDescriptor in project grpc-java by grpc.

the class ClientCallsTest method inprocessTransportOutboundFlowControl.

@Test
public void inprocessTransportOutboundFlowControl() throws Exception {
    final Semaphore semaphore = new Semaphore(0);
    final List<Object> receivedMessages = new ArrayList<Object>(6);
    final SettableFuture<ServerCallStreamObserver<Integer>> observerFuture = SettableFuture.create();
    ServerServiceDefinition service = ServerServiceDefinition.builder(new ServiceDescriptor("some", STREAMING_METHOD)).addMethod(STREAMING_METHOD, ServerCalls.asyncBidiStreamingCall(new ServerCalls.BidiStreamingMethod<Integer, Integer>() {

        @Override
        public StreamObserver<Integer> invoke(StreamObserver<Integer> responseObserver) {
            final ServerCallStreamObserver<Integer> serverCallObserver = (ServerCallStreamObserver<Integer>) responseObserver;
            serverCallObserver.disableAutoInboundFlowControl();
            observerFuture.set(serverCallObserver);
            return new StreamObserver<Integer>() {

                @Override
                public void onNext(Integer value) {
                    receivedMessages.add(value);
                }

                @Override
                public void onError(Throwable t) {
                    receivedMessages.add(t);
                }

                @Override
                public void onCompleted() {
                    serverCallObserver.onCompleted();
                }
            };
        }
    })).build();
    long tag = System.nanoTime();
    server = InProcessServerBuilder.forName("go-with-the-flow" + tag).directExecutor().addService(service).build().start();
    channel = InProcessChannelBuilder.forName("go-with-the-flow" + tag).directExecutor().build();
    final ClientCall<Integer, Integer> clientCall = channel.newCall(STREAMING_METHOD, CallOptions.DEFAULT);
    final SettableFuture<Void> future = SettableFuture.create();
    ClientResponseObserver<Integer, Integer> responseObserver = new ClientResponseObserver<Integer, Integer>() {

        @Override
        public void beforeStart(final ClientCallStreamObserver<Integer> requestStream) {
            requestStream.setOnReadyHandler(new Runnable() {

                int iteration;

                @Override
                public void run() {
                    while (requestStream.isReady()) {
                        requestStream.onNext(iteration);
                    }
                    iteration++;
                    if (iteration == 3) {
                        requestStream.onCompleted();
                    }
                    semaphore.release();
                }
            });
        }

        @Override
        public void onNext(Integer value) {
        }

        @Override
        public void onError(Throwable t) {
            future.setException(t);
        }

        @Override
        public void onCompleted() {
            future.set(null);
        }
    };
    ClientCalls.asyncBidiStreamingCall(clientCall, responseObserver);
    ServerCallStreamObserver<Integer> serverCallObserver = observerFuture.get(5, TimeUnit.SECONDS);
    serverCallObserver.request(1);
    assertTrue(semaphore.tryAcquire(5, TimeUnit.SECONDS));
    serverCallObserver.request(2);
    assertTrue(semaphore.tryAcquire(5, TimeUnit.SECONDS));
    serverCallObserver.request(3);
    future.get(5, TimeUnit.SECONDS);
    // Verify that number of messages produced in each onReady handler call matches the number
    // requested by the client.
    assertEquals(Arrays.asList(0, 1, 1, 2, 2, 2), receivedMessages);
}
Also used : ArrayList(java.util.ArrayList) Semaphore(java.util.concurrent.Semaphore) ServiceDescriptor(io.grpc.ServiceDescriptor) ServerServiceDefinition(io.grpc.ServerServiceDefinition) NoopStreamObserver(io.grpc.stub.ServerCalls.NoopStreamObserver) Test(org.junit.Test)

Aggregations

ServiceDescriptor (io.grpc.ServiceDescriptor)12 Test (org.junit.Test)9 Metadata (io.grpc.Metadata)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 ServerServiceDefinition (io.grpc.ServerServiceDefinition)5 ServerCall (io.grpc.ServerCall)4 Status (io.grpc.Status)4 JumpToApplicationThreadServerStreamListener (io.grpc.internal.ServerImpl.JumpToApplicationThreadServerStreamListener)4 StatsContext (com.google.instrumentation.stats.StatsContext)3 Semaphore (java.util.concurrent.Semaphore)3 Context (io.grpc.Context)2 NoopStreamObserver (io.grpc.stub.ServerCalls.NoopStreamObserver)2 MutableHandlerRegistry (io.grpc.util.MutableHandlerRegistry)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ArrayList (java.util.ArrayList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 TagValue (com.google.instrumentation.stats.TagValue)1 ClientCall (io.grpc.ClientCall)1