Search in sources :

Example 11 with FakeChannel

use of com.google.api.gax.rpc.testing.FakeChannel in project gax-java by googleapis.

the class ClientContextTest method testUserAgentExternalOnly.

@Test
public void testUserAgentExternalOnly() throws Exception {
    TransportChannelProvider transportChannelProvider = new FakeTransportProvider(FakeTransportChannel.create(new FakeChannel()), null, true, null, null);
    ClientSettings.Builder builder = new FakeClientSettings.Builder().setExecutorProvider(FixedExecutorProvider.create(Mockito.mock(ScheduledExecutorService.class))).setTransportChannelProvider(transportChannelProvider).setCredentialsProvider(FixedCredentialsProvider.create(Mockito.mock(GoogleCredentials.class)));
    builder.setHeaderProvider(FixedHeaderProvider.create("user-agent", "user-supplied-agent"));
    ClientContext clientContext = ClientContext.create(builder.build());
    FakeTransportChannel transportChannel = (FakeTransportChannel) clientContext.getTransportChannel();
    assertThat(transportChannel.getHeaders()).containsEntry("user-agent", "user-supplied-agent");
}
Also used : FakeTransportChannel(com.google.api.gax.rpc.testing.FakeTransportChannel) FakeClientSettings(com.google.api.gax.rpc.testing.FakeClientSettings) FakeClientSettings(com.google.api.gax.rpc.testing.FakeClientSettings) FakeChannel(com.google.api.gax.rpc.testing.FakeChannel) Test(org.junit.Test)

Example 12 with FakeChannel

use of com.google.api.gax.rpc.testing.FakeChannel in project gax-java by googleapis.

the class RetryingTest method resetClock.

@Before
public void resetClock() {
    fakeClock = new FakeApiClock(System.nanoTime());
    executor = RecordingScheduler.create(fakeClock);
    clientContext = ClientContext.newBuilder().setExecutor(executor).setClock(fakeClock).setDefaultCallContext(FakeCallContext.createDefault()).setTransportChannel(FakeTransportChannel.create(new FakeChannel())).build();
}
Also used : FakeApiClock(com.google.api.gax.core.FakeApiClock) FakeChannel(com.google.api.gax.rpc.testing.FakeChannel) Before(org.junit.Before)

Example 13 with FakeChannel

use of com.google.api.gax.rpc.testing.FakeChannel in project gax-java by googleapis.

the class ServerStreamingCallableTest method testIteratedServerStreamingCallWithContext.

@Test
public void testIteratedServerStreamingCallWithContext() {
    FakeChannel channel = new FakeChannel();
    Credentials credentials = Mockito.mock(Credentials.class);
    ApiCallContext context = FakeCallContext.createDefault().withChannel(channel).withCredentials(credentials);
    ServerStreamingStashCallable<Integer, Integer> stashCallable = new ServerStreamingStashCallable<>();
    ServerStreamingCallable<Integer, Integer> callable = stashCallable.withDefaultCallContext(FakeCallContext.createDefault());
    Integer request = 1;
    callable.call(request, context);
    Truth.assertThat(stashCallable.getActualRequest()).isSameInstanceAs(request);
    FakeCallContext actualContext = (FakeCallContext) stashCallable.getContext();
    Truth.assertThat(actualContext.getChannel()).isSameInstanceAs(channel);
    Truth.assertThat(actualContext.getCredentials()).isSameInstanceAs(credentials);
}
Also used : FakeCallContext(com.google.api.gax.rpc.testing.FakeCallContext) ServerStreamingStashCallable(com.google.api.gax.rpc.testing.FakeStreamingApi.ServerStreamingStashCallable) Credentials(com.google.auth.Credentials) FakeChannel(com.google.api.gax.rpc.testing.FakeChannel) Test(org.junit.Test)

Example 14 with FakeChannel

use of com.google.api.gax.rpc.testing.FakeChannel in project gax-java by googleapis.

the class GrpcCallContextTest method testWithTransportChannelWrongType.

@Test
public void testWithTransportChannelWrongType() {
    FakeChannel channel = new FakeChannel();
    try {
        GrpcCallContext.createDefault().withTransportChannel(FakeTransportChannel.create(channel));
        Assert.fail("GrpcCallContext should have thrown an exception");
    } catch (IllegalArgumentException expected) {
        Truth.assertThat(expected).hasMessageThat().contains("Expected GrpcTransportChannel");
    }
}
Also used : FakeChannel(com.google.api.gax.rpc.testing.FakeChannel) Test(org.junit.Test)

Example 15 with FakeChannel

