Search in sources :

Example 1 with ServerCallExecutorSupplier

use of io.grpc.ServerCallExecutorSupplier in project grpc-java by grpc.

the class ServerImplTest method executorSupplierSameExecutorBasic.

@Test
public void executorSupplierSameExecutorBasic() throws Exception {
    builder.executorSupplier = new ServerCallExecutorSupplier() {

        @Override
        public <ReqT, RespT> Executor getExecutor(ServerCall<ReqT, RespT> call, Metadata metadata) {
            return executor.getScheduledExecutorService();
        }
    };
    basicExchangeSuccessful();
}
Also used : ServerCallExecutorSupplier(io.grpc.ServerCallExecutorSupplier) Executor(java.util.concurrent.Executor) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 2 with ServerCallExecutorSupplier

use of io.grpc.ServerCallExecutorSupplier in project grpc-java by grpc.

the class ServerImplTest method executorSupplierSwitchExecutor.

@Test
@SuppressWarnings("unchecked")
public void executorSupplierSwitchExecutor() throws Exception {
    SingleExecutor switchingExecutor = new SingleExecutor();
    ServerCallExecutorSupplier mockSupplier = mock(ServerCallExecutorSupplier.class);
    when(mockSupplier.getExecutor(any(ServerCall.class), any(Metadata.class))).thenReturn(switchingExecutor);
    builder.executorSupplier = mockSupplier;
    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);
    verify(streamTracerFactory).newServerStreamTracer(eq("Waiter/serve"), same(requestHeaders));
    ArgumentCaptor<ServerCall<?, ?>> callCapture = ArgumentCaptor.forClass(ServerCall.class);
    verify(mockSupplier).getExecutor(callCapture.capture(), eq(requestHeaders));
    assertThat(switchingExecutor.runnable).isNotNull();
    assertEquals(0, executor.numPendingTasks());
    switchingExecutor.drain();
    ServerCall<String, Integer> call = callReference.get();
    assertNotNull(call);
    assertThat(call).isEqualTo(callCapture.getValue());
}
Also used : 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) ServerCall(io.grpc.ServerCall) ServiceDescriptor(io.grpc.ServiceDescriptor) JumpToApplicationThreadServerStreamListener(io.grpc.internal.ServerImpl.JumpToApplicationThreadServerStreamListener) Test(org.junit.Test)

Example 3 with ServerCallExecutorSupplier

use of io.grpc.ServerCallExecutorSupplier in project grpc-java by grpc.

the class ServerImplTest method executorSupplierNullBasic.

@Test
public void executorSupplierNullBasic() throws Exception {
    builder.executorSupplier = new ServerCallExecutorSupplier() {

        @Override
        public <ReqT, RespT> Executor getExecutor(ServerCall<ReqT, RespT> call, Metadata metadata) {
            return null;
        }
    };
    basicExchangeSuccessful();
}
Also used : ServerCallExecutorSupplier(io.grpc.ServerCallExecutorSupplier) Executor(java.util.concurrent.Executor) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 4 with ServerCallExecutorSupplier

use of io.grpc.ServerCallExecutorSupplier 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)

Aggregations

Metadata (io.grpc.Metadata)4 ServerCallExecutorSupplier (io.grpc.ServerCallExecutorSupplier)4 Test (org.junit.Test)4 Executor (java.util.concurrent.Executor)3 ServerCall (io.grpc.ServerCall)2 Listener (io.grpc.ServerCall.Listener)2 ServiceDescriptor (io.grpc.ServiceDescriptor)2 JumpToApplicationThreadServerStreamListener (io.grpc.internal.ServerImpl.JumpToApplicationThreadServerStreamListener)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 Status (io.grpc.Status)1