Search in sources :

Example 1 with FakeTransportChannel

use of com.google.api.gax.rpc.testing.FakeTransportChannel 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 FakeTransportChannel

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

the class ClientContextTest method testMergeHeaders_getQuotaProjectIdFromSettings.

@Test
public void testMergeHeaders_getQuotaProjectIdFromSettings() throws IOException {
    final String QUOTA_PROJECT_ID_KEY = "x-goog-user-project";
    final String QUOTA_PROJECT_ID_FROM_HEADERS = "quota_project_id_from_headers";
    final String QUOTA_PROJECT_ID_FROM_INTERNAL_HEADERS = "quota_project_id_from_internal_headers";
    final String QUOTA_PROJECT_ID_FROM_SETTINGS = "quota_project_id_from_settings";
    FakeClientSettings.Builder builder = new FakeClientSettings.Builder();
    InterceptingExecutor executor = new InterceptingExecutor(1);
    FakeTransportChannel transportChannel = FakeTransportChannel.create(new FakeChannel());
    FakeTransportProvider transportProvider = new FakeTransportProvider(transportChannel, executor, true, null, null);
    HeaderProvider headerProvider = new HeaderProvider() {

        @Override
        public Map<String, String> getHeaders() {
            return ImmutableMap.of(QUOTA_PROJECT_ID_KEY, QUOTA_PROJECT_ID_FROM_HEADERS, "k2", "v2");
        }
    };
    HeaderProvider internalHeaderProvider = new HeaderProvider() {

        @Override
        public Map<String, String> getHeaders() {
            return ImmutableMap.of(QUOTA_PROJECT_ID_KEY, QUOTA_PROJECT_ID_FROM_INTERNAL_HEADERS, "internal_header_k1", "v1");
        }
    };
    builder.setTransportChannelProvider(transportProvider);
    builder.setCredentialsProvider(FixedCredentialsProvider.create(Mockito.mock(Credentials.class)));
    builder.setHeaderProvider(headerProvider);
    builder.setInternalHeaderProvider(internalHeaderProvider);
    builder.setQuotaProjectId(QUOTA_PROJECT_ID_FROM_SETTINGS);
    ClientContext context = ClientContext.create(builder.build());
    List<BackgroundResource> resources = context.getBackgroundResources();
    FakeTransportChannel fakeTransportChannel = (FakeTransportChannel) resources.get(0);
    assertThat(fakeTransportChannel.getHeaders().size()).isEqualTo(headerProvider.getHeaders().size() + internalHeaderProvider.getHeaders().size() - 1);
    assertThat(fakeTransportChannel.getHeaders().get(QUOTA_PROJECT_ID_KEY)).isEqualTo(QUOTA_PROJECT_ID_FROM_SETTINGS);
}
Also used : FakeTransportChannel(com.google.api.gax.rpc.testing.FakeTransportChannel) FakeClientSettings(com.google.api.gax.rpc.testing.FakeClientSettings) BackgroundResource(com.google.api.gax.core.BackgroundResource) FakeChannel(com.google.api.gax.rpc.testing.FakeChannel) Test(org.junit.Test)

Example 3 with FakeTransportChannel

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

the class ClientContextTest method testUserAgentConcat.

@Test
public void testUserAgentConcat() 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"));
    builder.setInternalHeaderProvider(FixedHeaderProvider.create("user-agent", "internal-agent"));
    ClientContext clientContext = ClientContext.create(builder.build());
    FakeTransportChannel transportChannel = (FakeTransportChannel) clientContext.getTransportChannel();
    assertThat(transportChannel.getHeaders()).containsEntry("user-agent", "user-supplied-agent internal-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 4 with FakeTransportChannel

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

the class ClientContextTest method testHidingQuotaProjectId_noQuotaSetFromSetting.

@Test
public void testHidingQuotaProjectId_noQuotaSetFromSetting() throws IOException {
    FakeClientSettings.Builder builder = new FakeClientSettings.Builder();
    InterceptingExecutor executor = new InterceptingExecutor(1);
    FakeTransportChannel transportChannel = FakeTransportChannel.create(new FakeChannel());
    FakeTransportProvider transportProvider = new FakeTransportProvider(transportChannel, executor, true, null, null);
    Map<String, List<String>> metaData = ImmutableMap.of("k1", Collections.singletonList("v1"));
    final Credentials credentialsWithoutQuotaProjectId = Mockito.mock(GoogleCredentials.class);
    Mockito.when(credentialsWithoutQuotaProjectId.getRequestMetadata(null)).thenReturn(metaData);
    HeaderProvider headerProviderWithQuota = Mockito.mock(HeaderProvider.class);
    HeaderProvider internalHeaderProvider = Mockito.mock(HeaderProvider.class);
    builder.setExecutorProvider(new FakeExecutorProvider(executor, true));
    builder.setTransportChannelProvider(transportProvider);
    builder.setCredentialsProvider(new CredentialsProvider() {

        @Override
        public Credentials getCredentials() throws IOException {
            return credentialsWithoutQuotaProjectId;
        }
    });
    builder.setHeaderProvider(headerProviderWithQuota);
    builder.setInternalHeaderProvider(internalHeaderProvider);
    ClientContext clientContext = ClientContext.create(builder.build());
    assertThat(clientContext.getCredentials().getRequestMetadata(null)).isEqualTo(metaData);
}
Also used : FakeTransportChannel(com.google.api.gax.rpc.testing.FakeTransportChannel) FakeClientSettings(com.google.api.gax.rpc.testing.FakeClientSettings) FixedCredentialsProvider(com.google.api.gax.core.FixedCredentialsProvider) CredentialsProvider(com.google.api.gax.core.CredentialsProvider) IOException(java.io.IOException) List(java.util.List) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) Credentials(com.google.auth.Credentials) FakeChannel(com.google.api.gax.rpc.testing.FakeChannel) Test(org.junit.Test)

Example 5 with FakeTransportChannel

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

the class ClientContextTest method testUserAgentInternalOnly.

@Test
public void testUserAgentInternalOnly() 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.setInternalHeaderProvider(FixedHeaderProvider.create("user-agent", "internal-agent"));
    ClientContext clientContext = ClientContext.create(builder.build());
    FakeTransportChannel transportChannel = (FakeTransportChannel) clientContext.getTransportChannel();
    assertThat(transportChannel.getHeaders()).containsEntry("user-agent", "internal-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)

Aggregations

FakeChannel (com.google.api.gax.rpc.testing.FakeChannel)11 FakeClientSettings (com.google.api.gax.rpc.testing.FakeClientSettings)11 FakeTransportChannel (com.google.api.gax.rpc.testing.FakeTransportChannel)11 Test (org.junit.Test)10 BackgroundResource (com.google.api.gax.core.BackgroundResource)5 Credentials (com.google.auth.Credentials)3 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)3 ApiClock (com.google.api.core.ApiClock)2 CredentialsProvider (com.google.api.gax.core.CredentialsProvider)2 ExecutorProvider (com.google.api.gax.core.ExecutorProvider)2 FixedCredentialsProvider (com.google.api.gax.core.FixedCredentialsProvider)2 FixedExecutorProvider (com.google.api.gax.core.FixedExecutorProvider)2 IOException (java.io.IOException)2 List (java.util.List)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 Duration (org.threeten.bp.Duration)2 Executor (java.util.concurrent.Executor)1 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1