Search in sources :

Example 46 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)

Example 47 with ServerCall

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

Example 48 with ServerCall

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

Example 49 with ServerCall

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

Example 50 with ServerCall

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