use of com.google.api.gax.grpc.testing.FakeChannelFactory in project gax-java by googleapis.
the class ChannelPoolTest method testRoundRobin.
@Test
public void testRoundRobin() throws IOException {
ManagedChannel sub1 = Mockito.mock(ManagedChannel.class);
ManagedChannel sub2 = Mockito.mock(ManagedChannel.class);
Mockito.when(sub1.authority()).thenReturn("myAuth");
ArrayList<ManagedChannel> channels = Lists.newArrayList(sub1, sub2);
ChannelPool pool = ChannelPool.create(ChannelPoolSettings.staticallySized(channels.size()), new FakeChannelFactory(channels));
verifyTargetChannel(pool, channels, sub1);
verifyTargetChannel(pool, channels, sub2);
verifyTargetChannel(pool, channels, sub1);
}
use of com.google.api.gax.grpc.testing.FakeChannelFactory in project gax-java by googleapis.
the class ChannelPoolTest method callShouldCompleteAfterStarted.
// call should be allowed to complete and the channel should not be shutdown
@Test
public void callShouldCompleteAfterStarted() throws IOException {
final ManagedChannel underlyingChannel = Mockito.mock(ManagedChannel.class);
ManagedChannel replacementChannel = Mockito.mock(ManagedChannel.class);
FakeChannelFactory channelFactory = new FakeChannelFactory(ImmutableList.of(underlyingChannel, replacementChannel));
ChannelPool pool = ChannelPool.create(ChannelPoolSettings.staticallySized(1), channelFactory);
// create a mock call when new call comes to the underlying channel
MockClientCall<String, Integer> mockClientCall = new MockClientCall<>(1, Status.OK);
MockClientCall<String, Integer> spyClientCall = Mockito.spy(mockClientCall);
Mockito.when(underlyingChannel.newCall(Mockito.<MethodDescriptor<String, Integer>>any(), Mockito.any(CallOptions.class))).thenReturn(spyClientCall);
Answer<Object> verifyChannelNotShutdown = invocation -> {
Mockito.verify(underlyingChannel, Mockito.never()).shutdown();
return invocation.callRealMethod();
};
// verify that underlying channel is not shutdown when clientCall is still sending message
Mockito.doAnswer(verifyChannelNotShutdown).when(spyClientCall).sendMessage(Mockito.anyString());
// create a new call on safeShutdownManagedChannel
@SuppressWarnings("unchecked") ClientCall.Listener<Integer> listener = Mockito.mock(ClientCall.Listener.class);
ClientCall<String, Integer> call = pool.newCall(FakeMethodDescriptor.create(), CallOptions.DEFAULT);
// start clientCall
call.start(listener, new Metadata());
pool.refresh();
// shutdown is not called because there is still an outstanding call
Mockito.verify(underlyingChannel, Mockito.after(200).never()).shutdown();
// send message and end the call
call.sendMessage("message");
// shutdown is called because the outstanding call has completed
Mockito.verify(underlyingChannel, Mockito.atLeastOnce()).shutdown();
}
use of com.google.api.gax.grpc.testing.FakeChannelFactory in project gax-java by googleapis.
the class ChannelPoolTest method channelPrimerShouldCallPoolConstruction.
// Test channelPrimer is called same number of times as poolSize if executorService is set to null
@Test
public void channelPrimerShouldCallPoolConstruction() throws IOException {
ChannelPrimer mockChannelPrimer = Mockito.mock(ChannelPrimer.class);
ManagedChannel channel1 = Mockito.mock(ManagedChannel.class);
ManagedChannel channel2 = Mockito.mock(ManagedChannel.class);
ChannelPool.create(ChannelPoolSettings.staticallySized(2).toBuilder().setPreemptiveRefreshEnabled(true).build(), new FakeChannelFactory(Arrays.asList(channel1, channel2), mockChannelPrimer));
Mockito.verify(mockChannelPrimer, Mockito.times(2)).primeChannel(Mockito.any(ManagedChannel.class));
}
use of com.google.api.gax.grpc.testing.FakeChannelFactory in project gax-java by googleapis.
the class ChannelPoolTest method channelShouldShutdown.
// Channel should be shutdown after a refresh all the calls have completed
@Test
public void channelShouldShutdown() throws IOException {
ManagedChannel underlyingChannel = Mockito.mock(ManagedChannel.class);
ManagedChannel replacementChannel = Mockito.mock(ManagedChannel.class);
FakeChannelFactory channelFactory = new FakeChannelFactory(ImmutableList.of(underlyingChannel, replacementChannel));
ChannelPool pool = ChannelPool.create(ChannelPoolSettings.staticallySized(1), channelFactory);
// create a mock call when new call comes to the underlying channel
MockClientCall<String, Integer> mockClientCall = new MockClientCall<>(1, Status.OK);
MockClientCall<String, Integer> spyClientCall = Mockito.spy(mockClientCall);
Mockito.when(underlyingChannel.newCall(Mockito.<MethodDescriptor<String, Integer>>any(), Mockito.any(CallOptions.class))).thenReturn(spyClientCall);
Answer<Object> verifyChannelNotShutdown = invocation -> {
Mockito.verify(underlyingChannel, Mockito.never()).shutdown();
return invocation.callRealMethod();
};
// verify that underlying channel is not shutdown when clientCall is still sending message
Mockito.doAnswer(verifyChannelNotShutdown).when(spyClientCall).sendMessage(Mockito.anyString());
// create a new call on safeShutdownManagedChannel
@SuppressWarnings("unchecked") ClientCall.Listener<Integer> listener = Mockito.mock(ClientCall.Listener.class);
ClientCall<String, Integer> call = pool.newCall(FakeMethodDescriptor.create(), CallOptions.DEFAULT);
// start clientCall
call.start(listener, new Metadata());
// send message and end the call
call.sendMessage("message");
// shutdown is not called because it has not been shutdown yet
Mockito.verify(underlyingChannel, Mockito.after(200).never()).shutdown();
pool.refresh();
// shutdown is called because the outstanding call has completed
Mockito.verify(underlyingChannel, Mockito.atLeastOnce()).shutdown();
}
use of com.google.api.gax.grpc.testing.FakeChannelFactory in project gax-java by googleapis.
the class ChannelPoolTest method ensureEvenDistribution.
@Test
public void ensureEvenDistribution() throws InterruptedException, IOException {
int numChannels = 10;
final ManagedChannel[] channels = new ManagedChannel[numChannels];
final AtomicInteger[] counts = new AtomicInteger[numChannels];
final MethodDescriptor<Color, Money> methodDescriptor = FakeServiceGrpc.METHOD_RECOGNIZE;
final CallOptions callOptions = CallOptions.DEFAULT;
@SuppressWarnings("unchecked") final ClientCall<Color, Money> clientCall = Mockito.mock(ClientCall.class);
for (int i = 0; i < numChannels; i++) {
final int index = i;
counts[i] = new AtomicInteger();
channels[i] = Mockito.mock(ManagedChannel.class);
Mockito.when(channels[i].newCall(methodDescriptor, callOptions)).thenAnswer((ignored) -> {
counts[index].incrementAndGet();
return clientCall;
});
}
final ChannelPool pool = ChannelPool.create(ChannelPoolSettings.staticallySized(numChannels), new FakeChannelFactory(Arrays.asList(channels)));
int numThreads = 20;
final int numPerThread = 1000;
ExecutorService executor = Executors.newFixedThreadPool(numThreads);
for (int i = 0; i < numThreads; i++) {
executor.submit(() -> {
for (int j = 0; j < numPerThread; j++) {
pool.newCall(methodDescriptor, callOptions);
}
});
}
executor.shutdown();
boolean shutdown = executor.awaitTermination(1, TimeUnit.MINUTES);
assertThat(shutdown).isTrue();
int expectedCount = (numThreads * numPerThread) / numChannels;
for (AtomicInteger count : counts) {
assertThat(count.get()).isAnyOf(expectedCount, expectedCount + 1);
}
}
Aggregations