Search in sources :

Example 26 with ServerCall

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

the class XdsServerWrapperTest method buildInterceptor_inline.

@Test
@SuppressWarnings("unchecked")
public void buildInterceptor_inline() throws Exception {
    final SettableFuture<Server> start = SettableFuture.create();
    Executors.newSingleThreadExecutor().execute(new Runnable() {

        @Override
        public void run() {
            try {
                start.set(xdsServerWrapper.start());
            } catch (Exception ex) {
                start.setException(ex);
            }
        }
    });
    xdsClient.ldsResource.get(5, TimeUnit.SECONDS);
    RouteMatch routeMatch = RouteMatch.create(PathMatcher.fromPath("/FooService/barMethod", true), Collections.<HeaderMatcher>emptyList(), null);
    Filter filter = mock(Filter.class, withSettings().extraInterfaces(ServerInterceptorBuilder.class));
    when(filter.typeUrls()).thenReturn(new String[] { "filter-type-url" });
    filterRegistry.register(filter);
    FilterConfig f0 = mock(FilterConfig.class);
    FilterConfig f0Override = mock(FilterConfig.class);
    when(f0.typeUrl()).thenReturn("filter-type-url");
    final List<Integer> interceptorTrace = new ArrayList<>();
    ServerInterceptor interceptor0 = new ServerInterceptor() {

        @Override
        public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
            interceptorTrace.add(0);
            return next.startCall(call, headers);
        }
    };
    ServerInterceptor interceptor1 = new ServerInterceptor() {

        @Override
        public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
            interceptorTrace.add(1);
            return next.startCall(call, headers);
        }
    };
    when(((ServerInterceptorBuilder) filter).buildServerInterceptor(f0, null)).thenReturn(interceptor0);
    when(((ServerInterceptorBuilder) filter).buildServerInterceptor(f0, f0Override)).thenReturn(interceptor1);
    Route route = Route.forAction(routeMatch, null, ImmutableMap.<String, FilterConfig>of());
    VirtualHost virtualHost = VirtualHost.create("v1", Collections.singletonList("foo.google.com"), Arrays.asList(route), ImmutableMap.of("filter-config-name-0", f0Override));
    HttpConnectionManager hcmVirtual = HttpConnectionManager.forVirtualHosts(0L, Collections.singletonList(virtualHost), Arrays.asList(new NamedFilterConfig("filter-config-name-0", f0), new NamedFilterConfig("filter-config-name-1", f0)));
    EnvoyServerProtoData.FilterChain filterChain = createFilterChain("filter-chain-0", hcmVirtual);
    xdsClient.deliverLdsUpdate(Collections.singletonList(filterChain), null);
    start.get(5000, TimeUnit.MILLISECONDS);
    verify(mockServer).start();
    assertThat(selectorManager.getSelectorToUpdateSelector().getRoutingConfigs().size()).isEqualTo(1);
    ServerInterceptor realInterceptor = selectorManager.getSelectorToUpdateSelector().getRoutingConfigs().get(filterChain).get().interceptors().get(route);
    assertThat(realInterceptor).isNotNull();
    ServerCall<Void, Void> serverCall = mock(ServerCall.class);
    ServerCallHandler<Void, Void> mockNext = mock(ServerCallHandler.class);
    final ServerCall.Listener<Void> listener = new ServerCall.Listener<Void>() {
    };
    when(mockNext.startCall(any(ServerCall.class), any(Metadata.class))).thenReturn(listener);
    realInterceptor.interceptCall(serverCall, new Metadata(), mockNext);
    assertThat(interceptorTrace).isEqualTo(Arrays.asList(1, 0));
    verify(mockNext).startCall(eq(serverCall), any(Metadata.class));
}
Also used : XdsServingStatusListener(io.grpc.xds.XdsServerBuilder.XdsServingStatusListener) Server(io.grpc.Server) ServerCallHandler(io.grpc.ServerCallHandler) ArrayList(java.util.ArrayList) Metadata(io.grpc.Metadata) NamedFilterConfig(io.grpc.xds.Filter.NamedFilterConfig) ServerCall(io.grpc.ServerCall) FilterConfig(io.grpc.xds.Filter.FilterConfig) NamedFilterConfig(io.grpc.xds.Filter.NamedFilterConfig) ServerInterceptorBuilder(io.grpc.xds.Filter.ServerInterceptorBuilder) Route(io.grpc.xds.VirtualHost.Route) TimeoutException(java.util.concurrent.TimeoutException) StatusException(io.grpc.StatusException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) RouteMatch(io.grpc.xds.VirtualHost.Route.RouteMatch) ServerInterceptor(io.grpc.ServerInterceptor) FilterChain(io.grpc.xds.EnvoyServerProtoData.FilterChain) Test(org.junit.Test)

