Search in sources :

Example 76 with Subchannel

use of io.grpc.LoadBalancer.Subchannel in project grpc-java by grpc.

the class ManagedChannelImplTest method oobChannelWithOobChannelCredsHasChannelCallCredentials.

@Test
public void oobChannelWithOobChannelCredsHasChannelCallCredentials() {
    Metadata.Key<String> metadataKey = Metadata.Key.of("token", Metadata.ASCII_STRING_MARSHALLER);
    String channelCredValue = "channel-provided call cred";
    when(mockTransportFactory.swapChannelCredentials(any(CompositeChannelCredentials.class))).thenAnswer(new Answer<SwapChannelCredentialsResult>() {

        @Override
        public SwapChannelCredentialsResult answer(InvocationOnMock invocation) {
            CompositeChannelCredentials c = invocation.getArgument(0, CompositeChannelCredentials.class);
            return new SwapChannelCredentialsResult(mockTransportFactory, c.getCallCredentials());
        }
    });
    channelBuilder = new ManagedChannelImplBuilder(TARGET, InsecureChannelCredentials.create(), new FakeCallCredentials(metadataKey, channelCredValue), new UnsupportedClientTransportFactoryBuilder(), new FixedPortProvider(DEFAULT_PORT));
    channelBuilder.disableRetry();
    configureBuilder(channelBuilder);
    createChannel();
    // Verify that the normal channel has call creds, to validate configuration
    Subchannel subchannel = createSubchannelSafely(helper, addressGroup, Attributes.EMPTY, subchannelStateListener);
    requestConnectionSafely(helper, subchannel);
    MockClientTransportInfo transportInfo = transports.poll();
    transportInfo.listener.transportReady();
    when(mockPicker.pickSubchannel(any(PickSubchannelArgs.class))).thenReturn(PickResult.withSubchannel(subchannel));
    updateBalancingStateSafely(helper, READY, mockPicker);
    String callCredValue = "per-RPC call cred";
    CallOptions callOptions = CallOptions.DEFAULT.withCallCredentials(new FakeCallCredentials(metadataKey, callCredValue));
    Metadata headers = new Metadata();
    ClientCall<String, Integer> call = channel.newCall(method, callOptions);
    call.start(mockCallListener, headers);
    verify(transportInfo.transport).newStream(same(method), same(headers), same(callOptions), ArgumentMatchers.<ClientStreamTracer[]>any());
    assertThat(headers.getAll(metadataKey)).containsExactly(channelCredValue, callCredValue).inOrder();
    // Verify that resolving oob channel with oob channel creds provides call creds
    String oobChannelCredValue = "oob-channel-provided call cred";
    ChannelCredentials oobChannelCreds = CompositeChannelCredentials.create(InsecureChannelCredentials.create(), new FakeCallCredentials(metadataKey, oobChannelCredValue));
    ManagedChannel oob = helper.createResolvingOobChannelBuilder("fake://oobauthority/", oobChannelCreds).nameResolverFactory(new FakeNameResolverFactory.Builder(URI.create("fake://oobauthority/")).build()).defaultLoadBalancingPolicy(MOCK_POLICY_NAME).idleTimeout(ManagedChannelImplBuilder.IDLE_MODE_MAX_TIMEOUT_DAYS, TimeUnit.DAYS).disableRetry().build();
    oob.getState(true);
    ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(Helper.class);
    verify(mockLoadBalancerProvider, times(2)).newLoadBalancer(helperCaptor.capture());
    Helper oobHelper = helperCaptor.getValue();
    subchannel = createSubchannelSafely(oobHelper, addressGroup, Attributes.EMPTY, subchannelStateListener);
    requestConnectionSafely(oobHelper, subchannel);
    transportInfo = transports.poll();
    transportInfo.listener.transportReady();
    SubchannelPicker mockPicker2 = mock(SubchannelPicker.class);
    when(mockPicker2.pickSubchannel(any(PickSubchannelArgs.class))).thenReturn(PickResult.withSubchannel(subchannel));
    updateBalancingStateSafely(oobHelper, READY, mockPicker2);
    headers = new Metadata();
    call = oob.newCall(method, callOptions);
    call.start(mockCallListener2, headers);
    // CallOptions may contain StreamTracerFactory for census that is added by default.
    verify(transportInfo.transport).newStream(same(method), same(headers), any(CallOptions.class), ArgumentMatchers.<ClientStreamTracer[]>any());
    assertThat(headers.getAll(metadataKey)).containsExactly(oobChannelCredValue, callCredValue).inOrder();
    oob.shutdownNow();
}
Also used : ClientStreamTracer(io.grpc.ClientStreamTracer) UnsupportedClientTransportFactoryBuilder(io.grpc.internal.ManagedChannelImplBuilder.UnsupportedClientTransportFactoryBuilder) ClientTransportFactoryBuilder(io.grpc.internal.ManagedChannelImplBuilder.ClientTransportFactoryBuilder) Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) Helper(io.grpc.LoadBalancer.Helper) InsecureChannelCredentials(io.grpc.InsecureChannelCredentials) ChannelCredentials(io.grpc.ChannelCredentials) CompositeChannelCredentials(io.grpc.CompositeChannelCredentials) ManagedChannel(io.grpc.ManagedChannel) PickSubchannelArgs(io.grpc.LoadBalancer.PickSubchannelArgs) UnsupportedClientTransportFactoryBuilder(io.grpc.internal.ManagedChannelImplBuilder.UnsupportedClientTransportFactoryBuilder) CompositeChannelCredentials(io.grpc.CompositeChannelCredentials) SwapChannelCredentialsResult(io.grpc.internal.ClientTransportFactory.SwapChannelCredentialsResult) MockClientTransportInfo(io.grpc.internal.TestUtils.MockClientTransportInfo) SubchannelPicker(io.grpc.LoadBalancer.SubchannelPicker) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ForwardingSubchannel(io.grpc.util.ForwardingSubchannel) Subchannel(io.grpc.LoadBalancer.Subchannel) FixedPortProvider(io.grpc.internal.ManagedChannelImplBuilder.FixedPortProvider) Test(org.junit.Test)

