use of io.grpc.ServiceDescriptor in project grpc-java by grpc.
the class ServerImplTest method exceptionInStartCallPropagatesToStream.
@Test
public void exceptionInStartCallPropagatesToStream() throws Exception {
createAndStartServer(NO_FILTERS);
final Status status = Status.ABORTED.withDescription("Oh, no!");
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) {
throw status.asRuntimeException();
}
}).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);
verify(stream, atLeast(1)).statsTraceContext();
verifyNoMoreInteractions(stream);
assertEquals(1, executor.runDueTasks());
verify(stream).getAuthority();
verify(stream).close(same(status), notNull(Metadata.class));
verify(stream, atLeast(1)).statsTraceContext();
verifyNoMoreInteractions(stream);
}
use of io.grpc.ServiceDescriptor in project grpc-java by grpc.
the class ServerImplTest method handlerRegistryPriorities.
@Test
public void handlerRegistryPriorities() throws Exception {
fallbackRegistry = mock(HandlerRegistry.class);
MethodDescriptor<String, Integer> method1 = MethodDescriptor.<String, Integer>newBuilder().setType(MethodDescriptor.MethodType.UNKNOWN).setFullMethodName("Service1/Method1").setRequestMarshaller(STRING_MARSHALLER).setResponseMarshaller(INTEGER_MARSHALLER).build();
registry = new InternalHandlerRegistry.Builder().addService(ServerServiceDefinition.builder(new ServiceDescriptor("Service1", method1)).addMethod(method1, callHandler).build()).build();
transportServer = new SimpleServer();
createAndStartServer(NO_FILTERS);
ServerTransportListener transportListener = transportServer.registerNewServerTransport(new SimpleServerTransport());
Metadata requestHeaders = new Metadata();
StatsTraceContext statsTraceCtx = transportListener.methodDetermined("Waiter/serve", requestHeaders);
assertNotNull(statsTraceCtx);
when(stream.statsTraceContext()).thenReturn(statsTraceCtx);
// This call will be handled by callHandler from the internal registry
transportListener.streamCreated(stream, "Service1/Method1", requestHeaders);
assertEquals(1, executor.runDueTasks());
verify(callHandler).startCall(Matchers.<ServerCall<String, Integer>>anyObject(), Matchers.<Metadata>anyObject());
// This call will be handled by the fallbackRegistry because it's not registred in the internal
// registry.
transportListener.streamCreated(stream, "Service1/Method2", requestHeaders);
assertEquals(1, executor.runDueTasks());
verify(fallbackRegistry).lookupMethod("Service1/Method2", null);
verifyNoMoreInteractions(callHandler);
verifyNoMoreInteractions(fallbackRegistry);
}
use of io.grpc.ServiceDescriptor in project grpc-java by grpc.
the class ClientCallsTest method inprocessTransportInboundFlowControl.
@Test
public void inprocessTransportInboundFlowControl() throws Exception {
final Semaphore semaphore = new Semaphore(0);
ServerServiceDefinition service = ServerServiceDefinition.builder(new ServiceDescriptor("some", STREAMING_METHOD)).addMethod(STREAMING_METHOD, ServerCalls.asyncBidiStreamingCall(new ServerCalls.BidiStreamingMethod<Integer, Integer>() {
int iteration;
@Override
public StreamObserver<Integer> invoke(StreamObserver<Integer> responseObserver) {
final ServerCallStreamObserver<Integer> serverCallObserver = (ServerCallStreamObserver<Integer>) responseObserver;
serverCallObserver.setOnReadyHandler(new Runnable() {
@Override
public void run() {
while (serverCallObserver.isReady()) {
serverCallObserver.onNext(iteration);
}
iteration++;
semaphore.release();
}
});
return new ServerCalls.NoopStreamObserver<Integer>() {
@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 CountDownLatch latch = new CountDownLatch(1);
final List<Object> receivedMessages = new ArrayList<Object>(6);
ClientResponseObserver<Integer, Integer> responseObserver = new ClientResponseObserver<Integer, Integer>() {
@Override
public void beforeStart(final ClientCallStreamObserver<Integer> requestStream) {
requestStream.disableAutoInboundFlowControl();
}
@Override
public void onNext(Integer value) {
receivedMessages.add(value);
}
@Override
public void onError(Throwable t) {
receivedMessages.add(t);
latch.countDown();
}
@Override
public void onCompleted() {
latch.countDown();
}
};
CallStreamObserver<Integer> integerStreamObserver = (CallStreamObserver<Integer>) ClientCalls.asyncBidiStreamingCall(clientCall, responseObserver);
semaphore.acquire();
integerStreamObserver.request(2);
semaphore.acquire();
integerStreamObserver.request(3);
integerStreamObserver.onCompleted();
assertTrue(latch.await(5, TimeUnit.SECONDS));
// Verify that number of messages produced in each onReady handler call matches the number
// requested by the client. Note that ClientCalls.asyncBidiStreamingCall will request(1)
assertEquals(Arrays.asList(0, 1, 1, 2, 2, 2), receivedMessages);
}
use of io.grpc.ServiceDescriptor in project grpc-java by grpc.
the class ServerCallsTest method inprocessTransportManualFlow.
@Test
public void inprocessTransportManualFlow() throws Exception {
final Semaphore semaphore = new Semaphore(1);
ServerServiceDefinition service = ServerServiceDefinition.builder(new ServiceDescriptor("some", STREAMING_METHOD)).addMethod(STREAMING_METHOD, ServerCalls.asyncBidiStreamingCall(new ServerCalls.BidiStreamingMethod<Integer, Integer>() {
int iteration;
@Override
public StreamObserver<Integer> invoke(StreamObserver<Integer> responseObserver) {
final ServerCallStreamObserver<Integer> serverCallObserver = (ServerCallStreamObserver<Integer>) responseObserver;
serverCallObserver.setOnReadyHandler(new Runnable() {
@Override
public void run() {
while (serverCallObserver.isReady()) {
serverCallObserver.onNext(iteration);
}
iteration++;
semaphore.release();
}
});
return new ServerCalls.NoopStreamObserver<Integer>() {
@Override
public void onCompleted() {
serverCallObserver.onCompleted();
}
};
}
})).build();
long tag = System.nanoTime();
InProcessServerBuilder.forName("go-with-the-flow" + tag).addService(service).build().start();
ManagedChannel channel = InProcessChannelBuilder.forName("go-with-the-flow" + tag).build();
final ClientCall<Integer, Integer> clientCall = channel.newCall(STREAMING_METHOD, CallOptions.DEFAULT);
final CountDownLatch latch = new CountDownLatch(1);
final int[] receivedMessages = new int[6];
clientCall.start(new ClientCall.Listener<Integer>() {
int index;
@Override
public void onMessage(Integer message) {
receivedMessages[index++] = message;
}
@Override
public void onClose(Status status, Metadata trailers) {
latch.countDown();
}
}, new Metadata());
semaphore.acquire();
clientCall.request(1);
semaphore.acquire();
clientCall.request(2);
semaphore.acquire();
clientCall.request(3);
clientCall.halfClose();
latch.await(5, TimeUnit.SECONDS);
// Very that number of messages produced in each onReady handler call matches the number
// requested by the client.
assertArrayEquals(new int[] { 0, 1, 1, 2, 2, 2 }, receivedMessages);
}
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);
}
Aggregations