Search in sources :

Example 1 with FakeClientSettings

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

the class ClientContextTest method runTest.

private void runTest(boolean shouldAutoClose, boolean contextNeedsExecutor, boolean needHeaders, boolean headersCollision) throws Exception {
    FakeClientSettings.Builder builder = new FakeClientSettings.Builder();
    InterceptingExecutor executor = new InterceptingExecutor(1);
    ExecutorProvider executorProvider = new FakeExecutorProvider(executor, shouldAutoClose);
    Map<String, String> headers = ImmutableMap.of("k1", "v1", "k2", "v2");
    FakeTransportChannel transportChannel = FakeTransportChannel.create(new FakeChannel());
    FakeTransportProvider transportProvider = new FakeTransportProvider(transportChannel, contextNeedsExecutor ? null : executor, shouldAutoClose, needHeaders ? null : headers, null);
    Credentials credentials = Mockito.mock(Credentials.class);
    ApiClock clock = Mockito.mock(ApiClock.class);
    Watchdog watchdog = Watchdog.create(Mockito.mock(ApiClock.class), Duration.ZERO, Mockito.mock(ScheduledExecutorService.class));
    Duration watchdogCheckInterval = Duration.ofSeconds(11);
    builder.setExecutorProvider(executorProvider);
    builder.setTransportChannelProvider(transportProvider);
    builder.setCredentialsProvider(FixedCredentialsProvider.create(credentials));
    builder.setWatchdogProvider(FixedWatchdogProvider.create(watchdog));
    builder.setWatchdogCheckInterval(watchdogCheckInterval);
    builder.setClock(clock);
    HeaderProvider headerProvider = Mockito.mock(HeaderProvider.class);
    Mockito.when(headerProvider.getHeaders()).thenReturn(ImmutableMap.of("k1", "v1"));
    HeaderProvider internalHeaderProvider = Mockito.mock(HeaderProvider.class);
    if (headersCollision) {
        Mockito.when(internalHeaderProvider.getHeaders()).thenReturn(ImmutableMap.of("k1", "v1"));
    } else {
        Mockito.when(internalHeaderProvider.getHeaders()).thenReturn(ImmutableMap.of("k2", "v2"));
    }
    builder.setHeaderProvider(headerProvider);
    builder.setInternalHeaderProvider(internalHeaderProvider);
    FakeClientSettings settings = builder.build();
    ClientContext clientContext = ClientContext.create(settings);
    Truth.assertThat(clientContext.getExecutor()).isSameInstanceAs(executor);
    Truth.assertThat(clientContext.getTransportChannel()).isSameInstanceAs(transportChannel);
    FakeTransportChannel actualChannel = (FakeTransportChannel) clientContext.getTransportChannel();
    assert actualChannel != null;
    Truth.assertThat(actualChannel.getHeaders()).isEqualTo(headers);
    Truth.assertThat(clientContext.getCredentials()).isSameInstanceAs(credentials);
    Truth.assertThat(clientContext.getClock()).isSameInstanceAs(clock);
    Truth.assertThat(clientContext.getStreamWatchdog()).isSameInstanceAs(watchdog);
    Truth.assertThat(clientContext.getStreamWatchdogCheckInterval()).isEqualTo(watchdogCheckInterval);
    Truth.assertThat(clientContext.getHeaders()).isEqualTo(ImmutableMap.of("k1", "v1"));
    Truth.assertThat(clientContext.getInternalHeaders()).isEqualTo(ImmutableMap.of("k2", "v2"));
    Truth.assertThat(executor.shutdownCalled).isFalse();
    Truth.assertThat(transportChannel.isShutdown()).isFalse();
    List<BackgroundResource> resources = clientContext.getBackgroundResources();
    if (!resources.isEmpty()) {
        // This is slightly too implementation-specific, but we need to ensure that executor is shut
        // down after the transportChannel: https://github.com/googleapis/gax-java/issues/785
        Truth.assertThat(resources.size()).isEqualTo(2);
        Truth.assertThat(transportChannel.isShutdown()).isNotEqualTo(shouldAutoClose);
        Truth.assertThat(executor.shutdownCalled).isNotEqualTo(shouldAutoClose);
        resources.get(0).shutdown();
        Truth.assertThat(transportChannel.isShutdown()).isEqualTo(shouldAutoClose);
        Truth.assertThat(executor.shutdownCalled).isNotEqualTo(shouldAutoClose);
        resources.get(1).shutdown();
        Truth.assertThat(transportChannel.isShutdown()).isEqualTo(shouldAutoClose);
        Truth.assertThat(executor.shutdownCalled).isEqualTo(shouldAutoClose);
    }
}
Also used : FakeTransportChannel(com.google.api.gax.rpc.testing.FakeTransportChannel) FakeClientSettings(com.google.api.gax.rpc.testing.FakeClientSettings) ApiClock(com.google.api.core.ApiClock) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Duration(org.threeten.bp.Duration) BackgroundResource(com.google.api.gax.core.BackgroundResource) FixedExecutorProvider(com.google.api.gax.core.FixedExecutorProvider) ExecutorProvider(com.google.api.gax.core.ExecutorProvider) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) Credentials(com.google.auth.Credentials) FakeChannel(com.google.api.gax.rpc.testing.FakeChannel)