Example 77 with Subchannel

use of io.grpc.LoadBalancer.Subchannel in project grpc-java by grpc.

the class ManagedChannelImplTest method subchannelConnectionBroken_ignoreRefreshNameResolutionCheck_noRefresh.

@Test
public void subchannelConnectionBroken_ignoreRefreshNameResolutionCheck_noRefresh() {
    FakeNameResolverFactory nameResolverFactory = new FakeNameResolverFactory.Builder(expectedUri).setServers(Collections.singletonList(new EquivalentAddressGroup(socketAddress))).build();
    channelBuilder.nameResolverFactory(nameResolverFactory);
    createChannel();
    FakeNameResolverFactory.FakeNameResolver resolver = Iterables.getOnlyElement(nameResolverFactory.resolvers);
    assertThat(resolver.refreshCalled).isEqualTo(0);
    ArgumentCaptor<Helper> helperCaptor = ArgumentCaptor.forClass(Helper.class);
    verify(mockLoadBalancerProvider).newLoadBalancer(helperCaptor.capture());
    helper = helperCaptor.getValue();
    helper.ignoreRefreshNameResolutionCheck();
    Subchannel subchannel = createSubchannelSafely(helper, addressGroup, Attributes.EMPTY, subchannelStateListener);
    InternalSubchannel internalSubchannel = (InternalSubchannel) subchannel.getInternalSubchannel();
    internalSubchannel.obtainActiveTransport();
    MockClientTransportInfo transportInfo = transports.poll();
    // Break subchannel connection
    transportInfo.listener.transportShutdown(Status.UNAVAILABLE.withDescription("unreachable"));
    assertThat(logs).isEmpty();
    assertThat(resolver.refreshCalled).isEqualTo(0);
}
Also used : Helper(io.grpc.LoadBalancer.Helper) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) ForwardingSubchannel(io.grpc.util.ForwardingSubchannel) Subchannel(io.grpc.LoadBalancer.Subchannel) UnsupportedClientTransportFactoryBuilder(io.grpc.internal.ManagedChannelImplBuilder.UnsupportedClientTransportFactoryBuilder) ClientTransportFactoryBuilder(io.grpc.internal.ManagedChannelImplBuilder.ClientTransportFactoryBuilder) MockClientTransportInfo(io.grpc.internal.TestUtils.MockClientTransportInfo) Test(org.junit.Test)

Example 78 with Subchannel

use of io.grpc.LoadBalancer.Subchannel in project grpc-java by grpc.

the class ManagedChannelImplTest method noMoreCallbackAfterLoadBalancerShutdown.

