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());
}
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();
}
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());
}
Aggregations