Example 2 with FakeClientSettings

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

the class ClientSettingsTest method testEmptyBuilder.

@Test
public void testEmptyBuilder() throws Exception {
    FakeClientSettings.Builder builder = new FakeClientSettings.Builder();
    Truth.assertThat(builder.getExecutorProvider()).isNull();
    Truth.assertThat(builder.getBackgroundExecutorProvider()).isInstanceOf(InstantiatingExecutorProvider.class);
    Truth.assertThat(builder.getTransportChannelProvider()).isNull();
    Truth.assertThat(builder.getCredentialsProvider()).isInstanceOf(NoCredentialsProvider.class);
    Truth.assertThat(builder.getClock()).isInstanceOf(NanoClock.class);
    Truth.assertThat(builder.getHeaderProvider()).isInstanceOf(NoHeaderProvider.class);
    Truth.assertThat(builder.getInternalHeaderProvider()).isInstanceOf(NoHeaderProvider.class);
    Truth.assertThat(builder.getWatchdogProvider()).isInstanceOf(InstantiatingWatchdogProvider.class);
    Truth.assertThat(builder.getWatchdogCheckInterval()).isGreaterThan(Duration.ZERO);
    Truth.assertThat(builder.getQuotaProjectId()).isNull();
    FakeClientSettings settings = builder.build();
    Truth.assertThat(settings.getExecutorProvider()).isSameInstanceAs(builder.getExecutorProvider());
    Truth.assertThat(settings.getBackgroundExecutorProvider()).isSameInstanceAs(builder.getBackgroundExecutorProvider());
    Truth.assertThat(settings.getTransportChannelProvider()).isSameInstanceAs(builder.getTransportChannelProvider());
    Truth.assertThat(settings.getCredentialsProvider()).isSameInstanceAs(builder.getCredentialsProvider());
    Truth.assertThat(settings.getClock()).isSameInstanceAs(builder.getClock());
    Truth.assertThat(settings.getHeaderProvider()).isSameInstanceAs(builder.getHeaderProvider());
    Truth.assertThat(settings.getInternalHeaderProvider()).isSameInstanceAs(builder.getInternalHeaderProvider());
    Truth.assertThat(settings.getWatchdogProvider()).isInstanceOf(InstantiatingWatchdogProvider.class);
    Truth.assertThat(settings.getWatchdogCheckInterval()).isGreaterThan(Duration.ZERO);
    Truth.assertThat((settings.getQuotaProjectId())).isSameInstanceAs(builder.getQuotaProjectId());
    String settingsString = settings.toString();
    Truth.assertThat(settingsString).contains("executorProvider");
    Truth.assertThat(settingsString).contains("backgroundExecutorProvider");
    Truth.assertThat(settingsString).contains("transportChannelProvider");
    Truth.assertThat(settingsString).contains("credentialsProvider");
    Truth.assertThat(settingsString).contains("clock");
    Truth.assertThat(settingsString).contains("headerProvider");
    Truth.assertThat(settingsString).contains("watchdogProvider");
    Truth.assertThat(settingsString).contains("watchdogCheckInterval");
    Truth.assertThat(settingsString).contains(("quotaProjectId"));
}
Also used : FakeClientSettings(com.google.api.gax.rpc.testing.FakeClientSettings) Test(org.junit.Test)

Example 3 with FakeClientSettings

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

the class ClientSettingsTest method testBuilderFromSettings.

