Search in sources :

Example 1 with ClientTransportOptions

use of io.grpc.internal.ClientTransportFactory.ClientTransportOptions in project grpc-java by grpc.

the class CronetChannelBuilderTest method alwaysUsePut_defaultsToFalse.

@Test
public void alwaysUsePut_defaultsToFalse() throws Exception {
    CronetChannelBuilder builder = CronetChannelBuilder.forAddress("address", 1234, mockEngine);
    CronetTransportFactory transportFactory = (CronetTransportFactory) builder.buildTransportFactory();
    CronetClientTransport transport = (CronetClientTransport) transportFactory.newClientTransport(new InetSocketAddress("localhost", 443), new ClientTransportOptions(), channelLogger);
    CronetClientStream stream = transport.newStream(method, new Metadata(), CallOptions.DEFAULT, tracers);
    assertFalse(stream.idempotent);
}
Also used : ClientTransportOptions(io.grpc.internal.ClientTransportFactory.ClientTransportOptions) InetSocketAddress(java.net.InetSocketAddress) Metadata(io.grpc.Metadata) CronetTransportFactory(io.grpc.cronet.CronetChannelBuilder.CronetTransportFactory) Test(org.junit.Test)

Example 2 with ClientTransportOptions

use of io.grpc.internal.ClientTransportFactory.ClientTransportOptions in project grpc-java by grpc.

the class CronetChannelBuilderTest method alwaysUsePutTrue_cronetStreamIsIdempotent.

@Test
public void alwaysUsePutTrue_cronetStreamIsIdempotent() throws Exception {
    CronetChannelBuilder builder = CronetChannelBuilder.forAddress("address", 1234, mockEngine).alwaysUsePut(true);
    CronetTransportFactory transportFactory = (CronetTransportFactory) builder.buildTransportFactory();
    CronetClientTransport transport = (CronetClientTransport) transportFactory.newClientTransport(new InetSocketAddress("localhost", 443), new ClientTransportOptions(), channelLogger);
    CronetClientStream stream = transport.newStream(method, new Metadata(), CallOptions.DEFAULT, tracers);
    assertTrue(stream.idempotent);
}
Also used : ClientTransportOptions(io.grpc.internal.ClientTransportFactory.ClientTransportOptions) InetSocketAddress(java.net.InetSocketAddress) Metadata(io.grpc.Metadata) CronetTransportFactory(io.grpc.cronet.CronetChannelBuilder.CronetTransportFactory) Test(org.junit.Test)

Example 3 with ClientTransportOptions

use of io.grpc.internal.ClientTransportFactory.ClientTransportOptions in project grpc-java by grpc.

the class ManagedChannelImplTest method oobchannels.

