Search in sources :

Example 1 with FakeChannel

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);
}
Also used : Color(java.awt.Color) Currency(java.util.Currency) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) FakeOperationSnapshot(com.google.api.gax.rpc.testing.FakeOperationSnapshot) FakeChannel(com.google.api.gax.rpc.testing.FakeChannel) Test(org.junit.Test)

Example 2 with FakeChannel

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);
}
Also used : FakeCallContext(com.google.api.gax.rpc.testing.FakeCallContext) OperationStashCallable(com.google.api.gax.rpc.testing.FakeOperationApi.OperationStashCallable) Credentials(com.google.auth.Credentials) FakeChannel(com.google.api.gax.rpc.testing.FakeChannel) Test(org.junit.Test)

Example 3 with FakeChannel

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();
}
Also used : FakeApiClock(com.google.api.gax.core.FakeApiClock) FakeChannel(com.google.api.gax.rpc.testing.FakeChannel) Before(org.junit.Before)

Example 4 with FakeChannel

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);
    }
}
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 5 with FakeChannel

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

Aggregations

FakeChannel (com.google.api.gax.rpc.testing.FakeChannel)28 Test (org.junit.Test)24 FakeTransportChannel (com.google.api.gax.rpc.testing.FakeTransportChannel)12 Credentials (com.google.auth.Credentials)12 FakeClientSettings (com.google.api.gax.rpc.testing.FakeClientSettings)11 FakeCallContext (com.google.api.gax.rpc.testing.FakeCallContext)9 BackgroundResource (com.google.api.gax.core.BackgroundResource)5 ServerStreamingStashCallable (com.google.api.gax.rpc.testing.FakeStreamingApi.ServerStreamingStashCallable)4 OperationStashCallable (com.google.api.gax.rpc.testing.FakeOperationApi.OperationStashCallable)3 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)3 List (java.util.List)3 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)3 Before (org.junit.Before)3 ApiClock (com.google.api.core.ApiClock)2 CredentialsProvider (com.google.api.gax.core.CredentialsProvider)2 ExecutorProvider (com.google.api.gax.core.ExecutorProvider)2 FakeApiClock (com.google.api.gax.core.FakeApiClock)2 FixedCredentialsProvider (com.google.api.gax.core.FixedCredentialsProvider)2 FixedExecutorProvider (com.google.api.gax.core.FixedExecutorProvider)2 RetrySettings (com.google.api.gax.retrying.RetrySettings)2