@Test
public void testBuilderFromSettings() throws Exception {
    FakeClientSettings.Builder builder = new FakeClientSettings.Builder();
    ExecutorProvider executorProvider = Mockito.mock(ExecutorProvider.class);
    TransportChannelProvider transportProvider = Mockito.mock(TransportChannelProvider.class);
    CredentialsProvider credentialsProvider = Mockito.mock(CredentialsProvider.class);
    ApiClock clock = Mockito.mock(ApiClock.class);
    HeaderProvider headerProvider = Mockito.mock(HeaderProvider.class);
    HeaderProvider internalHeaderProvider = Mockito.mock(HeaderProvider.class);
    WatchdogProvider watchdogProvider = Mockito.mock(WatchdogProvider.class);
    Duration watchdogCheckInterval = Duration.ofSeconds(14);
    String quotaProjectId = "test_builder_from_settings_quotaProjectId";
    builder.setExecutorProvider(executorProvider);
    builder.setTransportChannelProvider(transportProvider);
    builder.setCredentialsProvider(credentialsProvider);
    builder.setClock(clock);
    builder.setHeaderProvider(headerProvider);
    builder.setInternalHeaderProvider(internalHeaderProvider);
    builder.setWatchdogProvider(watchdogProvider);
    builder.setWatchdogCheckInterval(watchdogCheckInterval);
    builder.setQuotaProjectId(quotaProjectId);
    FakeClientSettings settings = builder.build();
    FakeClientSettings.Builder newBuilder = new FakeClientSettings.Builder(settings);
    Truth.assertThat(newBuilder.getExecutorProvider()).isSameInstanceAs(executorProvider);
    Truth.assertThat(newBuilder.getBackgroundExecutorProvider()).isSameInstanceAs(executorProvider);
    Truth.assertThat(newBuilder.getTransportChannelProvider()).isSameInstanceAs(transportProvider);
    Truth.assertThat(newBuilder.getCredentialsProvider()).isSameInstanceAs(credentialsProvider);
    Truth.assertThat(newBuilder.getClock()).isSameInstanceAs(clock);
    Truth.assertThat(newBuilder.getHeaderProvider()).isSameInstanceAs(headerProvider);
    Truth.assertThat(newBuilder.getInternalHeaderProvider()).isSameInstanceAs(internalHeaderProvider);
    Truth.assertThat(newBuilder.getWatchdogProvider()).isSameInstanceAs(watchdogProvider);
    Truth.assertThat(newBuilder.getWatchdogCheckInterval()).isEqualTo(watchdogCheckInterval);
    Truth.assertThat(newBuilder.getQuotaProjectId()).isEqualTo(quotaProjectId);
}
Also used : FakeClientSettings(com.google.api.gax.rpc.testing.FakeClientSettings) ApiClock(com.google.api.core.ApiClock) Duration(org.threeten.bp.Duration) FixedCredentialsProvider(com.google.api.gax.core.FixedCredentialsProvider) NoCredentialsProvider(com.google.api.gax.core.NoCredentialsProvider) CredentialsProvider(com.google.api.gax.core.CredentialsProvider) FixedExecutorProvider(com.google.api.gax.core.FixedExecutorProvider) InstantiatingExecutorProvider(com.google.api.gax.core.InstantiatingExecutorProvider) ExecutorProvider(com.google.api.gax.core.ExecutorProvider) Test(org.junit.Test)

Aggregations

FakeClientSettings (com.google.api.gax.rpc.testing.FakeClientSettings)3 ApiClock (com.google.api.core.ApiClock)2 ExecutorProvider (com.google.api.gax.core.ExecutorProvider)2 FixedExecutorProvider (com.google.api.gax.core.FixedExecutorProvider)2 Test (org.junit.Test)2 Duration (org.threeten.bp.Duration)2 BackgroundResource (com.google.api.gax.core.BackgroundResource)1 CredentialsProvider (com.google.api.gax.core.CredentialsProvider)1 FixedCredentialsProvider (com.google.api.gax.core.FixedCredentialsProvider)1 InstantiatingExecutorProvider (com.google.api.gax.core.InstantiatingExecutorProvider)1 NoCredentialsProvider (com.google.api.gax.core.NoCredentialsProvider)1 FakeChannel (com.google.api.gax.rpc.testing.FakeChannel)1 FakeTransportChannel (com.google.api.gax.rpc.testing.FakeTransportChannel)1 Credentials (com.google.auth.Credentials)1 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1