@Test
public void oobchannels() {
    createChannel();
    ManagedChannel oob1 = helper.createOobChannel(Collections.singletonList(addressGroup), "oob1authority");
    ManagedChannel oob2 = helper.createOobChannel(Collections.singletonList(addressGroup), "oob2authority");
    verify(balancerRpcExecutorPool, times(2)).getObject();
    assertEquals("oob1authority", oob1.authority());
    assertEquals("oob2authority", oob2.authority());
    // OOB channels create connections lazily.  A new call will initiate the connection.
    Metadata headers = new Metadata();
    ClientCall<String, Integer> call = oob1.newCall(method, CallOptions.DEFAULT);
    call.start(mockCallListener, headers);
    verify(mockTransportFactory).newClientTransport(eq(socketAddress), eq(new ClientTransportOptions().setAuthority("oob1authority").setUserAgent(USER_AGENT)), isA(ChannelLogger.class));
    MockClientTransportInfo transportInfo = transports.poll();
    assertNotNull(transportInfo);
    assertEquals(0, balancerRpcExecutor.numPendingTasks());
    transportInfo.listener.transportReady();
    assertEquals(1, balancerRpcExecutor.runDueTasks());
    verify(transportInfo.transport).newStream(same(method), same(headers), same(CallOptions.DEFAULT), ArgumentMatchers.<ClientStreamTracer[]>any());
    // The transport goes away
    transportInfo.listener.transportShutdown(Status.UNAVAILABLE);
    transportInfo.listener.transportTerminated();
    // A new call will trigger a new transport
    ClientCall<String, Integer> call2 = oob1.newCall(method, CallOptions.DEFAULT);
    call2.start(mockCallListener2, headers);
    ClientCall<String, Integer> call3 = oob1.newCall(method, CallOptions.DEFAULT.withWaitForReady());
    call3.start(mockCallListener3, headers);
    verify(mockTransportFactory, times(2)).newClientTransport(eq(socketAddress), eq(new ClientTransportOptions().setAuthority("oob1authority").setUserAgent(USER_AGENT)), isA(ChannelLogger.class));
    transportInfo = transports.poll();
    assertNotNull(transportInfo);
    // This transport fails
    Status transportError = Status.UNAVAILABLE.withDescription("Connection refused");
    assertEquals(0, balancerRpcExecutor.numPendingTasks());
    transportInfo.listener.transportShutdown(transportError);
    assertTrue(balancerRpcExecutor.runDueTasks() > 0);
    // Fail-fast RPC will fail, while wait-for-ready RPC will still be pending
    verify(mockCallListener2).onClose(same(transportError), any(Metadata.class));
    verify(mockCallListener3, never()).onClose(any(Status.class), any(Metadata.class));
    // Shutdown
    assertFalse(oob1.isShutdown());
    assertFalse(oob2.isShutdown());
    oob1.shutdown();
    oob2.shutdownNow();
    assertTrue(oob1.isShutdown());
    assertTrue(oob2.isShutdown());
    assertTrue(oob2.isTerminated());
    verify(balancerRpcExecutorPool).returnObject(balancerRpcExecutor.getScheduledExecutorService());
    // New RPCs will be rejected.
    assertEquals(0, balancerRpcExecutor.numPendingTasks());
    ClientCall<String, Integer> call4 = oob1.newCall(method, CallOptions.DEFAULT);
    ClientCall<String, Integer> call5 = oob2.newCall(method, CallOptions.DEFAULT);
    call4.start(mockCallListener4, headers);
    call5.start(mockCallListener5, headers);
    assertTrue(balancerRpcExecutor.runDueTasks() > 0);
    verify(mockCallListener4).onClose(statusCaptor.capture(), any(Metadata.class));
    Status status4 = statusCaptor.getValue();
    assertEquals(Status.Code.UNAVAILABLE, status4.getCode());
    verify(mockCallListener5).onClose(statusCaptor.capture(), any(Metadata.class));
    Status status5 = statusCaptor.getValue();
    assertEquals(Status.Code.UNAVAILABLE, status5.getCode());
    // The pending RPC will still be pending
    verify(mockCallListener3, never()).onClose(any(Status.class), any(Metadata.class));
    // This will shutdownNow() the delayed transport, terminating the pending RPC
    assertEquals(0, balancerRpcExecutor.numPendingTasks());
    oob1.shutdownNow();
    assertTrue(balancerRpcExecutor.runDueTasks() > 0);
    verify(mockCallListener3).onClose(any(Status.class), any(Metadata.class));
    // Shut down the channel, and it will not terminated because OOB channel has not.
    channel.shutdown();
    assertFalse(channel.isTerminated());
    // Delayed transport has already terminated.  Terminating the transport terminates the
    // subchannel, which in turn terimates the OOB channel, which terminates the channel.
    assertFalse(oob1.isTerminated());
    verify(balancerRpcExecutorPool).returnObject(balancerRpcExecutor.getScheduledExecutorService());
    transportInfo.listener.transportTerminated();
    assertTrue(oob1.isTerminated());
    assertTrue(channel.isTerminated());
    verify(balancerRpcExecutorPool, times(2)).returnObject(balancerRpcExecutor.getScheduledExecutorService());
}
Also used : Status(io.grpc.Status) ClientStreamTracer(io.grpc.ClientStreamTracer) ClientTransportOptions(io.grpc.internal.ClientTransportFactory.ClientTransportOptions) Metadata(io.grpc.Metadata) ManagedChannel(io.grpc.ManagedChannel) MockClientTransportInfo(io.grpc.internal.TestUtils.MockClientTransportInfo) ChannelLogger(io.grpc.ChannelLogger) Test(org.junit.Test)

Example 4 with ClientTransportOptions

use of io.grpc.internal.ClientTransportFactory.ClientTransportOptions in project grpc-java by grpc.

the class ManagedChannelImplTest method subchannels.

