Search in sources :

Example 36 with ClientStreamTracer

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

the class ManagedChannelImplTest method idleMode_resetsDelayedTransportPicker.

@Test
public void idleMode_resetsDelayedTransportPicker() {
    ClientStream mockStream = mock(ClientStream.class);
    Status pickError = Status.UNAVAILABLE.withDescription("pick result error");
    long idleTimeoutMillis = 1000L;
    channelBuilder.idleTimeout(idleTimeoutMillis, TimeUnit.MILLISECONDS);
    channelBuilder.nameResolverFactory(new FakeNameResolverFactory.Builder(expectedUri).setServers(Collections.singletonList(new EquivalentAddressGroup(socketAddress))).build());
    createChannel();
    assertEquals(IDLE, channel.getState(false));
    // This call will be buffered in delayedTransport
    ClientCall<String, Integer> call = channel.newCall(method, CallOptions.DEFAULT);
    call.start(mockCallListener, new Metadata());
    // Move channel into TRANSIENT_FAILURE, which will fail the pending call
    when(mockPicker.pickSubchannel(any(PickSubchannelArgs.class))).thenReturn(PickResult.withError(pickError));
    updateBalancingStateSafely(helper, TRANSIENT_FAILURE, mockPicker);
    assertEquals(TRANSIENT_FAILURE, channel.getState(false));
    executor.runDueTasks();
    verify(mockCallListener).onClose(same(pickError), any(Metadata.class));
    // Move channel to idle
    timer.forwardNanos(TimeUnit.MILLISECONDS.toNanos(idleTimeoutMillis));
    assertEquals(IDLE, channel.getState(false));
    // This call should be buffered, but will move the channel out of idle
    ClientCall<String, Integer> call2 = channel.newCall(method, CallOptions.DEFAULT);
    call2.start(mockCallListener2, new Metadata());
    executor.runDueTasks();
    verifyNoMoreInteractions(mockCallListener2);
    // Get the helper created on exiting idle
    ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(Helper.class);
    verify(mockLoadBalancerProvider, times(2)).newLoadBalancer(helperCaptor.capture());
    Helper helper2 = helperCaptor.getValue();
    // Establish a connection
    Subchannel subchannel = createSubchannelSafely(helper2, addressGroup, Attributes.EMPTY, subchannelStateListener);
    requestConnectionSafely(helper, subchannel);
    MockClientTransportInfo transportInfo = transports.poll();
    ConnectionClientTransport mockTransport = transportInfo.transport;
    ManagedClientTransport.Listener transportListener = transportInfo.listener;
    when(mockTransport.newStream(same(method), any(Metadata.class), any(CallOptions.class), ArgumentMatchers.<ClientStreamTracer[]>any())).thenReturn(mockStream);
    transportListener.transportReady();
    when(mockPicker.pickSubchannel(any(PickSubchannelArgs.class))).thenReturn(PickResult.withSubchannel(subchannel));
    updateBalancingStateSafely(helper2, READY, mockPicker);
    assertEquals(READY, channel.getState(false));
    executor.runDueTasks();
    // Verify the buffered call was drained
    verify(mockTransport).newStream(same(method), any(Metadata.class), any(CallOptions.class), ArgumentMatchers.<ClientStreamTracer[]>any());
    verify(mockStream).start(any(ClientStreamListener.class));
}
Also used : Status(io.grpc.Status) ClientStreamTracer(io.grpc.ClientStreamTracer) Metadata(io.grpc.Metadata) MockClientTransportInfo(io.grpc.internal.TestUtils.MockClientTransportInfo) CallOptions(io.grpc.CallOptions) Helper(io.grpc.LoadBalancer.Helper) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) ForwardingSubchannel(io.grpc.util.ForwardingSubchannel) Subchannel(io.grpc.LoadBalancer.Subchannel) PickSubchannelArgs(io.grpc.LoadBalancer.PickSubchannelArgs) Test(org.junit.Test)

Example 37 with ClientStreamTracer

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

the class OrcaPerRequestUtilTest method onlyParentPolicyReceivesReportsIfCreatesOwnTracer.

/**
 * Tests the case when parent policy creates its own {@link ClientStreamTracer.Factory}, ORCA
 * reports are only forwarded to the parent's listener.
 */
