Search in sources :

Example 1 with BackgroundResource

use of com.google.api.gax.core.BackgroundResource 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 BackgroundResource

use of com.google.api.gax.core.BackgroundResource 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 BackgroundResource

use of com.google.api.gax.core.BackgroundResource in project gax-java by googleapis.

the class ClientContextTest method testMergeHeaders_noQuotaProjectIdSet.

@Test
public void testMergeHeaders_noQuotaProjectIdSet() throws IOException {
    final String QUOTA_PROJECT_ID_KEY = "x-goog-user-project";
    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 = Mockito.mock(HeaderProvider.class);
    Mockito.when(headerProvider.getHeaders()).thenReturn(ImmutableMap.of("header_k1", "v1"));
    HeaderProvider internalHeaderProvider = Mockito.mock(HeaderProvider.class);
    Mockito.when(internalHeaderProvider.getHeaders()).thenReturn(ImmutableMap.of("internal_header_k1", "v1"));
    builder.setTransportChannelProvider(transportProvider);
    builder.setCredentialsProvider(FixedCredentialsProvider.create(Mockito.mock(Credentials.class)));
    builder.setHeaderProvider(headerProvider);
    builder.setInternalHeaderProvider(internalHeaderProvider);
    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());
    assertThat(fakeTransportChannel.getHeaders().containsKey(QUOTA_PROJECT_ID_KEY)).isFalse();
}
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 4 with BackgroundResource

use of com.google.api.gax.core.BackgroundResource in project gax-java by googleapis.

the class ClientContext method create.

/**
 * Instantiates the executor, credentials, and transport context based on the given client
 * settings.
 */
public static ClientContext create(StubSettings settings) throws IOException {
    ApiClock clock = settings.getClock();
    ExecutorProvider backgroundExecutorProvider = settings.getBackgroundExecutorProvider();
    final ScheduledExecutorService backgroundExecutor = backgroundExecutorProvider.getExecutor();
    Credentials credentials = settings.getCredentialsProvider().getCredentials();
    if (settings.getQuotaProjectId() != null) {
        // If the quotaProjectId is set, wrap original credentials with correct quotaProjectId as
        // QuotaProjectIdHidingCredentials.
        // Ensure that a custom set quota project id takes priority over one detected by credentials.
        // Avoid the backend receiving possibly conflict values of quotaProjectId
        credentials = new QuotaProjectIdHidingCredentials(credentials);
    }
    TransportChannelProvider transportChannelProvider = settings.getTransportChannelProvider();
    // will have a default executor if it needs one.
    if (transportChannelProvider.needsExecutor() && settings.getExecutorProvider() != null) {
        transportChannelProvider = transportChannelProvider.withExecutor(backgroundExecutor);
    }
    Map<String, String> headers = getHeadersFromSettings(settings);
    if (transportChannelProvider.needsHeaders()) {
        transportChannelProvider = transportChannelProvider.withHeaders(headers);
    }
    if (transportChannelProvider.needsCredentials() && credentials != null) {
        transportChannelProvider = transportChannelProvider.withCredentials(credentials);
    }
    String endpoint = getEndpoint(settings.getEndpoint(), settings.getMtlsEndpoint(), settings.getSwitchToMtlsEndpointAllowed(), new MtlsProvider());
    if (transportChannelProvider.needsEndpoint()) {
        transportChannelProvider = transportChannelProvider.withEndpoint(endpoint);
    }
    TransportChannel transportChannel = transportChannelProvider.getTransportChannel();
    ApiCallContext defaultCallContext = transportChannel.getEmptyCallContext().withTransportChannel(transportChannel);
    if (credentials != null) {
        defaultCallContext = defaultCallContext.withCredentials(credentials);
    }
    WatchdogProvider watchdogProvider = settings.getStreamWatchdogProvider();
    @Nullable Watchdog watchdog = null;
    if (watchdogProvider != null) {
        if (watchdogProvider.needsCheckInterval()) {
            watchdogProvider = watchdogProvider.withCheckInterval(settings.getStreamWatchdogCheckInterval());
        }
        if (watchdogProvider.needsClock()) {
            watchdogProvider = watchdogProvider.withClock(clock);
        }
        if (watchdogProvider.needsExecutor()) {
            watchdogProvider = watchdogProvider.withExecutor(backgroundExecutor);
        }
        watchdog = watchdogProvider.getWatchdog();
    }
    ImmutableList.Builder<BackgroundResource> backgroundResources = ImmutableList.builder();
    if (transportChannelProvider.shouldAutoClose()) {
        backgroundResources.add(transportChannel);
    }
    if (backgroundExecutorProvider.shouldAutoClose()) {
        backgroundResources.add(new ExecutorAsBackgroundResource(backgroundExecutor));
    }
    if (watchdogProvider != null && watchdogProvider.shouldAutoClose()) {
        backgroundResources.add(watchdog);
    }
    return newBuilder().setBackgroundResources(backgroundResources.build()).setExecutor(backgroundExecutor).setCredentials(credentials).setTransportChannel(transportChannel).setHeaders(ImmutableMap.copyOf(settings.getHeaderProvider().getHeaders())).setInternalHeaders(ImmutableMap.copyOf(settings.getInternalHeaderProvider().getHeaders())).setClock(clock).setDefaultCallContext(defaultCallContext).setEndpoint(settings.getEndpoint()).setQuotaProjectId(settings.getQuotaProjectId()).setStreamWatchdog(watchdog).setStreamWatchdogCheckInterval(settings.getStreamWatchdogCheckInterval()).setTracerFactory(settings.getTracerFactory()).build();
}
Also used : ApiClock(com.google.api.core.ApiClock) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ImmutableList(com.google.common.collect.ImmutableList) QuotaProjectIdHidingCredentials(com.google.api.gax.rpc.internal.QuotaProjectIdHidingCredentials) ExecutorAsBackgroundResource(com.google.api.gax.core.ExecutorAsBackgroundResource) BackgroundResource(com.google.api.gax.core.BackgroundResource) ExecutorAsBackgroundResource(com.google.api.gax.core.ExecutorAsBackgroundResource) MtlsProvider(com.google.api.gax.rpc.mtls.MtlsProvider) ExecutorProvider(com.google.api.gax.core.ExecutorProvider) Credentials(com.google.auth.Credentials) QuotaProjectIdHidingCredentials(com.google.api.gax.rpc.internal.QuotaProjectIdHidingCredentials) Nullable(javax.annotation.Nullable)