Example 27 with ServerCall

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

the class XdsServerWrapperTest method interceptor_failingRouterConfig.

@Test
@SuppressWarnings("unchecked")
public void interceptor_failingRouterConfig() throws Exception {
    ArgumentCaptor<ConfigApplyingInterceptor> interceptorCaptor = ArgumentCaptor.forClass(ConfigApplyingInterceptor.class);
    final SettableFuture<Server> start = SettableFuture.create();
    Executors.newSingleThreadExecutor().execute(new Runnable() {

        @Override
        public void run() {
            try {
                start.set(xdsServerWrapper.start());
            } catch (Exception ex) {
                start.setException(ex);
            }
        }
    });
    xdsClient.ldsResource.get(5, TimeUnit.SECONDS);
    verify(mockBuilder).intercept(interceptorCaptor.capture());
    ConfigApplyingInterceptor interceptor = interceptorCaptor.getValue();
    ServerCall<Void, Void> serverCall = mock(ServerCall.class);
    when(serverCall.getAttributes()).thenReturn(Attributes.newBuilder().set(ATTR_SERVER_ROUTING_CONFIG, new AtomicReference<>(ServerRoutingConfig.FAILING_ROUTING_CONFIG)).build());
    ServerCallHandler<Void, Void> next = mock(ServerCallHandler.class);
    interceptor.interceptCall(serverCall, new Metadata(), next);
    verify(next, never()).startCall(any(ServerCall.class), any(Metadata.class));
    ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
    verify(serverCall).close(statusCaptor.capture(), any(Metadata.class));
    Status status = statusCaptor.getValue();
    assertThat(status.getCode()).isEqualTo(Status.UNAVAILABLE.getCode());
    assertThat(status.getDescription()).isEqualTo("Missing or broken xDS routing config: RDS config unavailable.");
}
Also used : Status(io.grpc.Status) Server(io.grpc.Server) Metadata(io.grpc.Metadata) TimeoutException(java.util.concurrent.TimeoutException) StatusException(io.grpc.StatusException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ConfigApplyingInterceptor(io.grpc.xds.XdsServerWrapper.ConfigApplyingInterceptor) ServerCall(io.grpc.ServerCall) Test(org.junit.Test)

Example 28 with ServerCall

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

the class TransmitStatusRuntimeExceptionInterceptor method interceptCall.

@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
    final ServerCall<ReqT, RespT> serverCall = new SerializingServerCall<>(call);
    ServerCall.Listener<ReqT> listener = next.startCall(serverCall, headers);
    return new ForwardingServerCallListener.SimpleForwardingServerCallListener<ReqT>(listener) {

        @Override
        public void onMessage(ReqT message) {
            try {
                super.onMessage(message);
            } catch (StatusRuntimeException e) {
                closeWithException(e);
            }
        }

        @Override
        public void onHalfClose() {
            try {
                super.onHalfClose();
            } catch (StatusRuntimeException e) {
                closeWithException(e);
            }
        }

        @Override
        public void onCancel() {
            try {
                super.onCancel();
            } catch (StatusRuntimeException e) {
                closeWithException(e);
            }
        }

        @Override
        public void onComplete() {
            try {
                super.onComplete();
            } catch (StatusRuntimeException e) {
                closeWithException(e);
            }
        }

        @Override
        public void onReady() {
            try {
                super.onReady();
            } catch (StatusRuntimeException e) {
                closeWithException(e);
            }
        }

        private void closeWithException(StatusRuntimeException t) {
            Metadata metadata = t.getTrailers();
            if (metadata == null) {
                metadata = new Metadata();
            }
            serverCall.close(t.getStatus(), metadata);
        }
    };
}
Also used : ForwardingServerCall(io.grpc.ForwardingServerCall) ServerCall(io.grpc.ServerCall) StatusRuntimeException(io.grpc.StatusRuntimeException) Metadata(io.grpc.Metadata)

