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();
}
use of io.grpc.ServerCall in project grpc-java by grpc.
the class ServerCallsTest method clientSendsOne_errorTooManyRequests_unary.
@Test
public void clientSendsOne_errorTooManyRequests_unary() {
ServerCallRecorder serverCall = new ServerCallRecorder(UNARY_METHOD);
ServerCallHandler<Integer, Integer> callHandler = ServerCalls.asyncUnaryCall(new ServerCalls.UnaryMethod<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();
}
use of io.grpc.ServerCall in project grpc-java by grpc.
the class ServerCallsTest method cannotSetOnReadyHandlerAfterServiceInvocation.
@Test
public void cannotSetOnReadyHandlerAfterServiceInvocation() 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().setOnReadyHandler(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_errorMissingRequest_unary.
@Test
public void clientSendsOne_errorMissingRequest_unary() {
ServerCallRecorder serverCall = new ServerCallRecorder(UNARY_METHOD);
ServerCallHandler<Integer, Integer> callHandler = ServerCalls.asyncUnaryCall(new ServerCalls.UnaryMethod<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 cannotSetOnCancelHandlerAfterServiceInvocation.
@Test
public void cannotSetOnCancelHandlerAfterServiceInvocation() 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().setOnCancelHandler(new Runnable() {
@Override
public void run() {
}
});
fail("Cannot set onCancel handler after service invocation");
} catch (IllegalStateException expected) {
// Expected
}
}
Aggregations