@Test
public void noMoreCallbackAfterLoadBalancerShutdown() {
    FakeNameResolverFactory nameResolverFactory = new FakeNameResolverFactory.Builder(expectedUri).setServers(Collections.singletonList(new EquivalentAddressGroup(socketAddress))).build();
    channelBuilder.nameResolverFactory(nameResolverFactory);
    Status resolutionError = Status.UNAVAILABLE.withDescription("Resolution failed");
    createChannel();
    FakeNameResolverFactory.FakeNameResolver resolver = nameResolverFactory.resolvers.get(0);
    verify(mockLoadBalancerProvider).newLoadBalancer(any(Helper.class));
    verify(mockLoadBalancer).handleResolvedAddresses(resolvedAddressCaptor.capture());
    assertThat(resolvedAddressCaptor.getValue().getAddresses()).containsExactly(addressGroup);
    SubchannelStateListener stateListener1 = mock(SubchannelStateListener.class);
    SubchannelStateListener stateListener2 = mock(SubchannelStateListener.class);
    Subchannel subchannel1 = createSubchannelSafely(helper, addressGroup, Attributes.EMPTY, stateListener1);
    Subchannel subchannel2 = createSubchannelSafely(helper, addressGroup, Attributes.EMPTY, stateListener2);
    requestConnectionSafely(helper, subchannel1);
    requestConnectionSafely(helper, subchannel2);
    verify(mockTransportFactory, times(2)).newClientTransport(any(SocketAddress.class), any(ClientTransportOptions.class), any(ChannelLogger.class));
    MockClientTransportInfo transportInfo1 = transports.poll();
    MockClientTransportInfo transportInfo2 = transports.poll();
    // LoadBalancer receives all sorts of callbacks
    transportInfo1.listener.transportReady();
    verify(stateListener1, times(2)).onSubchannelState(stateInfoCaptor.capture());
    assertSame(CONNECTING, stateInfoCaptor.getAllValues().get(0).getState());
    assertSame(READY, stateInfoCaptor.getAllValues().get(1).getState());
    verify(stateListener2).onSubchannelState(stateInfoCaptor.capture());
    assertSame(CONNECTING, stateInfoCaptor.getValue().getState());
    resolver.listener.onError(resolutionError);
    verify(mockLoadBalancer).handleNameResolutionError(resolutionError);
    verifyNoMoreInteractions(mockLoadBalancer);
    channel.shutdown();
    verify(mockLoadBalancer).shutdown();
    verifyNoMoreInteractions(stateListener1, stateListener2);
    // LoadBalancer will normally shutdown all subchannels
    shutdownSafely(helper, subchannel1);
    shutdownSafely(helper, subchannel2);
    // Since subchannels are shutdown, SubchannelStateListeners will only get SHUTDOWN regardless of
    // the transport states.
    transportInfo1.listener.transportShutdown(Status.UNAVAILABLE);
    transportInfo2.listener.transportReady();
    verify(stateListener1).onSubchannelState(ConnectivityStateInfo.forNonError(SHUTDOWN));
    verify(stateListener2).onSubchannelState(ConnectivityStateInfo.forNonError(SHUTDOWN));
    verifyNoMoreInteractions(stateListener1, stateListener2);
    // No more callback should be delivered to LoadBalancer after it's shut down
    resolver.listener.onError(resolutionError);
    resolver.resolved();
    verifyNoMoreInteractions(mockLoadBalancer);
}
Also used : Status(io.grpc.Status) Helper(io.grpc.LoadBalancer.Helper) SubchannelStateListener(io.grpc.LoadBalancer.SubchannelStateListener) ClientTransportOptions(io.grpc.internal.ClientTransportFactory.ClientTransportOptions) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) ForwardingSubchannel(io.grpc.util.ForwardingSubchannel) Subchannel(io.grpc.LoadBalancer.Subchannel) UnsupportedClientTransportFactoryBuilder(io.grpc.internal.ManagedChannelImplBuilder.UnsupportedClientTransportFactoryBuilder) ClientTransportFactoryBuilder(io.grpc.internal.ManagedChannelImplBuilder.ClientTransportFactoryBuilder) MockClientTransportInfo(io.grpc.internal.TestUtils.MockClientTransportInfo) ChannelLogger(io.grpc.ChannelLogger) ProxiedSocketAddress(io.grpc.ProxiedSocketAddress) SocketAddress(java.net.SocketAddress) Test(org.junit.Test)

Example 79 with Subchannel

use of io.grpc.LoadBalancer.Subchannel in project grpc-java by grpc.

the class ManagedChannelImplTest method subchannelsNoConnectionShutdown.