@Test
public void onlyParentPolicyReceivesReportsIfCreatesOwnTracer() {
    ClientStreamTracer.Factory parentFactory = OrcaPerRequestUtil.getInstance().newOrcaClientStreamTracerFactory(orcaListener1);
    ClientStreamTracer.Factory childFactory = mock(ClientStreamTracer.Factory.class, delegatesTo(OrcaPerRequestUtil.getInstance().newOrcaClientStreamTracerFactory(parentFactory, orcaListener2)));
    ClientStreamTracer parentTracer = parentFactory.newClientStreamTracer(STREAM_INFO, new Metadata());
    Metadata trailer = new Metadata();
    trailer.put(OrcaReportingTracerFactory.ORCA_ENDPOINT_LOAD_METRICS_KEY, OrcaLoadReport.getDefaultInstance());
    parentTracer.inboundTrailers(trailer);
    verify(orcaListener1).onLoadReport(eq(OrcaLoadReport.getDefaultInstance()));
    verifyNoInteractions(childFactory);
    verifyNoInteractions(orcaListener2);
}
Also used : ClientStreamTracer(io.grpc.ClientStreamTracer) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 38 with ClientStreamTracer

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

the class OrcaPerRequestUtilTest method twoLevelPoliciesTypicalWorkflow.

/**
 * Tests parent-child load balance policies' listeners both receive per-request ORCA reports upon
 * call trailer arrives and ORCA report deserialization happens only once.
 */
@Test
public void twoLevelPoliciesTypicalWorkflow() {
    ClientStreamTracer.Factory parentFactory = mock(ClientStreamTracer.Factory.class, delegatesTo(OrcaPerRequestUtil.getInstance().newOrcaClientStreamTracerFactory(orcaListener1)));
    ClientStreamTracer.Factory childFactory = OrcaPerRequestUtil.getInstance().newOrcaClientStreamTracerFactory(parentFactory, orcaListener2);
    // Child factory will augment the StreamInfo and pass it to the parent factory.
    ClientStreamTracer childTracer = childFactory.newClientStreamTracer(STREAM_INFO, new Metadata());
    ArgumentCaptor<ClientStreamTracer.StreamInfo> streamInfoCaptor = ArgumentCaptor.forClass(null);
    verify(parentFactory).newClientStreamTracer(streamInfoCaptor.capture(), any(Metadata.class));
    ClientStreamTracer.StreamInfo parentStreamInfo = streamInfoCaptor.getValue();
    assertThat(parentStreamInfo).isNotEqualTo(STREAM_INFO);
    // When the trailer does not contain ORCA report, no listener callback will be invoked.
    Metadata trailer = new Metadata();
    childTracer.inboundTrailers(trailer);
    verifyNoMoreInteractions(orcaListener1);
    verifyNoMoreInteractions(orcaListener2);
    // When the trailer contains an ORCA report, callbacks for both listeners will be invoked.
    // Both listener will receive the same ORCA report instance, which means deserialization
    // happens only once.
    trailer.put(OrcaReportingTracerFactory.ORCA_ENDPOINT_LOAD_METRICS_KEY, OrcaLoadReport.getDefaultInstance());
    childTracer.inboundTrailers(trailer);
    ArgumentCaptor<OrcaLoadReport> parentReportCap = ArgumentCaptor.forClass(null);
    ArgumentCaptor<OrcaLoadReport> childReportCap = ArgumentCaptor.forClass(null);
    verify(orcaListener1).onLoadReport(parentReportCap.capture());
    verify(orcaListener2).onLoadReport(childReportCap.capture());
    assertThat(parentReportCap.getValue()).isEqualTo(OrcaLoadReport.getDefaultInstance());
    assertThat(childReportCap.getValue()).isSameInstanceAs(parentReportCap.getValue());
}
Also used : ClientStreamTracer(io.grpc.ClientStreamTracer) Metadata(io.grpc.Metadata) OrcaLoadReport(com.github.xds.data.orca.v3.OrcaLoadReport) Test(org.junit.Test)

Example 39 with ClientStreamTracer

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

the class CallCredentialsApplyingTest method fail_inline.

