io.grpc.ConnectivityStateInfo#forTransientFailure ( )源码实例Demo

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

@Test
public void serviceConfigDisablesHealthCheckWhenRpcInactive() {
  Attributes resolutionAttrs = attrsWithHealthCheckService("TeeService");
  hcLbEventDelivery.handleResolvedAddressGroups(resolvedAddressList, resolutionAttrs);

  verify(origLb).handleResolvedAddressGroups(same(resolvedAddressList), same(resolutionAttrs));
  verifyNoMoreInteractions(origLb);

  Subchannel subchannel = createSubchannel(0, Attributes.EMPTY);
  assertThat(subchannel).isSameAs(subchannels[0]);
  InOrder inOrder = inOrder(origLb);

  // Underlying subchannel is not READY initially
  ConnectivityStateInfo underlyingErrorState =
      ConnectivityStateInfo.forTransientFailure(
          Status.UNAVAILABLE.withDescription("connection refused"));
  hcLbEventDelivery.handleSubchannelState(subchannel, underlyingErrorState);
  inOrder.verify(origLb).handleSubchannelState(same(subchannel), same(underlyingErrorState));
  inOrder.verifyNoMoreInteractions();

  // NameResolver gives an update without service config, thus health check will be disabled
  hcLbEventDelivery.handleResolvedAddressGroups(resolvedAddressList, Attributes.EMPTY);

  inOrder.verify(origLb).handleResolvedAddressGroups(
      same(resolvedAddressList), same(Attributes.EMPTY));

  // Underlying subchannel is now ready
  hcLbEventDelivery.handleSubchannelState(subchannel, ConnectivityStateInfo.forNonError(READY));

  // Since health check is disabled, READY state is propagated directly.
  inOrder.verify(origLb).handleSubchannelState(
      same(subchannel), eq(ConnectivityStateInfo.forNonError(READY)));

  // and there is no health check activity.
  assertThat(healthImpls[0].calls).isEmpty();

  verifyNoMoreInteractions(origLb);
}
 
@Test
public void serviceConfigChangesServiceNameWhenRpcInactive() {
  Attributes resolutionAttrs = attrsWithHealthCheckService("TeeService");
  hcLbEventDelivery.handleResolvedAddressGroups(resolvedAddressList, resolutionAttrs);

  verify(origLb).handleResolvedAddressGroups(same(resolvedAddressList), same(resolutionAttrs));
  verifyNoMoreInteractions(origLb);

  Subchannel subchannel = createSubchannel(0, Attributes.EMPTY);
  assertThat(subchannel).isSameAs(subchannels[0]);
  InOrder inOrder = inOrder(origLb);
  HealthImpl healthImpl = healthImpls[0];

  // Underlying subchannel is not READY initially
  ConnectivityStateInfo underlyingErrorState =
      ConnectivityStateInfo.forTransientFailure(
          Status.UNAVAILABLE.withDescription("connection refused"));
  hcLbEventDelivery.handleSubchannelState(subchannel, underlyingErrorState);
  inOrder.verify(origLb).handleSubchannelState(same(subchannel), same(underlyingErrorState));
  inOrder.verifyNoMoreInteractions();

  // Service config returns with the same health check name.
  hcLbEventDelivery.handleResolvedAddressGroups(resolvedAddressList, resolutionAttrs);
  // It's delivered to origLb, but nothing else happens
  inOrder.verify(origLb).handleResolvedAddressGroups(
      same(resolvedAddressList), same(resolutionAttrs));
  assertThat(healthImpl.calls).isEmpty();
  verifyNoMoreInteractions(origLb);

  // Service config returns a different health check name.
  resolutionAttrs = attrsWithHealthCheckService("FooService");
  hcLbEventDelivery.handleResolvedAddressGroups(resolvedAddressList, resolutionAttrs);

  inOrder.verify(origLb).handleResolvedAddressGroups(
      same(resolvedAddressList), same(resolutionAttrs));

  // Underlying subchannel is now ready
  hcLbEventDelivery.handleSubchannelState(subchannel, ConnectivityStateInfo.forNonError(READY));

  // Concluded CONNECTING state
  inOrder.verify(origLb).handleSubchannelState(
      same(subchannel), eq(ConnectivityStateInfo.forNonError(CONNECTING)));

  // Health check RPC is started
  assertThat(healthImpl.calls).hasSize(1);
  // with the new service name
  assertThat(healthImpl.calls.poll().request).isEqualTo(makeRequest("FooService"));

  verifyNoMoreInteractions(origLb);
}
 
