use of com.google.api.gax.grpc.testing.FakeChannelFactory in project gax-java by googleapis.
the class ChannelPoolTest method channelRefreshShouldSwapChannels.
@Test
public void channelRefreshShouldSwapChannels() throws IOException {
ManagedChannel underlyingChannel1 = Mockito.mock(ManagedChannel.class);
ManagedChannel underlyingChannel2 = Mockito.mock(ManagedChannel.class);
// mock executor service to capture the runnable scheduled, so we can invoke it when we want to
ScheduledExecutorService scheduledExecutorService = Mockito.mock(ScheduledExecutorService.class);
Mockito.doReturn(null).when(scheduledExecutorService).schedule(Mockito.any(Runnable.class), Mockito.anyLong(), Mockito.eq(TimeUnit.MILLISECONDS));
FakeChannelFactory channelFactory = new FakeChannelFactory(ImmutableList.of(underlyingChannel1, underlyingChannel2));
ChannelPool pool = new ChannelPool(ChannelPoolSettings.staticallySized(1).toBuilder().setPreemptiveRefreshEnabled(true).build(), channelFactory, scheduledExecutorService);
Mockito.reset(underlyingChannel1);
pool.newCall(FakeMethodDescriptor.<String, Integer>create(), CallOptions.DEFAULT);
Mockito.verify(underlyingChannel1, Mockito.only()).newCall(Mockito.<MethodDescriptor<String, Integer>>any(), Mockito.any(CallOptions.class));
// swap channel
pool.refresh();
pool.newCall(FakeMethodDescriptor.<String, Integer>create(), CallOptions.DEFAULT);
Mockito.verify(underlyingChannel2, Mockito.only()).newCall(Mockito.<MethodDescriptor<String, Integer>>any(), Mockito.any(CallOptions.class));
}
use of com.google.api.gax.grpc.testing.FakeChannelFactory in project gax-java by googleapis.
the class ChannelPoolTest method testAuthority.
@Test
public void testAuthority() throws IOException {
ManagedChannel sub1 = Mockito.mock(ManagedChannel.class);
ManagedChannel sub2 = Mockito.mock(ManagedChannel.class);
Mockito.when(sub1.authority()).thenReturn("myAuth");
ChannelPool pool = ChannelPool.create(ChannelPoolSettings.staticallySized(2), new FakeChannelFactory(Arrays.asList(sub1, sub2)));
assertThat(pool.authority()).isEqualTo("myAuth");
}
use of com.google.api.gax.grpc.testing.FakeChannelFactory in project gax-java by googleapis.
the class ChannelPoolTest method callShouldCompleteAfterCreation.
// ----
// call should be allowed to complete and the channel should not be shutdown
@Test
public void callShouldCompleteAfterCreation() 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 entry
@SuppressWarnings("unchecked") ClientCall.Listener<Integer> listener = Mockito.mock(ClientCall.Listener.class);
ClientCall<String, Integer> call = pool.newCall(FakeMethodDescriptor.create(), CallOptions.DEFAULT);
pool.refresh();
// shutdown is not called because there is still an outstanding call, even if it hasn't started
Mockito.verify(underlyingChannel, Mockito.after(200).never()).shutdown();
// start clientCall
call.start(listener, new Metadata());
// send message and end the call
call.sendMessage("message");
// shutdown is called because the outstanding call has completed
Mockito.verify(underlyingChannel, Mockito.atLeastOnce()).shutdown();
// Replacement channel shouldn't be touched
Mockito.verify(replacementChannel, Mockito.never()).shutdown();
Mockito.verify(replacementChannel, Mockito.never()).newCall(Mockito.any(), Mockito.any());
}
use of com.google.api.gax.grpc.testing.FakeChannelFactory in project gax-java by googleapis.
the class ChannelPoolTest method channelPrimerIsCalledPeriodically.
// Test channelPrimer is called periodically, if there's an executorService
@Test
public void channelPrimerIsCalledPeriodically() throws IOException {
ChannelPrimer mockChannelPrimer = Mockito.mock(ChannelPrimer.class);
ManagedChannel channel1 = Mockito.mock(ManagedChannel.class);
ManagedChannel channel2 = Mockito.mock(ManagedChannel.class);
ManagedChannel channel3 = Mockito.mock(ManagedChannel.class);
List<Runnable> channelRefreshers = new ArrayList<>();
ScheduledExecutorService scheduledExecutorService = Mockito.mock(ScheduledExecutorService.class);
Answer<?> extractChannelRefresher = invocation -> {
channelRefreshers.add(invocation.getArgument(0));
return Mockito.mock(ScheduledFuture.class);
};
Mockito.doAnswer(extractChannelRefresher).when(scheduledExecutorService).scheduleAtFixedRate(Mockito.any(Runnable.class), Mockito.anyLong(), Mockito.anyLong(), Mockito.any());
FakeChannelFactory channelFactory = new FakeChannelFactory(Arrays.asList(channel1, channel2, channel3), mockChannelPrimer);
new ChannelPool(ChannelPoolSettings.staticallySized(1).toBuilder().setPreemptiveRefreshEnabled(true).build(), channelFactory, scheduledExecutorService);
// 1 call during the creation
Mockito.verify(mockChannelPrimer, Mockito.times(1)).primeChannel(Mockito.any(ManagedChannel.class));
channelRefreshers.get(0).run();
// 1 more call during channel refresh
Mockito.verify(mockChannelPrimer, Mockito.times(2)).primeChannel(Mockito.any(ManagedChannel.class));
channelRefreshers.get(0).run();
// 1 more call during channel refresh
Mockito.verify(mockChannelPrimer, Mockito.times(3)).primeChannel(Mockito.any(ManagedChannel.class));
}
use of com.google.api.gax.grpc.testing.FakeChannelFactory in project gax-java by googleapis.
the class GrpcClientCallsTest method testAffinity.
@Test
public void testAffinity() throws IOException {
MethodDescriptor<Color, Money> descriptor = FakeServiceGrpc.METHOD_RECOGNIZE;
@SuppressWarnings("unchecked") ClientCall<Color, Money> clientCall0 = Mockito.mock(ClientCall.class);
@SuppressWarnings("unchecked") ClientCall<Color, Money> clientCall1 = Mockito.mock(ClientCall.class);
ManagedChannel channel0 = Mockito.mock(ManagedChannel.class);
ManagedChannel channel1 = Mockito.mock(ManagedChannel.class);
Mockito.when(channel0.newCall(Mockito.eq(descriptor), Mockito.<CallOptions>any())).thenReturn(clientCall0);
Mockito.when(channel1.newCall(Mockito.eq(descriptor), Mockito.<CallOptions>any())).thenReturn(clientCall1);
Channel pool = ChannelPool.create(ChannelPoolSettings.staticallySized(2), new FakeChannelFactory(Arrays.asList(channel0, channel1)));
GrpcCallContext context = GrpcCallContext.createDefault().withChannel(pool);
ClientCall<Color, Money> gotCallA = GrpcClientCalls.newCall(descriptor, context.withChannelAffinity(0));
ClientCall<Color, Money> gotCallB = GrpcClientCalls.newCall(descriptor, context.withChannelAffinity(0));
ClientCall<Color, Money> gotCallC = GrpcClientCalls.newCall(descriptor, context.withChannelAffinity(1));
verify(channel0, Mockito.times(2)).newCall(Mockito.eq(descriptor), Mockito.any());
verify(channel1, Mockito.times(1)).newCall(Mockito.eq(descriptor), Mockito.any());
}
Aggregations