use of io.grpc.ServerCall in project grpc-java by grpc.
the class ServerImplTest method executorSupplierFutureNotSet.
@Test
@SuppressWarnings("CheckReturnValue")
public void executorSupplierFutureNotSet() throws Exception {
builder.executorSupplier = new ServerCallExecutorSupplier() {
@Override
public <ReqT, RespT> Executor getExecutor(ServerCall<ReqT, RespT> call, Metadata metadata) {
throw new IllegalStateException("Yeah!");
}
};
doThrow(new IllegalStateException("Yeah")).doNothing().when(stream).close(any(Status.class), any(Metadata.class));
final AtomicReference<ServerCall<String, Integer>> callReference = new AtomicReference<>();
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());
createAndStartServer();
ServerTransportListener transportListener = transportServer.registerNewServerTransport(new SimpleServerTransport());
transportListener.transportReady(Attributes.EMPTY);
Metadata requestHeaders = new Metadata();
StatsTraceContext statsTraceCtx = StatsTraceContext.newServerContext(streamTracerFactories, "Waiter/serve", requestHeaders);
when(stream.statsTraceContext()).thenReturn(statsTraceCtx);
transportListener.streamCreated(stream, "Waiter/serve", requestHeaders);
verify(stream).setListener(isA(ServerStreamListener.class));
verify(stream, atLeast(1)).statsTraceContext();
assertEquals(1, executor.runDueTasks());
verify(fallbackRegistry).lookupMethod("Waiter/serve", AUTHORITY);
assertThat(callReference.get()).isNull();
verify(stream, times(2)).close(statusCaptor.capture(), any(Metadata.class));
Status status = statusCaptor.getAllValues().get(1);
assertEquals(Code.UNKNOWN, status.getCode());
assertThat(status.getCause() instanceof IllegalStateException);
}
use of io.grpc.ServerCall in project grpc-java by grpc.
the class ServerImplTest method testStreamClose_clientOkTriggersDelayedCancellation.
@Test
public void testStreamClose_clientOkTriggersDelayedCancellation() throws Exception {
AtomicBoolean contextCancelled = new AtomicBoolean(false);
AtomicReference<Context> context = new AtomicReference<>();
AtomicReference<ServerCall<String, Integer>> callReference = new AtomicReference<>();
ServerStreamListener streamListener = testStreamClose_setup(callReference, context, contextCancelled, null);
// For close status OK:
// The context isCancelled is expected to be true after all pending work is done,
// but for the call it should be false as it gets set cancelled only if the call
// fails with a non-OK status.
assertFalse(callReference.get().isCancelled());
assertFalse(context.get().isCancelled());
streamListener.closed(Status.OK);
assertFalse(callReference.get().isCancelled());
assertFalse(context.get().isCancelled());
assertEquals(1, executor.runDueTasks());
assertFalse(callReference.get().isCancelled());
assertTrue(context.get().isCancelled());
assertTrue(contextCancelled.get());
}
use of io.grpc.ServerCall in project grpc-java by grpc.
the class ServerCallsTest method onCloseHandlerCalledIfSetInStreamingClientCall.
@Test
public void onCloseHandlerCalledIfSetInStreamingClientCall() throws Exception {
final AtomicBoolean onCloseHandlerCalled = new AtomicBoolean();
ServerCallHandler<Integer, Integer> callHandler = ServerCalls.asyncBidiStreamingCall(new ServerCalls.BidiStreamingMethod<Integer, Integer>() {
@Override
public StreamObserver<Integer> invoke(StreamObserver<Integer> responseObserver) {
ServerCallStreamObserver<Integer> serverCallObserver = (ServerCallStreamObserver<Integer>) responseObserver;
serverCallObserver.setOnCloseHandler(new Runnable() {
@Override
public void run() {
onCloseHandlerCalled.set(true);
}
});
return new ServerCalls.NoopStreamObserver<>();
}
});
ServerCall.Listener<Integer> callListener = callHandler.startCall(serverCall, new Metadata());
callListener.onComplete();
assertTrue(onCloseHandlerCalled.get());
}
use of io.grpc.ServerCall in project grpc-java by grpc.
the class ServerCallsTest method cannotSetOnCloseHandlerAfterServiceInvocation.
@Test
public void cannotSetOnCloseHandlerAfterServiceInvocation() throws Exception {
final AtomicReference<ServerCallStreamObserver<Integer>> callObserver = new AtomicReference<>();
ServerCallHandler<Integer, Integer> callHandler = ServerCalls.asyncBidiStreamingCall(new ServerCalls.BidiStreamingMethod<Integer, Integer>() {
@Override
public StreamObserver<Integer> invoke(StreamObserver<Integer> responseObserver) {
callObserver.set((ServerCallStreamObserver<Integer>) responseObserver);
return new ServerCalls.NoopStreamObserver<>();
}
});
ServerCall.Listener<Integer> callListener = callHandler.startCall(serverCall, new Metadata());
callListener.onMessage(1);
try {
callObserver.get().setOnCloseHandler(new Runnable() {
@Override
public void run() {
}
});
fail("Cannot set onReady after service invocation");
} catch (IllegalStateException expected) {
// Expected
}
}
use of io.grpc.ServerCall in project grpc-java by grpc.
the class ServerCallsTest method clientSendsOne_errorTooManyRequests_serverStreaming.
@Test
public void clientSendsOne_errorTooManyRequests_serverStreaming() {
ServerCallRecorder serverCall = new ServerCallRecorder(SERVER_STREAMING_METHOD);
ServerCallHandler<Integer, Integer> callHandler = ServerCalls.asyncServerStreamingCall(new ServerCalls.ServerStreamingMethod<Integer, Integer>() {
@Override
public void invoke(Integer req, StreamObserver<Integer> responseObserver) {
fail("should not be reached");
}
});
ServerCall.Listener<Integer> listener = callHandler.startCall(serverCall, new Metadata());
listener.onMessage(1);
listener.onMessage(1);
assertThat(serverCall.responses).isEmpty();
assertEquals(Status.Code.INTERNAL, serverCall.status.getCode());
assertEquals(ServerCalls.TOO_MANY_REQUESTS, serverCall.status.getDescription());
// ensure onHalfClose does not invoke
listener.onHalfClose();
}
Aggregations