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());
}
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