@Test
public void subchannelsNoConnectionShutdown() {
    createChannel();
    Subchannel sub1 = createSubchannelSafely(helper, addressGroup, Attributes.EMPTY, subchannelStateListener);
    Subchannel sub2 = createSubchannelSafely(helper, addressGroup, Attributes.EMPTY, subchannelStateListener);
    channel.shutdown();
    verify(mockLoadBalancer).shutdown();
    shutdownSafely(helper, sub1);
    assertFalse(channel.isTerminated());
    shutdownSafely(helper, sub2);
    assertTrue(channel.isTerminated());
    verify(mockTransportFactory, never()).newClientTransport(any(SocketAddress.class), any(ClientTransportOptions.class), any(ChannelLogger.class));
}
Also used : ClientTransportOptions(io.grpc.internal.ClientTransportFactory.ClientTransportOptions) ForwardingSubchannel(io.grpc.util.ForwardingSubchannel) Subchannel(io.grpc.LoadBalancer.Subchannel) ChannelLogger(io.grpc.ChannelLogger) ProxiedSocketAddress(io.grpc.ProxiedSocketAddress) SocketAddress(java.net.SocketAddress) Test(org.junit.Test)

Example 80 with Subchannel

use of io.grpc.LoadBalancer.Subchannel in project grpc-java by grpc.

the class ManagedChannelImplTest method createSubchannel_channelBuilderOverrideAuthority.

@Test
public void createSubchannel_channelBuilderOverrideAuthority() {
    channelBuilder.overrideAuthority("channel-builder.override.authority");
    EquivalentAddressGroup addressGroup = new EquivalentAddressGroup(socketAddress, Attributes.newBuilder().set(ATTR_AUTHORITY_OVERRIDE, "resolver.override.authority").build());
    channelBuilder.nameResolverFactory(new FakeNameResolverFactory.Builder(expectedUri).setServers(Collections.singletonList(addressGroup)).build());
    createChannel();
    final Subchannel subchannel = createSubchannelSafely(helper, addressGroup, Attributes.EMPTY, subchannelStateListener);
    requestConnectionSafely(helper, subchannel);
    ArgumentCaptor<ClientTransportOptions> transportOptionCaptor = ArgumentCaptor.forClass(null);
    verify(mockTransportFactory).newClientTransport(any(SocketAddress.class), transportOptionCaptor.capture(), any(ChannelLogger.class));
    assertThat(transportOptionCaptor.getValue().getAuthority()).isEqualTo("channel-builder.override.authority");
    final List<EquivalentAddressGroup> subchannelEags = new ArrayList<>();
    helper.getSynchronizationContext().execute(new Runnable() {

        @Override
        public void run() {
            subchannelEags.addAll(subchannel.getAllAddresses());
        }
    });
    assertThat(subchannelEags).isEqualTo(ImmutableList.of(addressGroup));
}
Also used : ClientTransportOptions(io.grpc.internal.ClientTransportFactory.ClientTransportOptions) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) ForwardingSubchannel(io.grpc.util.ForwardingSubchannel) Subchannel(io.grpc.LoadBalancer.Subchannel) ArrayList(java.util.ArrayList) ChannelLogger(io.grpc.ChannelLogger) ProxiedSocketAddress(io.grpc.ProxiedSocketAddress) SocketAddress(java.net.SocketAddress) Test(org.junit.Test)

Aggregations

Subchannel (io.grpc.LoadBalancer.Subchannel)125 Test (org.junit.Test)108 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)54 InOrder (org.mockito.InOrder)43 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)42 Metadata (io.grpc.Metadata)40 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)35 CreateSubchannelArgs (io.grpc.LoadBalancer.CreateSubchannelArgs)33 ForwardingSubchannel (io.grpc.util.ForwardingSubchannel)32 MockClientTransportInfo (io.grpc.internal.TestUtils.MockClientTransportInfo)30 Attributes (io.grpc.Attributes)26 ConnectivityStateInfo (io.grpc.ConnectivityStateInfo)25 ClientStreamTracer (io.grpc.ClientStreamTracer)24 Status (io.grpc.Status)23 PickResult (io.grpc.LoadBalancer.PickResult)19 CallOptions (io.grpc.CallOptions)18 ConnectivityState (io.grpc.ConnectivityState)18 Helper (io.grpc.LoadBalancer.Helper)18 SocketAddress (java.net.SocketAddress)18 ChannelLogger (io.grpc.ChannelLogger)14