use of com.google.api.gax.rpc.testing.FakeChannel in project gax-java by googleapis.
the class OperationCallableImplTest method testResumeFutureCall.
@Test
public void testResumeFutureCall() throws Exception {
String opName = "testResumeFutureCall";
Color resp = getColor(0.5f);
Currency meta = Currency.getInstance("UAH");
OperationSnapshot resultOperation = getOperation(opName, resp, null, meta, true);
LongRunningClient longRunningClient = mockGetOperation(StatusCode.Code.OK, resultOperation);
ClientContext mockContext = getClientContext(new FakeChannel(), executor);
OperationCallable<Integer, Color, Currency> callable = FakeCallableFactory.createOperationCallable(getUnexpectedStartCallable(), callSettings, mockContext, longRunningClient);
OperationFuture<Color, Currency> future = callable.resumeFutureCall(opName);
assertFutureSuccessMetaSuccess(opName, future, resp, meta);
assertThat(executor.getIterationsCount()).isEqualTo(0);
}
use of com.google.api.gax.rpc.testing.FakeChannel in project gax-java by googleapis.
the class OperationCallableImplTest method callResumeWithContext.
@Test
public void callResumeWithContext() throws Exception {
FakeChannel channel = new FakeChannel();
Credentials credentials = Mockito.mock(Credentials.class);
ApiCallContext context = FakeCallContext.createDefault().withChannel(channel).withCredentials(credentials);
OperationStashCallable stashCallable = new OperationStashCallable();
OperationCallable<Integer, String, Long> callable = stashCallable.withDefaultCallContext(FakeCallContext.createDefault());
OperationFuture<String, Long> operationFuture = callable.futureCall(45);
String response = callable.resumeFutureCall(operationFuture.getName(), context).get();
Truth.assertThat(response).isEqualTo("45");
FakeCallContext actualContext = (FakeCallContext) stashCallable.getResumeContext();
Truth.assertThat(actualContext.getChannel()).isSameInstanceAs(channel);
Truth.assertThat(actualContext.getCredentials()).isSameInstanceAs(credentials);
}
use of com.google.api.gax.rpc.testing.FakeChannel in project gax-java by googleapis.
the class CancellationTest 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();
}
use of com.google.api.gax.rpc.testing.FakeChannel 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.FakeChannel 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);
}
Aggregations