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);
}
}
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);
}
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");
}
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);
}
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");
}
Aggregations