Search in sources :

Example 1 with CancellableContext

use of io.grpc.Context.CancellableContext in project grpc-java by grpc.

the class CascadingTest method testCascadingCancellationViaOuterContextCancellation.

/**
 * Test {@link Context} cancellation propagates from the first node in the call chain all the way
 * to the last.
 */
@Test
public void testCascadingCancellationViaOuterContextCancellation() throws Exception {
    observedCancellations = new CountDownLatch(2);
    receivedCancellations = new CountDownLatch(3);
    Future<?> chainReady = startChainingServer(3);
    CancellableContext context = Context.current().withCancellation();
    Future<SimpleResponse> future;
    Context prevContext = context.attach();
    try {
        future = futureStub.unaryCall(SimpleRequest.getDefaultInstance());
    } finally {
        context.detach(prevContext);
    }
    chainReady.get(5, TimeUnit.SECONDS);
    context.cancel(null);
    try {
        future.get(5, TimeUnit.SECONDS);
        fail("Expected cancellation");
    } catch (ExecutionException ex) {
        Status status = Status.fromThrowable(ex);
        assertEquals(Status.Code.CANCELLED, status.getCode());
        // Should have observed 2 cancellations responses from downstream servers
        if (!observedCancellations.await(5, TimeUnit.SECONDS)) {
            fail("Expected number of cancellations not observed by clients");
        }
        if (!receivedCancellations.await(5, TimeUnit.SECONDS)) {
            fail("Expected number of cancellations to be received by servers not observed");
        }
    }
}
Also used : Context(io.grpc.Context) CancellableContext(io.grpc.Context.CancellableContext) Status(io.grpc.Status) CancellableContext(io.grpc.Context.CancellableContext) SimpleResponse(io.grpc.testing.integration.Messages.SimpleResponse) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 2 with CancellableContext

use of io.grpc.Context.CancellableContext in project grpc-java by grpc.

the class GrpclbLoadBalancerTest method useIndependentRpcContext.

@Test
public void useIndependentRpcContext() {
    // Simulates making RPCs within the context of an inbound RPC.
    CancellableContext cancellableContext = Context.current().withCancellation();
    Context prevContext = cancellableContext.attach();
    try {
        List<EquivalentAddressGroup> backendList = createResolvedBackendAddresses(2);
        List<EquivalentAddressGroup> grpclbBalancerList = createResolvedBalancerAddresses(2);
        deliverResolvedAddresses(backendList, grpclbBalancerList);
        verify(helper).createOobChannel(eq(xattr(grpclbBalancerList)), eq(lbAuthority(0) + NO_USE_AUTHORITY_SUFFIX));
        verify(mockLbService).balanceLoad(lbResponseObserverCaptor.capture());
        StreamObserver<LoadBalanceResponse> lbResponseObserver = lbResponseObserverCaptor.getValue();
        assertEquals(1, lbRequestObservers.size());
        StreamObserver<LoadBalanceRequest> lbRequestObserver = lbRequestObservers.poll();
        verify(lbRequestObserver).onNext(eq(LoadBalanceRequest.newBuilder().setInitialRequest(InitialLoadBalanceRequest.newBuilder().setName(SERVICE_AUTHORITY).build()).build()));
        lbResponseObserver.onNext(buildInitialResponse());
        // The inbound RPC finishes and closes its context. The outbound RPC's control plane RPC
        // should not be impacted (no retry).
        cancellableContext.close();
        assertEquals(0, fakeClock.numPendingTasks(LB_RPC_RETRY_TASK_FILTER));
        verifyNoMoreInteractions(mockLbService);
    } finally {
        cancellableContext.detach(prevContext);
    }
}
Also used : SynchronizationContext(io.grpc.SynchronizationContext) Context(io.grpc.Context) CancellableContext(io.grpc.Context.CancellableContext) CancellableContext(io.grpc.Context.CancellableContext) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) LoadBalanceRequest(io.grpc.lb.v1.LoadBalanceRequest) InitialLoadBalanceRequest(io.grpc.lb.v1.InitialLoadBalanceRequest) InitialLoadBalanceResponse(io.grpc.lb.v1.InitialLoadBalanceResponse) LoadBalanceResponse(io.grpc.lb.v1.LoadBalanceResponse) Test(org.junit.Test)

Example 3 with CancellableContext

use of io.grpc.Context.CancellableContext in project grpc-java by grpc.

the class ClientXdsClientTestBase method useIndependentRpcContext.

