use of io.grpc.ServerCall in project grpc-java by grpc.
the class ServerCallsTest method onCloseHandlerCalledIfSetInUnaryClientCall.
@Test
public void onCloseHandlerCalledIfSetInUnaryClientCall() throws Exception {
final AtomicBoolean onCloseHandlerCalled = new AtomicBoolean();
ServerCallHandler<Integer, Integer> callHandler = ServerCalls.asyncServerStreamingCall(new ServerCalls.ServerStreamingMethod<Integer, Integer>() {
@Override
public void invoke(Integer request, StreamObserver<Integer> responseObserver) {
ServerCallStreamObserver<Integer> serverCallObserver = (ServerCallStreamObserver<Integer>) responseObserver;
serverCallObserver.setOnCloseHandler(new Runnable() {
@Override
public void run() {
onCloseHandlerCalled.set(true);
}
});
}
});
ServerCall.Listener<Integer> callListener = callHandler.startCall(serverCall, new Metadata());
callListener.onMessage(0);
callListener.onHalfClose();
callListener.onComplete();
assertTrue(onCloseHandlerCalled.get());
}
use of io.grpc.ServerCall in project grpc-java by grpc.
the class ServerCallsTest method clientSendsOne_errorMissingRequest_serverStreaming.
@Test
public void clientSendsOne_errorMissingRequest_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.onHalfClose();
assertThat(serverCall.responses).isEmpty();
assertEquals(Status.Code.INTERNAL, serverCall.status.getCode());
assertEquals(ServerCalls.MISSING_REQUEST, serverCall.status.getDescription());
}
use of io.grpc.ServerCall in project grpc-java by grpc.
the class ServerCallsTest method noCancellationExceptionIfOnCancelHandlerSet.
@Test
public void noCancellationExceptionIfOnCancelHandlerSet() throws Exception {
final AtomicBoolean onCancelCalled = new AtomicBoolean();
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) {
ServerCallStreamObserver<Integer> serverCallObserver = (ServerCallStreamObserver<Integer>) responseObserver;
callObserver.set(serverCallObserver);
serverCallObserver.setOnCancelHandler(new Runnable() {
@Override
public void run() {
onCancelCalled.set(true);
}
});
return new ServerCalls.NoopStreamObserver<>();
}
});
ServerCall.Listener<Integer> callListener = callHandler.startCall(serverCall, new Metadata());
callListener.onReady();
callListener.onCancel();
assertTrue(onCancelCalled.get());
serverCall.isCancelled = true;
assertTrue(callObserver.get().isCancelled());
callObserver.get().onNext(null);
callObserver.get().onCompleted();
}
use of io.grpc.ServerCall in project grpc-java by grpc.
the class ServerCallsTest method disablingInboundAutoFlowControlSuppressesRequestsForMoreMessages.
@Test
public void disablingInboundAutoFlowControlSuppressesRequestsForMoreMessages() throws Exception {
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.disableAutoRequest();
return new ServerCalls.NoopStreamObserver<>();
}
});
ServerCall.Listener<Integer> callListener = callHandler.startCall(serverCall, new Metadata());
callListener.onReady();
// Transport should not call this if nothing has been requested but forcing it here
// to verify that message delivery does not trigger a call to request(1).
callListener.onMessage(1);
// Should never be called
assertThat(serverCall.requestCalls).isEmpty();
}
use of io.grpc.ServerCall in project grpc-java by grpc.
the class ServerCallsTest method expectCancellationExceptionIfOnCancelHandlerNotSet.
@Test
public void expectCancellationExceptionIfOnCancelHandlerNotSet() 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) {
ServerCallStreamObserver<Integer> serverCallObserver = (ServerCallStreamObserver<Integer>) responseObserver;
callObserver.set(serverCallObserver);
return new ServerCalls.NoopStreamObserver<>();
}
});
ServerCall.Listener<Integer> callListener = callHandler.startCall(serverCall, new Metadata());
callListener.onReady();
callListener.onCancel();
serverCall.isCancelled = true;
assertTrue(callObserver.get().isCancelled());
try {
callObserver.get().onNext(null);
fail("Expected cancellation exception when onCallHandler not set");
} catch (StatusRuntimeException expected) {
// Expected
}
// No exception
callObserver.get().onCompleted();
}
Aggregations