Search in sources :

Example 6 with ServerServiceDefinition

use of io.grpc.ServerServiceDefinition 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)

Example 7 with ServerServiceDefinition

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

the class MutableHandlerRegistryTest method replaceAndLookup.

@Test
public void replaceAndLookup() {
    assertNull(registry.addService(basicServiceDefinition));
    assertNotNull(registry.lookupMethod("basic/flow"));
    MethodDescriptor<String, Integer> anotherMethod = MethodDescriptor.<String, Integer>newBuilder().setType(MethodType.UNKNOWN).setFullMethodName("basic/another").setRequestMarshaller(requestMarshaller).setResponseMarshaller(responseMarshaller).build();
    ServerServiceDefinition replaceServiceDefinition = ServerServiceDefinition.builder(new ServiceDescriptor("basic", anotherMethod)).addMethod(anotherMethod, flowHandler).build();
    ServerMethodDefinition<?, ?> anotherMethodDefinition = replaceServiceDefinition.getMethod("basic/another");
    assertSame(basicServiceDefinition, registry.addService(replaceServiceDefinition));
    assertNull(registry.lookupMethod("basic/flow"));
    ServerMethodDefinition<?, ?> method = registry.lookupMethod("basic/another");
    assertSame(anotherMethodDefinition, method);
}
Also used : ServiceDescriptor(io.grpc.ServiceDescriptor) ServerServiceDefinition(io.grpc.ServerServiceDefinition) Test(org.junit.Test)

Example 8 with ServerServiceDefinition

use of io.grpc.ServerServiceDefinition in project core-java by SpineEventEngine.

the class GrpcContainer method isScheduledForDeployment.

/**
     * Check if the given gRPC service is scheduled for the deployment in this container.
     *
     * <p>Note, that the given gRPC service will become available to the clients,
     * once the gRPC container is started.
     *
     * <p>To find out, whether the service is already available for calls,
     * use {@link #isLive(BindableService)} method.
     *
     * @param service the gRPC service to check
     * @return {@code true}, if the given gRPC service for deployment; {@code false} otherwise
     */
public boolean isScheduledForDeployment(BindableService service) {
    final String nameOfInterest = service.bindService().getServiceDescriptor().getName();
    boolean serviceIsPresent = false;
    for (ServerServiceDefinition serverServiceDefinition : services) {
        final String scheduledServiceName = serverServiceDefinition.getServiceDescriptor().getName();
        serviceIsPresent = serviceIsPresent || scheduledServiceName.equals(nameOfInterest);
    }
    return serviceIsPresent;
}
Also used : ServerServiceDefinition(io.grpc.ServerServiceDefinition)

Aggregations

ServerServiceDefinition (io.grpc.ServerServiceDefinition)8 Test (org.junit.Test)5 ServiceDescriptor (io.grpc.ServiceDescriptor)4 ArrayList (java.util.ArrayList)3 Semaphore (java.util.concurrent.Semaphore)3 NoopStreamObserver (io.grpc.stub.ServerCalls.NoopStreamObserver)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 FileDescriptor (com.google.protobuf.Descriptors.FileDescriptor)1 BindableService (io.grpc.BindableService)1 ClientCall (io.grpc.ClientCall)1 ManagedChannel (io.grpc.ManagedChannel)1 Metadata (io.grpc.Metadata)1 Status (io.grpc.Status)1 ProtoFileDescriptorSupplier (io.grpc.protobuf.ProtoFileDescriptorSupplier)1 HashSet (java.util.HashSet)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Nullable (javax.annotation.Nullable)1