Search in sources :

Example 36 with ServerCall

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);
}
Also used : Status(io.grpc.Status) ServerCallExecutorSupplier(io.grpc.ServerCallExecutorSupplier) JumpToApplicationThreadServerStreamListener(io.grpc.internal.ServerImpl.JumpToApplicationThreadServerStreamListener) Listener(io.grpc.ServerCall.Listener) Metadata(io.grpc.Metadata) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Executor(java.util.concurrent.Executor) ServerCall(io.grpc.ServerCall) ServiceDescriptor(io.grpc.ServiceDescriptor) JumpToApplicationThreadServerStreamListener(io.grpc.internal.ServerImpl.JumpToApplicationThreadServerStreamListener) Test(org.junit.Test)

Example 37 with ServerCall

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

Example 38 with ServerCall

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

Example 39 with ServerCall

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
    }
}
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 40 with ServerCall

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