use of com.google.api.gax.rpc.testing.FakeChannel in project gax-java by googleapis.

the class ClientContextTest method testExecutorSettings.

@Test
public void testExecutorSettings() throws Exception {
    TransportChannelProvider transportChannelProvider = new FakeTransportProvider(FakeTransportChannel.create(new FakeChannel()), null, true, null, null);
    ClientSettings.Builder builder = new FakeClientSettings.Builder().setTransportChannelProvider(transportChannelProvider).setCredentialsProvider(FixedCredentialsProvider.create(Mockito.mock(GoogleCredentials.class)));
    // By default, if executor is not set, channel provider should not have an executor set
    ClientContext context = ClientContext.create(builder.build());
    FakeTransportChannel transportChannel = (FakeTransportChannel) context.getTransportChannel();
    assertThat(transportChannel.getExecutor()).isNull();
    ExecutorProvider channelExecutorProvider = FixedExecutorProvider.create(Mockito.mock(ScheduledExecutorService.class));
    builder.setTransportChannelProvider(transportChannelProvider.withExecutor((Executor) channelExecutorProvider.getExecutor()));
    context = ClientContext.create(builder.build());
    transportChannel = (FakeTransportChannel) context.getTransportChannel();
    assertThat(transportChannel.getExecutor()).isSameInstanceAs(channelExecutorProvider.getExecutor());
    ExecutorProvider executorProvider = FixedExecutorProvider.create(Mockito.mock(ScheduledExecutorService.class));
    assertThat(channelExecutorProvider.getExecutor()).isNotSameInstanceAs(executorProvider.getExecutor());
    // For backward compatibility, if executor is set from stubSettings.setExecutor, and transport
    // channel already has an executor, the ExecutorProvider set in stubSettings won't override
    // transport channel's executor
    builder.setExecutorProvider(executorProvider);
    context = ClientContext.create(builder.build());
    transportChannel = (FakeTransportChannel) context.getTransportChannel();
    assertThat(transportChannel.getExecutor()).isSameInstanceAs(channelExecutorProvider.getExecutor());
    // For backward compatibility, if executor is set from stubSettings.setExecutor, and transport
    // channel doesn't have an executor, transport channel will get the executor from
    // stubSettings.setExecutor
    builder.setExecutorProvider(executorProvider);
    builder.setTransportChannelProvider(new FakeTransportProvider(FakeTransportChannel.create(new FakeChannel()), null, true, null, null));
    context = ClientContext.create(builder.build());
    transportChannel = (FakeTransportChannel) context.getTransportChannel();
    assertThat(transportChannel.getExecutor()).isSameInstanceAs(executorProvider.getExecutor());
}
Also used : FakeTransportChannel(com.google.api.gax.rpc.testing.FakeTransportChannel) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Executor(java.util.concurrent.Executor) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) FakeClientSettings(com.google.api.gax.rpc.testing.FakeClientSettings) FixedExecutorProvider(com.google.api.gax.core.FixedExecutorProvider) ExecutorProvider(com.google.api.gax.core.ExecutorProvider) FakeChannel(com.google.api.gax.rpc.testing.FakeChannel) Test(org.junit.Test)

Aggregations

FakeChannel (com.google.api.gax.rpc.testing.FakeChannel)28 Test (org.junit.Test)24 FakeTransportChannel (com.google.api.gax.rpc.testing.FakeTransportChannel)12 Credentials (com.google.auth.Credentials)12 FakeClientSettings (com.google.api.gax.rpc.testing.FakeClientSettings)11 FakeCallContext (com.google.api.gax.rpc.testing.FakeCallContext)9 BackgroundResource (com.google.api.gax.core.BackgroundResource)5 ServerStreamingStashCallable (com.google.api.gax.rpc.testing.FakeStreamingApi.ServerStreamingStashCallable)4 OperationStashCallable (com.google.api.gax.rpc.testing.FakeOperationApi.OperationStashCallable)3 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)3 List (java.util.List)3 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)3 Before (org.junit.Before)3 ApiClock (com.google.api.core.ApiClock)2 CredentialsProvider (com.google.api.gax.core.CredentialsProvider)2 ExecutorProvider (com.google.api.gax.core.ExecutorProvider)2 FakeApiClock (com.google.api.gax.core.FakeApiClock)2 FixedCredentialsProvider (com.google.api.gax.core.FixedCredentialsProvider)2 FixedExecutorProvider (com.google.api.gax.core.FixedExecutorProvider)2 RetrySettings (com.google.api.gax.retrying.RetrySettings)2