Search in sources :

Example 41 with ServerCall

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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServerCall(io.grpc.ServerCall) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 42 with ServerCall

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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServerCall(io.grpc.ServerCall) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 43 with ServerCall

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();
}
Also used : Metadata(io.grpc.Metadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServerCall(io.grpc.ServerCall) Test(org.junit.Test)

Example 44 with ServerCall

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();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServerCall(io.grpc.ServerCall) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 45 with ServerCall

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();
}
Also used : Metadata(io.grpc.Metadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServerCall(io.grpc.ServerCall) StatusRuntimeException(io.grpc.StatusRuntimeException) Test(org.junit.Test)

Aggregations

ServerCall (io.grpc.ServerCall)52 Metadata (io.grpc.Metadata)48 Test (org.junit.Test)42 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)26 AtomicReference (java.util.concurrent.atomic.AtomicReference)19 ServerCallHandler (io.grpc.ServerCallHandler)14 Status (io.grpc.Status)14 Context (io.grpc.Context)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 JumpToApplicationThreadServerStreamListener (io.grpc.internal.ServerImpl.JumpToApplicationThreadServerStreamListener)10 Server (io.grpc.Server)9 ServerInterceptor (io.grpc.ServerInterceptor)9 ServiceDescriptor (io.grpc.ServiceDescriptor)9 IOException (java.io.IOException)8 ExecutionException (java.util.concurrent.ExecutionException)8 StatusException (io.grpc.StatusException)7 TimeoutException (java.util.concurrent.TimeoutException)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)7 Listener (io.grpc.ServerCall.Listener)6 StatusRuntimeException (io.grpc.StatusRuntimeException)5