io.grpc.ClientStreamTracer#inboundTrailers ( )源码实例Demo

下面列出了io.grpc.ClientStreamTracer#inboundTrailers ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: grpc-java   文件: OrcaPerRequestUtilTest.java
/**
 * 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()));
  verifyZeroInteractions(childFactory);
  verifyZeroInteractions(orcaListener2);
}
 
源代码2 项目: grpc-java   文件: OrcaPerRequestUtilTest.java
/**
 * Tests a single load balance policy's listener receive per-request ORCA reports upon call
 * trailer arrives.
 */
@Test
public void singlePolicyTypicalWorkflow() {
  // Use a mocked noop stream tracer factory as the original stream tracer factory.
  ClientStreamTracer.Factory fakeDelegateFactory = mock(ClientStreamTracer.Factory.class);
  ClientStreamTracer fakeTracer = mock(ClientStreamTracer.class);
  doNothing().when(fakeTracer).inboundTrailers(any(Metadata.class));
  when(fakeDelegateFactory.newClientStreamTracer(
          any(ClientStreamTracer.StreamInfo.class), any(Metadata.class)))
      .thenReturn(fakeTracer);

  // The OrcaReportingTracerFactory will augment the StreamInfo passed to its
  // newClientStreamTracer method. The augmented StreamInfo's CallOptions will contain
  // a OrcaReportBroker, in which has the registered listener.
  ClientStreamTracer.Factory factory =
      OrcaPerRequestUtil.getInstance()
          .newOrcaClientStreamTracerFactory(fakeDelegateFactory, orcaListener1);
  ClientStreamTracer tracer = factory.newClientStreamTracer(STREAM_INFO, new Metadata());
  ArgumentCaptor<ClientStreamTracer.StreamInfo> streamInfoCaptor = ArgumentCaptor.forClass(null);
  verify(fakeDelegateFactory)
      .newClientStreamTracer(streamInfoCaptor.capture(), any(Metadata.class));
  ClientStreamTracer.StreamInfo capturedInfo = streamInfoCaptor.getValue();
  assertThat(capturedInfo).isNotEqualTo(STREAM_INFO);

  // When the trailer does not contain ORCA report, listener callback will not be invoked.
  Metadata trailer = new Metadata();
  tracer.inboundTrailers(trailer);
  verifyNoMoreInteractions(orcaListener1);

  // When the trailer contains an ORCA report, listener callback will be invoked.
  trailer.put(
      OrcaReportingTracerFactory.ORCA_ENDPOINT_LOAD_METRICS_KEY,
      OrcaLoadReport.getDefaultInstance());
  tracer.inboundTrailers(trailer);
  ArgumentCaptor<OrcaLoadReport> reportCaptor = ArgumentCaptor.forClass(null);
  verify(orcaListener1).onLoadReport(reportCaptor.capture());
  assertThat(reportCaptor.getValue()).isEqualTo(OrcaLoadReport.getDefaultInstance());
}
 
源代码3 项目: grpc-java   文件: OrcaPerRequestUtilTest.java
/**
 * 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());
}