Skip to content
This repository was archived by the owner on Sep 26, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
change channel pool test to remove reliance on timer
  • Loading branch information
tonytanger committed Nov 13, 2019
commit 193fcc55368231e70d5f038b2e010c4808f0f49e
36 changes: 26 additions & 10 deletions gax-grpc/src/test/java/com/google/api/gax/grpc/ChannelPoolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
Expand All @@ -55,7 +55,6 @@
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.threeten.bp.Duration;

@RunWith(JUnit4.class)
public class ChannelPoolTest {
Expand Down Expand Up @@ -187,11 +186,20 @@ public void channelPrimerIsCalledPeriodically() throws IOException, InterruptedE
ManagedChannel channel2 = Mockito.mock(RefreshingManagedChannel.class);
ManagedChannel channel3 = Mockito.mock(RefreshingManagedChannel.class);

ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(1);

RefreshingManagedChannel.refreshPeriod = Duration.ofMillis(100);
RefreshingManagedChannel.jitterPercentage = 0;
RefreshingManagedChannel.terminationWait = Duration.ofMillis(0);
final List<Runnable> channelRefreshers = new ArrayList<>();

ScheduledExecutorService scheduledExecutorService =
Mockito.mock(ScheduledExecutorService.class);
Mockito.when(
Comment thread
tonytanger marked this conversation as resolved.
Outdated
scheduledExecutorService.schedule(
Mockito.any(Runnable.class), Mockito.anyLong(), Mockito.eq(TimeUnit.MILLISECONDS)))
.thenAnswer(
new Answer() {
public Object answer(InvocationOnMock invocation) {
channelRefreshers.add((Runnable) invocation.getArgument(0));
return Mockito.mock(ScheduledFuture.class);
}
});

new ChannelPool(
1,
Expand All @@ -200,17 +208,25 @@ public void channelPrimerIsCalledPeriodically() throws IOException, InterruptedE
// 1 call during the creation
Mockito.verify(mockChannelPrimer, Mockito.times(1))
.primeChannel(Mockito.any(ManagedChannel.class));
Mockito.verify(scheduledExecutorService, Mockito.times(1))
.schedule(
Mockito.any(Runnable.class), Mockito.anyLong(), Mockito.eq(TimeUnit.MILLISECONDS));

TimeUnit.MILLISECONDS.sleep(150);
channelRefreshers.get(0).run();
// 1 more call during channel refresh
Mockito.verify(mockChannelPrimer, Mockito.times(2))
.primeChannel(Mockito.any(ManagedChannel.class));
scheduledExecutorService.shutdown();
Mockito.verify(scheduledExecutorService, Mockito.times(2))
.schedule(
Mockito.any(Runnable.class), Mockito.anyLong(), Mockito.eq(TimeUnit.MILLISECONDS));

TimeUnit.MILLISECONDS.sleep(100);
channelRefreshers.get(0).run();
// 1 more call during channel refresh
Mockito.verify(mockChannelPrimer, Mockito.times(3))
.primeChannel(Mockito.any(ManagedChannel.class));
Mockito.verify(scheduledExecutorService, Mockito.times(3))
.schedule(
Mockito.any(Runnable.class), Mockito.anyLong(), Mockito.eq(TimeUnit.MILLISECONDS));
scheduledExecutorService.shutdown();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,35 +89,9 @@ public Object answer(InvocationOnMock invocation) {
ManagedChannel refreshingManagedChannel =
new RefreshingManagedChannel(fakeChannelFactory, scheduledExecutorService);

final boolean[] channelsIsShutDown = {false, false};

// when shutdown is called
Mockito.doAnswer(
new Answer() {
@Override
public Object answer(InvocationOnMock invocation) {
channelsIsShutDown[0] = true;
return null;
}
})
.when(underlyingChannel1)
.shutdown();

// call 1
MockClientCall<String, Integer> mockClientCall1 = new MockClientCall<>(1, Status.OK);
final ClientCall<String, Integer> clientCall1 = Mockito.spy(mockClientCall1);

// // ensure that when the client call starts, the underlying channel has not shutdown
// Mockito.doAnswer(
// new Answer() {
// @Override
// public Object answer(InvocationOnMock invocation) throws Throwable {
// Truth.assertThat(channelsIsShutDown[0]).isFalse();
// return invocation.callRealMethod();
// }
// })
// .when(clientCall1)
// .start(Mockito.any(ClientCall.Listener.class), Mockito.any(Metadata.class));
Mockito.when(
underlyingChannel1.newCall(
Mockito.<MethodDescriptor<String, Integer>>any(), Mockito.any(CallOptions.class)))
Expand All @@ -126,18 +100,6 @@ public Object answer(InvocationOnMock invocation) {
// call 2
MockClientCall<String, Integer> mockClientCall2 = new MockClientCall<>(1, Status.OK);
final ClientCall<String, Integer> clientCall2 = Mockito.spy(mockClientCall2);
// ensure that when the client call starts, the underlying channel has not shutdown

// Mockito.doAnswer(
// new Answer() {
// @Override
// public Object answer(InvocationOnMock invocation) throws Throwable {
// Truth.assertThat(channelsIsShutDown[0]).isFalse();
// return invocation.callRealMethod();
// }
// })
// .when(clientCall2)
// .start(Mockito.any(ClientCall.Listener.class), Mockito.any(Metadata.class));
Mockito.when(
underlyingChannel1.newCall(
Mockito.<MethodDescriptor<String, Integer>>any(), Mockito.any(CallOptions.class)))
Expand Down