Search in sources :

Example 6 with FakeChannelFactory

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

Example 7 with FakeChannelFactory

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

Example 8 with FakeChannelFactory

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());
}
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 9 with FakeChannelFactory

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));
}
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) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ArrayList(java.util.ArrayList) ManagedChannel(io.grpc.ManagedChannel) FakeChannelFactory(com.google.api.gax.grpc.testing.FakeChannelFactory) ScheduledFuture(java.util.concurrent.ScheduledFuture) Test(org.junit.Test)

Example 10 with FakeChannelFactory

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());
}
Also used : Money(com.google.type.Money) Color(com.google.type.Color) ManagedChannel(io.grpc.ManagedChannel) Channel(io.grpc.Channel) ManagedChannel(io.grpc.ManagedChannel) FakeChannelFactory(com.google.api.gax.grpc.testing.FakeChannelFactory) 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