@Test
public void useIndependentRpcContext() {
    // Simulates making RPCs within the context of an inbound RPC.
    CancellableContext cancellableContext = Context.current().withCancellation();
    Context prevContext = cancellableContext.attach();
    try {
        DiscoveryRpcCall call = startResourceWatcher(LDS, LDS_RESOURCE, ldsResourceWatcher);
        // The inbound RPC finishes and closes its context. The outbound RPC's control plane RPC
        // should not be impacted.
        cancellableContext.close();
        verify(ldsResourceWatcher, never()).onError(any(Status.class));
        call.sendResponse(LDS, testListenerRds, VERSION_1, "0000");
        verify(ldsResourceWatcher).onChanged(any(LdsUpdate.class));
    } finally {
        cancellableContext.detach(prevContext);
    }
}
Also used : Context(io.grpc.Context) CancellableContext(io.grpc.Context.CancellableContext) CommonTlsContext(io.envoyproxy.envoy.extensions.transport_sockets.tls.v3.CommonTlsContext) Status(io.grpc.Status) ResourceMetadataStatus(io.grpc.xds.XdsClient.ResourceMetadata.ResourceMetadataStatus) CancellableContext(io.grpc.Context.CancellableContext) LdsUpdate(io.grpc.xds.XdsClient.LdsUpdate) Test(org.junit.Test)

Example 4 with CancellableContext

use of io.grpc.Context.CancellableContext in project grpc-java by grpc.

the class HealthStatusManagerTest method watchRemovedWhenClientCloses.

@Test
public void watchRemovedWhenClientCloses() throws Exception {
    CancellableContext withCancellation = Context.current().withCancellation();
    Context prevCtx = withCancellation.attach();
    RespObserver respObs1 = new RespObserver();
    try {
        assertThat(service.numWatchersForTest(SERVICE1)).isEqualTo(0);
        stub.watch(HealthCheckRequest.newBuilder().setService(SERVICE1).build(), respObs1);
    } finally {
        withCancellation.detach(prevCtx);
    }
    RespObserver respObs1b = new RespObserver();
    stub.watch(HealthCheckRequest.newBuilder().setService(SERVICE1).build(), respObs1b);
    RespObserver respObs2 = new RespObserver();
    stub.watch(HealthCheckRequest.newBuilder().setService(SERVICE2).build(), respObs2);
    assertThat(respObs1.responses.poll()).isEqualTo(HealthCheckResponse.newBuilder().setStatus(ServingStatus.SERVICE_UNKNOWN).build());
    assertThat(respObs1b.responses.poll()).isEqualTo(HealthCheckResponse.newBuilder().setStatus(ServingStatus.SERVICE_UNKNOWN).build());
    assertThat(respObs2.responses.poll()).isEqualTo(HealthCheckResponse.newBuilder().setStatus(ServingStatus.SERVICE_UNKNOWN).build());
    assertThat(service.numWatchersForTest(SERVICE1)).isEqualTo(2);
    assertThat(service.numWatchersForTest(SERVICE2)).isEqualTo(1);
    assertThat(respObs1.responses).isEmpty();
    assertThat(respObs1b.responses).isEmpty();
    assertThat(respObs2.responses).isEmpty();
    // This will cancel the RPC with respObs1
    withCancellation.close();
    assertThat(respObs1.responses.poll()).isInstanceOf(Throwable.class);
    assertThat(service.numWatchersForTest(SERVICE1)).isEqualTo(1);
    assertThat(service.numWatchersForTest(SERVICE2)).isEqualTo(1);
    assertThat(respObs1.responses).isEmpty();
    assertThat(respObs1b.responses).isEmpty();
    assertThat(respObs2.responses).isEmpty();
}
Also used : Context(io.grpc.Context) CancellableContext(io.grpc.Context.CancellableContext) CancellableContext(io.grpc.Context.CancellableContext) Test(org.junit.Test)

Aggregations

Context (io.grpc.Context)4 CancellableContext (io.grpc.Context.CancellableContext)4 Test (org.junit.Test)4 Status (io.grpc.Status)2 CommonTlsContext (io.envoyproxy.envoy.extensions.transport_sockets.tls.v3.CommonTlsContext)1 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)1 SynchronizationContext (io.grpc.SynchronizationContext)1 InitialLoadBalanceRequest (io.grpc.lb.v1.InitialLoadBalanceRequest)1 InitialLoadBalanceResponse (io.grpc.lb.v1.InitialLoadBalanceResponse)1 LoadBalanceRequest (io.grpc.lb.v1.LoadBalanceRequest)1 LoadBalanceResponse (io.grpc.lb.v1.LoadBalanceResponse)1 SimpleResponse (io.grpc.testing.integration.Messages.SimpleResponse)1 LdsUpdate (io.grpc.xds.XdsClient.LdsUpdate)1 ResourceMetadataStatus (io.grpc.xds.XdsClient.ResourceMetadata.ResourceMetadataStatus)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1