@Test
public void fail_inline() {
    final Status error = Status.FAILED_PRECONDITION.withDescription("channel not secure for creds");
    when(mockTransport.getAttributes()).thenReturn(Attributes.EMPTY);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            CallCredentials.MetadataApplier applier = (CallCredentials.MetadataApplier) invocation.getArguments()[2];
            applier.fail(error);
            return null;
        }
    }).when(mockCreds).applyRequestMetadata(any(RequestInfo.class), same(mockExecutor), any(CallCredentials.MetadataApplier.class));
    FailingClientStream stream = (FailingClientStream) transport.newStream(method, origHeaders, callOptions, tracers);
    verify(mockTransport, never()).newStream(any(MethodDescriptor.class), any(Metadata.class), any(CallOptions.class), ArgumentMatchers.<ClientStreamTracer[]>any());
    assertSame(error, stream.getError());
    transport.shutdownNow(Status.UNAVAILABLE);
    assertTrue(transport.newStream(method, origHeaders, callOptions, tracers) instanceof FailingClientStream);
    verify(mockTransport).shutdownNow(Status.UNAVAILABLE);
}
Also used : Status(io.grpc.Status) ClientStreamTracer(io.grpc.ClientStreamTracer) Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) RequestInfo(io.grpc.CallCredentials.RequestInfo) MethodDescriptor(io.grpc.MethodDescriptor) CallCredentials(io.grpc.CallCredentials) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 40 with ClientStreamTracer

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

the class CallCredentialsApplyingTest method applyMetadata_delayed.

@Test
public void applyMetadata_delayed() {
    when(mockTransport.getAttributes()).thenReturn(Attributes.EMPTY);
    // Will call applyRequestMetadata(), which is no-op.
    DelayedStream stream = (DelayedStream) transport.newStream(method, origHeaders, callOptions, tracers);
    ArgumentCaptor<CallCredentials.MetadataApplier> applierCaptor = ArgumentCaptor.forClass(null);
    verify(mockCreds).applyRequestMetadata(any(RequestInfo.class), same(mockExecutor), applierCaptor.capture());
    verify(mockTransport, never()).newStream(any(MethodDescriptor.class), any(Metadata.class), any(CallOptions.class), ArgumentMatchers.<ClientStreamTracer[]>any());
    transport.shutdown(Status.UNAVAILABLE);
    verify(mockTransport, never()).shutdown(Status.UNAVAILABLE);
    assertTrue(transport.newStream(method, origHeaders, callOptions, tracers) instanceof FailingClientStream);
    Metadata headers = new Metadata();
    headers.put(CREDS_KEY, CREDS_VALUE);
    applierCaptor.getValue().apply(headers);
    verify(mockTransport).newStream(method, origHeaders, callOptions, tracers);
    assertSame(mockStream, stream.getRealStream());
    assertEquals(CREDS_VALUE, origHeaders.get(CREDS_KEY));
    assertEquals(ORIG_HEADER_VALUE, origHeaders.get(ORIG_HEADER_KEY));
    verify(mockTransport).shutdown(Status.UNAVAILABLE);
}
Also used : ClientStreamTracer(io.grpc.ClientStreamTracer) Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) RequestInfo(io.grpc.CallCredentials.RequestInfo) MethodDescriptor(io.grpc.MethodDescriptor) Test(org.junit.Test)

Aggregations

ClientStreamTracer (io.grpc.ClientStreamTracer)57 Metadata (io.grpc.Metadata)54 Test (org.junit.Test)44 CallOptions (io.grpc.CallOptions)28 Subchannel (io.grpc.LoadBalancer.Subchannel)22 MockClientTransportInfo (io.grpc.internal.TestUtils.MockClientTransportInfo)21 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)19 ForwardingSubchannel (io.grpc.util.ForwardingSubchannel)18 MethodDescriptor (io.grpc.MethodDescriptor)16 Status (io.grpc.Status)14 ChannelLogger (io.grpc.ChannelLogger)11 ClientTransportOptions (io.grpc.internal.ClientTransportFactory.ClientTransportOptions)10 ProxiedSocketAddress (io.grpc.ProxiedSocketAddress)9 StreamInfo (io.grpc.ClientStreamTracer.StreamInfo)8 CallAttemptsTracerFactory (io.grpc.census.CensusTracingModule.CallAttemptsTracerFactory)8 SocketAddress (java.net.SocketAddress)8 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)7 Helper (io.grpc.LoadBalancer.Helper)7 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)7 ManagedChannel (io.grpc.ManagedChannel)7