Example 29 with ServerCall

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

the class InProcessTransportTest method methodNotFound.

@Test
public void methodNotFound() throws Exception {
    server = null;
    ServerServiceDefinition definition = ServerServiceDefinition.builder("service_foo").addMethod(TestMethodDescriptors.voidMethod(), new ServerCallHandler<Void, Void>() {

        @Override
        public Listener<Void> startCall(ServerCall<Void, Void> call, Metadata headers) {
            return null;
        }
    }).build();
    Server failingServer = InProcessServerBuilder.forName("nocall-service").addService(definition).directExecutor().build().start();
    grpcCleanupRule.register(failingServer);
    ManagedChannel channel = InProcessChannelBuilder.forName("nocall-service").propagateCauseWithStatus(true).build();
    grpcCleanupRule.register(channel);
    MethodDescriptor<Void, Void> nonMatchMethod = MethodDescriptor.<Void, Void>newBuilder().setType(MethodDescriptor.MethodType.UNKNOWN).setFullMethodName("Waiter/serve").setRequestMarshaller(TestMethodDescriptors.voidMarshaller()).setResponseMarshaller(TestMethodDescriptors.voidMarshaller()).build();
    ClientCall<Void, Void> call = channel.newCall(nonMatchMethod, CallOptions.DEFAULT);
    try {
        ClientCalls.futureUnaryCall(call, null).get(5, TimeUnit.SECONDS);
        fail("Call should fail.");
    } catch (ExecutionException ex) {
        StatusRuntimeException s = (StatusRuntimeException) ex.getCause();
        assertEquals(s.getStatus().getCode(), Code.UNIMPLEMENTED);
    }
}
Also used : ServerCallHandler(io.grpc.ServerCallHandler) InternalServer(io.grpc.internal.InternalServer) Server(io.grpc.Server) ServerCall(io.grpc.ServerCall) ServerServiceDefinition(io.grpc.ServerServiceDefinition) Metadata(io.grpc.Metadata) StatusRuntimeException(io.grpc.StatusRuntimeException) ManagedChannel(io.grpc.ManagedChannel) ExecutionException(java.util.concurrent.ExecutionException) AbstractTransportTest(io.grpc.internal.AbstractTransportTest) Test(org.junit.Test)

Example 30 with ServerCall

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

the class InProcessTransportTest method causeShouldBePropagatedWithStatus.

@Test
public void causeShouldBePropagatedWithStatus() throws Exception {
    server = null;
    String failingServerName = "server_foo";
    String serviceFoo = "service_foo";
    final Status s = Status.INTERNAL.withCause(new Throwable("failing server exception"));
    ServerServiceDefinition definition = ServerServiceDefinition.builder(serviceFoo).addMethod(TestMethodDescriptors.voidMethod(), new ServerCallHandler<Void, Void>() {

        @Override
        public ServerCall.Listener<Void> startCall(ServerCall<Void, Void> call, Metadata headers) {
            call.close(s, new Metadata());
            return new ServerCall.Listener<Void>() {
            };
        }
    }).build();
    Server failingServer = InProcessServerBuilder.forName(failingServerName).addService(definition).directExecutor().build().start();
    grpcCleanupRule.register(failingServer);
    ManagedChannel channel = InProcessChannelBuilder.forName(failingServerName).propagateCauseWithStatus(true).build();
    grpcCleanupRule.register(channel);
    try {
        ClientCalls.blockingUnaryCall(channel, TestMethodDescriptors.voidMethod(), CallOptions.DEFAULT, null);
        fail("exception should have been thrown");
    } catch (StatusRuntimeException e) {
        // When propagateCauseWithStatus is true, the cause should be sent forward
        assertEquals(s.getCause(), e.getCause());
    }
}
Also used : Status(io.grpc.Status) Listener(io.grpc.ServerCall.Listener) ServerCallHandler(io.grpc.ServerCallHandler) InternalServer(io.grpc.internal.InternalServer) Server(io.grpc.Server) ServerCall(io.grpc.ServerCall) ServerServiceDefinition(io.grpc.ServerServiceDefinition) Metadata(io.grpc.Metadata) StatusRuntimeException(io.grpc.StatusRuntimeException) ManagedChannel(io.grpc.ManagedChannel) AbstractTransportTest(io.grpc.internal.AbstractTransportTest) 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