Search in sources :

Example 1 with FakeChannelFactory

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);
}
Also used : ManagedChannel(io.grpc.ManagedChannel) FakeChannelFactory(com.google.api.gax.grpc.testing.FakeChannelFactory) Test(org.junit.Test)

Example 2 with FakeChannelFactory

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();
}
Also used : CallOptions(io.grpc.CallOptions) Arrays(java.util.Arrays) ScheduledFuture(java.util.concurrent.ScheduledFuture) ManagedChannel(io.grpc.ManagedChannel) RunWith(org.junit.runner.RunWith) FakeServiceGrpc(com.google.api.gax.grpc.testing.FakeServiceGrpc) ArrayList(java.util.ArrayList) Answer(org.mockito.stubbing.Answer) Lists(com.google.common.collect.Lists) ArgumentCaptor(org.mockito.ArgumentCaptor) ImmutableList(com.google.common.collect.ImmutableList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientCalls(io.grpc.stub.ClientCalls) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) MethodDescriptor(io.grpc.MethodDescriptor) FakeChannelFactory(com.google.api.gax.grpc.testing.FakeChannelFactory) Status(io.grpc.Status) ExecutorService(java.util.concurrent.ExecutorService) IOException(java.io.IOException) Test(org.junit.Test) Color(com.google.type.Color) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) ClientCall(io.grpc.ClientCall) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) List(java.util.List) Metadata(io.grpc.Metadata) FakeMethodDescriptor(com.google.api.gax.grpc.testing.FakeMethodDescriptor) Money(com.google.type.Money) Metadata(io.grpc.Metadata) FakeChannelFactory(com.google.api.gax.grpc.testing.FakeChannelFactory) CallOptions(io.grpc.CallOptions) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientCall(io.grpc.ClientCall) ManagedChannel(io.grpc.ManagedChannel) Test(org.junit.Test)

Example 3 with FakeChannelFactory

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));
}
Also used : ManagedChannel(io.grpc.ManagedChannel) FakeChannelFactory(com.google.api.gax.grpc.testing.FakeChannelFactory) Test(org.junit.Test)

Example 4 with FakeChannelFactory

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();
}
Also used : CallOptions(io.grpc.CallOptions) Arrays(java.util.Arrays) ScheduledFuture(java.util.concurrent.ScheduledFuture) ManagedChannel(io.grpc.ManagedChannel) RunWith(org.junit.runner.RunWith) FakeServiceGrpc(com.google.api.gax.grpc.testing.FakeServiceGrpc) ArrayList(java.util.ArrayList) Answer(org.mockito.stubbing.Answer) Lists(com.google.common.collect.Lists) ArgumentCaptor(org.mockito.ArgumentCaptor) ImmutableList(com.google.common.collect.ImmutableList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientCalls(io.grpc.stub.ClientCalls) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) MethodDescriptor(io.grpc.MethodDescriptor) FakeChannelFactory(com.google.api.gax.grpc.testing.FakeChannelFactory) Status(io.grpc.Status) ExecutorService(java.util.concurrent.ExecutorService) IOException(java.io.IOException) Test(org.junit.Test) Color(com.google.type.Color) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) ClientCall(io.grpc.ClientCall) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) List(java.util.List) Metadata(io.grpc.Metadata) FakeMethodDescriptor(com.google.api.gax.grpc.testing.FakeMethodDescriptor) Money(com.google.type.Money) Metadata(io.grpc.Metadata) FakeChannelFactory(com.google.api.gax.grpc.testing.FakeChannelFactory) CallOptions(io.grpc.CallOptions) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientCall(io.grpc.ClientCall) ManagedChannel(io.grpc.ManagedChannel) Test(org.junit.Test)

Example 5 with FakeChannelFactory

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);
    }
}
Also used : Color(com.google.type.Color) FakeChannelFactory(com.google.api.gax.grpc.testing.FakeChannelFactory) CallOptions(io.grpc.CallOptions) Money(com.google.type.Money) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) ManagedChannel(io.grpc.ManagedChannel) Test(org.junit.Test)

Aggregations

FakeChannelFactory (com.google.api.gax.grpc.testing.FakeChannelFactory)10 ManagedChannel (io.grpc.ManagedChannel)10 Test (org.junit.Test)10 Color (com.google.type.Color)6 Money (com.google.type.Money)6 CallOptions (io.grpc.CallOptions)6 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 ExecutorService (java.util.concurrent.ExecutorService)5 FakeMethodDescriptor (com.google.api.gax.grpc.testing.FakeMethodDescriptor)4 FakeServiceGrpc (com.google.api.gax.grpc.testing.FakeServiceGrpc)4 ImmutableList (com.google.common.collect.ImmutableList)4 Lists (com.google.common.collect.Lists)4 Truth.assertThat (com.google.common.truth.Truth.assertThat)4 ClientCall (io.grpc.ClientCall)4 Metadata (io.grpc.Metadata)4 MethodDescriptor (io.grpc.MethodDescriptor)4 Status (io.grpc.Status)4 ClientCalls (io.grpc.stub.ClientCalls)4 IOException (java.io.IOException)4