Search in sources :

Example 16 with MockClientTransportInfo

use of io.grpc.internal.TestUtils.MockClientTransportInfo in project grpc-java by grpc.

the class InternalSubchannelTest method transportStartReturnsRunnable.

@Test
public void transportStartReturnsRunnable() {
    SocketAddress addr1 = mock(SocketAddress.class);
    SocketAddress addr2 = mock(SocketAddress.class);
    createInternalSubchannel(addr1, addr2);
    final AtomicInteger runnableInvokes = new AtomicInteger(0);
    Runnable startRunnable = new Runnable() {

        @Override
        public void run() {
            runnableInvokes.incrementAndGet();
        }
    };
    transports = TestUtils.captureTransports(mockTransportFactory, startRunnable);
    assertEquals(0, runnableInvokes.get());
    internalSubchannel.obtainActiveTransport();
    assertEquals(1, runnableInvokes.get());
    internalSubchannel.obtainActiveTransport();
    assertEquals(1, runnableInvokes.get());
    MockClientTransportInfo t0 = transports.poll();
    t0.listener.transportShutdown(Status.UNAVAILABLE);
    assertEquals(2, runnableInvokes.get());
    // 2nd address: reconnect immediatly
    MockClientTransportInfo t1 = transports.poll();
    t1.listener.transportShutdown(Status.UNAVAILABLE);
    // Addresses exhausted, waiting for back-off.
    assertEquals(2, runnableInvokes.get());
    // Run out the back-off period
    fakeClock.forwardMillis(10);
    assertEquals(3, runnableInvokes.get());
    // This test doesn't care about scheduled InternalSubchannel callbacks.  Clear it up so that
    // noMorePendingTasks() won't fail.
    fakeExecutor.runDueTasks();
    assertEquals(3, runnableInvokes.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MockClientTransportInfo(io.grpc.internal.TestUtils.MockClientTransportInfo) SocketAddress(java.net.SocketAddress) Test(org.junit.Test)

Example 17 with MockClientTransportInfo

use of io.grpc.internal.TestUtils.MockClientTransportInfo in project grpc-java by grpc.

the class ManagedChannelImplTest method subchannels.

@Test
public void subchannels() {
    createChannel(new FakeNameResolverFactory(true), NO_INTERCEPTOR);
    // 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();
    Subchannel sub1 = helper.createSubchannel(addressGroup, attrs1);
    Subchannel sub2 = helper.createSubchannel(addressGroup, attrs2);
    assertNotSame(sub1, sub2);
    assertNotSame(attrs1, attrs2);
    assertSame(attrs1, sub1.getAttributes());
    assertSame(attrs2, sub2.getAttributes());
    assertSame(addressGroup, sub1.getAddresses());
    assertSame(addressGroup, sub2.getAddresses());
    // requestConnection()
    verify(mockTransportFactory, never()).newClientTransport(any(SocketAddress.class), any(String.class), any(String.class));
    sub1.requestConnection();
    verify(mockTransportFactory).newClientTransport(socketAddress, authority, userAgent);
    MockClientTransportInfo transportInfo1 = transports.poll();
    assertNotNull(transportInfo1);
    sub2.requestConnection();
    verify(mockTransportFactory, times(2)).newClientTransport(socketAddress, authority, userAgent);
    MockClientTransportInfo transportInfo2 = transports.poll();
    assertNotNull(transportInfo2);
    sub1.requestConnection();
    sub2.requestConnection();
    verify(mockTransportFactory, times(2)).newClientTransport(socketAddress, authority, userAgent);
    // shutdown() has a delay
    sub1.shutdown();
    timer.forwardTime(ManagedChannelImpl.SUBCHANNEL_SHUTDOWN_DELAY_SECONDS - 1, TimeUnit.SECONDS);
    sub1.shutdown();
    verify(transportInfo1.transport, never()).shutdown();
    timer.forwardTime(1, TimeUnit.SECONDS);
    verify(transportInfo1.transport).shutdown();
    // ... but not after Channel is terminating
    verify(mockLoadBalancer, never()).shutdown();
    channel.shutdown();
    verify(mockLoadBalancer).shutdown();
    verify(transportInfo2.transport, never()).shutdown();
    sub2.shutdown();
    verify(transportInfo2.transport).shutdown();
}
Also used : Subchannel(io.grpc.LoadBalancer.Subchannel) Attributes(io.grpc.Attributes) MockClientTransportInfo(io.grpc.internal.TestUtils.MockClientTransportInfo) Matchers.anyString(org.mockito.Matchers.anyString) SocketAddress(java.net.SocketAddress) Test(org.junit.Test)

Example 18 with MockClientTransportInfo

use of io.grpc.internal.TestUtils.MockClientTransportInfo in project grpc-java by grpc.

the class ManagedChannelImplTest method subchannelsWhenChannelShutdownNow.

@Test
public void subchannelsWhenChannelShutdownNow() {
    createChannel(new FakeNameResolverFactory(true), NO_INTERCEPTOR);
    Subchannel sub1 = helper.createSubchannel(addressGroup, Attributes.EMPTY);
    Subchannel sub2 = helper.createSubchannel(addressGroup, Attributes.EMPTY);
    sub1.requestConnection();
    sub2.requestConnection();
    assertEquals(2, transports.size());
    MockClientTransportInfo ti1 = transports.poll();
    MockClientTransportInfo ti2 = transports.poll();
    ti1.listener.transportReady();
    ti2.listener.transportReady();
    channel.shutdownNow();
    verify(ti1.transport).shutdownNow(any(Status.class));
    verify(ti2.transport).shutdownNow(any(Status.class));
    ti1.listener.transportShutdown(Status.UNAVAILABLE.withDescription("shutdown now"));
    ti2.listener.transportShutdown(Status.UNAVAILABLE.withDescription("shutdown now"));
    ti1.listener.transportTerminated();
    assertFalse(channel.isTerminated());
    ti2.listener.transportTerminated();
    assertTrue(channel.isTerminated());
}
Also used : Status(io.grpc.Status) Subchannel(io.grpc.LoadBalancer.Subchannel) MockClientTransportInfo(io.grpc.internal.TestUtils.MockClientTransportInfo) Test(org.junit.Test)

Aggregations

MockClientTransportInfo (io.grpc.internal.TestUtils.MockClientTransportInfo)18 Test (org.junit.Test)17 SocketAddress (java.net.SocketAddress)13 Metadata (io.grpc.Metadata)9 Matchers.anyString (org.mockito.Matchers.anyString)9 Subchannel (io.grpc.LoadBalancer.Subchannel)8 Status (io.grpc.Status)7 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)6 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)4 CallOptions (io.grpc.CallOptions)3 Helper (io.grpc.LoadBalancer.Helper)3 ManagedChannel (io.grpc.ManagedChannel)3 MethodDescriptor (io.grpc.MethodDescriptor)3 ResolvedServerInfoGroup (io.grpc.ResolvedServerInfoGroup)3 Attributes (io.grpc.Attributes)2 ResolvedServerInfo (io.grpc.ResolvedServerInfo)2 InOrder (org.mockito.InOrder)2 MetadataApplier (io.grpc.CallCredentials.MetadataApplier)1 Context (io.grpc.Context)1 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)1