@Test
public void subchannels() {
    createChannel();
    // createSubchannel() always return a new Subchannel
    Attributes attrs1 = Attributes.newBuilder().set(SUBCHANNEL_ATTR_KEY, "attr1").build();
    Attributes attrs2 = Attributes.newBuilder().set(SUBCHANNEL_ATTR_KEY, "attr2").build();
    SubchannelStateListener listener1 = mock(SubchannelStateListener.class);
    SubchannelStateListener listener2 = mock(SubchannelStateListener.class);
    final Subchannel sub1 = createSubchannelSafely(helper, addressGroup, attrs1, listener1);
    final Subchannel sub2 = createSubchannelSafely(helper, addressGroup, attrs2, listener2);
    assertNotSame(sub1, sub2);
    assertNotSame(attrs1, attrs2);
    assertSame(attrs1, sub1.getAttributes());
    assertSame(attrs2, sub2.getAttributes());
    final AtomicBoolean snippetPassed = new AtomicBoolean(false);
    helper.getSynchronizationContext().execute(new Runnable() {

        @Override
        public void run() {
            // getAddresses() must be called from sync context
            assertSame(addressGroup, sub1.getAddresses());
            assertSame(addressGroup, sub2.getAddresses());
            snippetPassed.set(true);
        }
    });
    assertThat(snippetPassed.get()).isTrue();
    // requestConnection()
    verify(mockTransportFactory, never()).newClientTransport(any(SocketAddress.class), any(ClientTransportOptions.class), any(TransportLogger.class));
    requestConnectionSafely(helper, sub1);
    verify(mockTransportFactory).newClientTransport(eq(socketAddress), eq(clientTransportOptions), isA(TransportLogger.class));
    MockClientTransportInfo transportInfo1 = transports.poll();
    assertNotNull(transportInfo1);
    requestConnectionSafely(helper, sub2);
    verify(mockTransportFactory, times(2)).newClientTransport(eq(socketAddress), eq(clientTransportOptions), isA(TransportLogger.class));
    MockClientTransportInfo transportInfo2 = transports.poll();
    assertNotNull(transportInfo2);
    requestConnectionSafely(helper, sub1);
    requestConnectionSafely(helper, sub2);
    // The subchannel doesn't matter since this isn't called
    verify(mockTransportFactory, times(2)).newClientTransport(eq(socketAddress), eq(clientTransportOptions), isA(TransportLogger.class));
    // updateAddresses()
    updateAddressesSafely(helper, sub1, Collections.singletonList(addressGroup2));
    assertThat(((InternalSubchannel) sub1.getInternalSubchannel()).getAddressGroups()).isEqualTo(Collections.singletonList(addressGroup2));
    // shutdown() has a delay
    shutdownSafely(helper, sub1);
    timer.forwardTime(ManagedChannelImpl.SUBCHANNEL_SHUTDOWN_DELAY_SECONDS - 1, TimeUnit.SECONDS);
    shutdownSafely(helper, sub1);
    verify(transportInfo1.transport, never()).shutdown(any(Status.class));
    timer.forwardTime(1, TimeUnit.SECONDS);
    verify(transportInfo1.transport).shutdown(same(ManagedChannelImpl.SUBCHANNEL_SHUTDOWN_STATUS));
    // ... but not after Channel is terminating
    verify(mockLoadBalancer, never()).shutdown();
    channel.shutdown();
    verify(mockLoadBalancer).shutdown();
    verify(transportInfo2.transport, never()).shutdown(any(Status.class));
    shutdownSafely(helper, sub2);
    verify(transportInfo2.transport).shutdown(same(ManagedChannelImpl.SHUTDOWN_STATUS));
    // Cleanup
    transportInfo1.listener.transportShutdown(Status.UNAVAILABLE);
    transportInfo1.listener.transportTerminated();
    transportInfo2.listener.transportShutdown(Status.UNAVAILABLE);
    transportInfo2.listener.transportTerminated();
    timer.forwardTime(ManagedChannelImpl.SUBCHANNEL_SHUTDOWN_DELAY_SECONDS, TimeUnit.SECONDS);
}
Also used : Status(io.grpc.Status) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SubchannelStateListener(io.grpc.LoadBalancer.SubchannelStateListener) ClientTransportOptions(io.grpc.internal.ClientTransportFactory.ClientTransportOptions) TransportLogger(io.grpc.internal.InternalSubchannel.TransportLogger) ForwardingSubchannel(io.grpc.util.ForwardingSubchannel) Subchannel(io.grpc.LoadBalancer.Subchannel) Attributes(io.grpc.Attributes) MockClientTransportInfo(io.grpc.internal.TestUtils.MockClientTransportInfo) ProxiedSocketAddress(io.grpc.ProxiedSocketAddress) SocketAddress(java.net.SocketAddress) Test(org.junit.Test)

Example 5 with ClientTransportOptions

use of io.grpc.internal.ClientTransportFactory.ClientTransportOptions 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

ClientTransportOptions (io.grpc.internal.ClientTransportFactory.ClientTransportOptions)8 Test (org.junit.Test)8 ChannelLogger (io.grpc.ChannelLogger)3 Subchannel (io.grpc.LoadBalancer.Subchannel)3 Metadata (io.grpc.Metadata)3 ProxiedSocketAddress (io.grpc.ProxiedSocketAddress)3 ForwardingSubchannel (io.grpc.util.ForwardingSubchannel)3 SocketAddress (java.net.SocketAddress)3 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)2 Status (io.grpc.Status)2 CronetTransportFactory (io.grpc.cronet.CronetChannelBuilder.CronetTransportFactory)2 MockClientTransportInfo (io.grpc.internal.TestUtils.MockClientTransportInfo)2 InetSocketAddress (java.net.InetSocketAddress)2 Attributes (io.grpc.Attributes)1 ClientStreamTracer (io.grpc.ClientStreamTracer)1 SubchannelStateListener (io.grpc.LoadBalancer.SubchannelStateListener)1 ManagedChannel (io.grpc.ManagedChannel)1 TransportLogger (io.grpc.internal.InternalSubchannel.TransportLogger)1 ArrayList (java.util.ArrayList)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1