Example 5 with BackgroundResource

use of com.google.api.gax.core.BackgroundResource in project java-firestore by googleapis.

the class GrpcFirestoreRpc method shutdown.

@Override
public void shutdown() {
    if (closed) {
        return;
    }
    firestoreStub.shutdown();
    for (BackgroundResource resource : clientContext.getBackgroundResources()) {
        resource.shutdown();
    }
    executorFactory.release(executor);
    closed = true;
}
Also used : BackgroundResource(com.google.api.gax.core.BackgroundResource)

Aggregations

BackgroundResource (com.google.api.gax.core.BackgroundResource)11 FakeChannel (com.google.api.gax.rpc.testing.FakeChannel)5 FakeClientSettings (com.google.api.gax.rpc.testing.FakeClientSettings)5 FakeTransportChannel (com.google.api.gax.rpc.testing.FakeTransportChannel)5 Test (org.junit.Test)4 ApiClock (com.google.api.core.ApiClock)3 ExecutorProvider (com.google.api.gax.core.ExecutorProvider)2 Credentials (com.google.auth.Credentials)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 Duration (org.threeten.bp.Duration)2 ExecutorAsBackgroundResource (com.google.api.gax.core.ExecutorAsBackgroundResource)1 FixedExecutorProvider (com.google.api.gax.core.FixedExecutorProvider)1 ClientContext (com.google.api.gax.rpc.ClientContext)1 QuotaProjectIdHidingCredentials (com.google.api.gax.rpc.internal.QuotaProjectIdHidingCredentials)1 MtlsProvider (com.google.api.gax.rpc.mtls.MtlsProvider)1 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)1 ImmutableList (com.google.common.collect.ImmutableList)1 Nullable (javax.annotation.Nullable)1