@Test
public void serviceConfigDisablesHealthCheckWhenRpcInactive() {
  Attributes resolutionAttrs = attrsWithHealthCheckService("TeeService");
  ResolvedAddresses result1 = ResolvedAddresses.newBuilder()
      .setAddresses(resolvedAddressList)
      .setAttributes(resolutionAttrs)
      .build();
  hcLbEventDelivery.handleResolvedAddresses(result1);

  verify(origLb).handleResolvedAddresses(result1);
  verifyNoMoreInteractions(origLb);

  Subchannel subchannel = createSubchannel(0, Attributes.EMPTY);
  assertThat(unwrap(subchannel)).isSameInstanceAs(subchannels[0]);
  InOrder inOrder = inOrder(origLb, mockStateListeners[0]);

  // Underlying subchannel is not READY initially
  ConnectivityStateInfo underlyingErrorState =
      ConnectivityStateInfo.forTransientFailure(
          Status.UNAVAILABLE.withDescription("connection refused"));
  deliverSubchannelState(0, underlyingErrorState);
  inOrder.verify(mockStateListeners[0]).onSubchannelState(same(underlyingErrorState));
  inOrder.verifyNoMoreInteractions();

  // NameResolver gives an update without service config, thus health check will be disabled
  ResolvedAddresses result2 = ResolvedAddresses.newBuilder()
      .setAddresses(resolvedAddressList)
      .setAttributes(Attributes.EMPTY)
      .build();
  hcLbEventDelivery.handleResolvedAddresses(result2);

  inOrder.verify(origLb).handleResolvedAddresses(result2);

  // Underlying subchannel is now ready
  deliverSubchannelState(0, ConnectivityStateInfo.forNonError(READY));

  // Since health check is disabled, READY state is propagated directly.
  inOrder.verify(mockStateListeners[0]).onSubchannelState(
      eq(ConnectivityStateInfo.forNonError(READY)));

  // and there is no health check activity.
  assertThat(healthImpls[0].calls).isEmpty();

  verifyNoMoreInteractions(origLb, mockStateListeners[0]);
}
 
@Test
public void serviceConfigChangesServiceNameWhenRpcInactive() {
  Attributes resolutionAttrs = attrsWithHealthCheckService("TeeService");
  ResolvedAddresses result1 = ResolvedAddresses.newBuilder()
      .setAddresses(resolvedAddressList)
      .setAttributes(resolutionAttrs)
      .build();
  hcLbEventDelivery.handleResolvedAddresses(result1);

  verify(origLb).handleResolvedAddresses(result1);
  verifyNoMoreInteractions(origLb);

  Subchannel subchannel = createSubchannel(0, Attributes.EMPTY);
  SubchannelStateListener mockListener = mockStateListeners[0];
  assertThat(unwrap(subchannel)).isSameInstanceAs(subchannels[0]);
  InOrder inOrder = inOrder(origLb, mockListener);
  HealthImpl healthImpl = healthImpls[0];

  // Underlying subchannel is not READY initially
  ConnectivityStateInfo underlyingErrorState =
      ConnectivityStateInfo.forTransientFailure(
          Status.UNAVAILABLE.withDescription("connection refused"));
  deliverSubchannelState(0, underlyingErrorState);
  inOrder.verify(mockListener).onSubchannelState(same(underlyingErrorState));
  inOrder.verifyNoMoreInteractions();

  // Service config returns with the same health check name.
  hcLbEventDelivery.handleResolvedAddresses(result1);
  // It's delivered to origLb, but nothing else happens
  inOrder.verify(origLb).handleResolvedAddresses(result1);
  assertThat(healthImpl.calls).isEmpty();
  verifyNoMoreInteractions(origLb);

  // Service config returns a different health check name.
  resolutionAttrs = attrsWithHealthCheckService("FooService");
  ResolvedAddresses result2 = ResolvedAddresses.newBuilder()
      .setAddresses(resolvedAddressList)
      .setAttributes(resolutionAttrs)
      .build();
  hcLbEventDelivery.handleResolvedAddresses(result2);

  inOrder.verify(origLb).handleResolvedAddresses(result2);

  // Underlying subchannel is now ready
  deliverSubchannelState(0, ConnectivityStateInfo.forNonError(READY));

  // Concluded CONNECTING state
  inOrder.verify(mockListener).onSubchannelState(
      eq(ConnectivityStateInfo.forNonError(CONNECTING)));

  // Health check RPC is started
  assertThat(healthImpl.calls).hasSize(1);
  // with the new service name
  assertThat(healthImpl.calls.poll().request).isEqualTo(makeRequest("FooService"));

  verifyNoMoreInteractions(origLb, mockListener);
}