Search in sources :

Example 31 with OperationSnapshot

use of com.google.api.gax.longrunning.OperationSnapshot in project gax-java by googleapis.

the class GrpcLongRunningTest method setUp.

@Before
public void setUp() throws IOException {
    channel = mock(ManagedChannel.class);
    TransportChannelProvider operationsChannelProvider = mock(TransportChannelProvider.class);
    TransportChannel transportChannel = GrpcTransportChannel.newBuilder().setManagedChannel(channel).build();
    when(operationsChannelProvider.getTransportChannel()).thenReturn(transportChannel);
    clock = new FakeApiClock(0L);
    executor = RecordingScheduler.create(clock);
    pollingAlgorithm = OperationTimedPollAlgorithm.create(FAST_RETRY_SETTINGS, clock);
    OperationsSettings.Builder settingsBuilder = OperationsSettings.newBuilder();
    settingsBuilder.getOperationSettings().setRetrySettings(FAST_RETRY_SETTINGS.toBuilder().setMaxAttempts(1).build());
    OperationsSettings settings = OperationsSettings.newBuilder().setTransportChannelProvider(operationsChannelProvider).build();
    operationsStub = GrpcOperationsStub.create(((OperationsStubSettings) settings.getStubSettings()));
    UnaryCallSettings<Integer, OperationSnapshot> initialCallSettings = UnaryCallSettings.<Integer, OperationSnapshot>newUnaryCallSettingsBuilder().setRetrySettings(FAST_RETRY_SETTINGS.toBuilder().setMaxAttempts(1).build()).build();
    callSettings = OperationCallSettings.<Integer, Color, Money>newBuilder().setInitialCallSettings(initialCallSettings).setResponseTransformer(ProtoOperationTransformers.ResponseTransformer.create(Color.class)).setMetadataTransformer(ProtoOperationTransformers.MetadataTransformer.create(Money.class)).setPollingAlgorithm(pollingAlgorithm).build();
    initialContext = ClientContext.newBuilder().setTransportChannel(GrpcTransportChannel.newBuilder().setManagedChannel(channel).build()).setExecutor(executor).setDefaultCallContext(GrpcCallContext.of(channel, CallOptions.DEFAULT)).setClock(clock).build();
}
Also used : Money(com.google.type.Money) Color(com.google.type.Color) ManagedChannel(io.grpc.ManagedChannel) FakeApiClock(com.google.api.gax.core.FakeApiClock) TransportChannel(com.google.api.gax.rpc.TransportChannel) TransportChannelProvider(com.google.api.gax.rpc.TransportChannelProvider) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) OperationsSettings(com.google.longrunning.OperationsSettings) Before(org.junit.Before)

Example 32 with OperationSnapshot

use of com.google.api.gax.longrunning.OperationSnapshot in project gax-java by googleapis.

the class GrpcCallableFactory method createOperationCallable.

/**
 * Creates a callable object that represents a long-running operation. Designed for use by
 * generated code.
 *
 * @param grpcCallSettings the gRPC call settings
 * @param operationCallSettings {@link OperationCallSettings} to configure the method-level
 *     settings with.
 * @param clientContext {@link ClientContext} to use to connect to the service.
 * @param operationsStub {@link OperationsStub} to use to poll for updates on the Operation.
 * @return {@link com.google.api.gax.rpc.OperationCallable} callable object.
 */
public static <RequestT, ResponseT, MetadataT> OperationCallable<RequestT, ResponseT, MetadataT> createOperationCallable(GrpcCallSettings<RequestT, Operation> grpcCallSettings, OperationCallSettings<RequestT, ResponseT, MetadataT> operationCallSettings, ClientContext clientContext, OperationsStub operationsStub) {
    SpanName initialSpanName = getSpanName(grpcCallSettings.getMethodDescriptor());
    SpanName operationSpanName = SpanName.of(initialSpanName.getClientName(), initialSpanName.getMethodName() + "Operation");
    UnaryCallable<RequestT, Operation> initialGrpcCallable = createBaseUnaryCallable(grpcCallSettings, operationCallSettings.getInitialCallSettings(), clientContext);
    UnaryCallable<RequestT, OperationSnapshot> initialCallable = new GrpcOperationSnapshotCallable<>(initialGrpcCallable);
    // Create a sub-trace for the initial RPC that starts the operation.
    UnaryCallable<RequestT, OperationSnapshot> tracedInitialCallable = new TracedOperationInitialCallable<>(initialCallable, clientContext.getTracerFactory(), initialSpanName);
    LongRunningClient longRunningClient = new GrpcLongRunningClient(operationsStub);
    OperationCallable<RequestT, ResponseT, MetadataT> operationCallable = Callables.longRunningOperation(tracedInitialCallable, operationCallSettings, clientContext, longRunningClient);
    OperationCallable<RequestT, ResponseT, MetadataT> tracedOperationCallable = new TracedOperationCallable<>(operationCallable, clientContext.getTracerFactory(), operationSpanName);
    return tracedOperationCallable.withDefaultCallContext(clientContext.getDefaultCallContext());
}
Also used : TracedOperationInitialCallable(com.google.api.gax.tracing.TracedOperationInitialCallable) SpanName(com.google.api.gax.tracing.SpanName) Operation(com.google.longrunning.Operation) TracedOperationCallable(com.google.api.gax.tracing.TracedOperationCallable) LongRunningClient(com.google.api.gax.rpc.LongRunningClient) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot)

Example 33 with OperationSnapshot

use of com.google.api.gax.longrunning.OperationSnapshot in project gax-java by googleapis.

the class OperationCallableImplTest method testFutureCallContextPropagation.

@Test
public void testFutureCallContextPropagation() throws Exception {
    String opName = "testFutureCallContextPropagation";
    Color resp = getColor(0.5f);
    Currency meta = Currency.getInstance("USD");
    OperationSnapshot initialOperation = getOperation(opName, null, null, null, false);
    OperationSnapshot resultOperation = getOperation(opName, resp, null, meta, true);
    UnaryCallable<Integer, OperationSnapshot> initialCallable = mockGetOpSnapshotCallable(StatusCode.Code.OK, initialOperation);
    LongRunningClient longRunningClient = Mockito.mock(LongRunningClient.class);
    @SuppressWarnings("unchecked") UnaryCallable<String, OperationSnapshot> getOpCallable = Mockito.mock(UnaryCallable.class);
    ArgumentCaptor<ApiCallContext> callContextCaptor = ArgumentCaptor.forClass(ApiCallContext.class);
    Mockito.when(longRunningClient.getOperationCallable()).thenReturn(getOpCallable);
    Mockito.when(getOpCallable.futureCall(Mockito.<String>any(), callContextCaptor.capture())).thenReturn(ApiFutures.immediateFuture(resultOperation));
    OperationCallable<Integer, Color, Currency> callable = FakeCallableFactory.createOperationCallable(initialCallable, callSettings, initialContext, longRunningClient);
    ApiCallContext callContext = FakeCallContext.createDefault().withTimeout(Duration.ofMillis(10));
    callable.futureCall(2, callContext).get(10, TimeUnit.SECONDS);
    assertThat(callContextCaptor.getValue().getTimeout()).isEqualTo(Duration.ofMillis(10));
}
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) Test(org.junit.Test)

Example 34 with OperationSnapshot

use of com.google.api.gax.longrunning.OperationSnapshot in project gax-java by googleapis.

the class OperationCallableImplTest method testFutureCallPollDoneOnSecond.

@Test
public void testFutureCallPollDoneOnSecond() throws Exception {
    String opName = "testFutureCallPollDoneOnSecond";
    Color resp = getColor(0.5f);
    Currency meta1 = Currency.getInstance("UAH");
    Currency meta2 = Currency.getInstance("USD");
    OperationSnapshot initialOperation = getOperation(opName, null, null, null, false);
    OperationSnapshot resultOperation1 = getOperation(opName, null, null, meta1, false);
    OperationSnapshot resultOperation2 = getOperation(opName, resp, null, meta2, true);
    UnaryCallable<Integer, OperationSnapshot> initialCallable = mockGetOpSnapshotCallable(StatusCode.Code.OK, initialOperation);
    LongRunningClient longRunningClient = mockGetOperation(StatusCode.Code.OK, resultOperation1, resultOperation2);
    OperationCallable<Integer, Color, Currency> callable = FakeCallableFactory.createOperationCallable(initialCallable, callSettings, initialContext, longRunningClient);
    OperationFuture<Color, Currency> future = callable.futureCall(2, FakeCallContext.createDefault());
    assertFutureSuccessMetaSuccess(opName, future, resp, meta2);
    assertThat(executor.getIterationsCount()).isEqualTo(1);
}
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) Test(org.junit.Test)

Example 35 with OperationSnapshot

use of com.google.api.gax.longrunning.OperationSnapshot in project gax-java by googleapis.

the class OperationCallableImplTest method testFutureCallInitialDoneWithError.

@Test
public void testFutureCallInitialDoneWithError() throws Exception {
    String opName = "testFutureCallInitialDoneWithError";
    StatusCode errorCode = FakeStatusCode.of(StatusCode.Code.ALREADY_EXISTS);
    Currency meta = Currency.getInstance("UAH");
    OperationSnapshot resultOperation = getOperation(opName, null, errorCode, meta, true, "Already exists error");
    UnaryCallable<Integer, OperationSnapshot> initialCallable = mockGetOpSnapshotCallable(StatusCode.Code.OK, resultOperation);
    LongRunningClient longRunningClient = new UnsupportedOperationApi();
    OperationCallable<Integer, Color, Currency> callable = FakeCallableFactory.createOperationCallable(initialCallable, callSettings, initialContext, longRunningClient);
    OperationFuture<Color, Currency> future = callable.futureCall(2, FakeCallContext.createDefault());
    String errorMessage = "Operation with name \"" + opName + "\" failed with status = " + errorCode + " and message = " + "Already exists error";
    assertFutureFailMetaSuccess(future, meta, FakeStatusCode.of(StatusCode.Code.ALREADY_EXISTS), errorMessage);
    assertThat(executor.getIterationsCount()).isEqualTo(0);
}
Also used : Currency(java.util.Currency) Color(java.awt.Color) OperationSnapshot(com.google.api.gax.longrunning.OperationSnapshot) FakeOperationSnapshot(com.google.api.gax.rpc.testing.FakeOperationSnapshot) FakeStatusCode(com.google.api.gax.rpc.testing.FakeStatusCode) Test(org.junit.Test)

Aggregations

OperationSnapshot (com.google.api.gax.longrunning.OperationSnapshot)55 Test (org.junit.Test)44 FakeOperationSnapshot (com.google.api.gax.rpc.testing.FakeOperationSnapshot)28 Color (java.awt.Color)23 Currency (java.util.Currency)23 Money (com.google.type.Money)13 UnavailableException (com.google.api.gax.rpc.UnavailableException)7 UnknownException (com.google.api.gax.rpc.UnknownException)6 Status (com.google.rpc.Status)6 FakeStatusCode (com.google.api.gax.rpc.testing.FakeStatusCode)4 ExecutionException (java.util.concurrent.ExecutionException)4 FakeApiClock (com.google.api.gax.core.FakeApiClock)2 FakeApiMessage (com.google.api.gax.httpjson.testing.FakeApiMessage)2 TimedRetryAlgorithm (com.google.api.gax.retrying.TimedRetryAlgorithm)2 ApiCallContext (com.google.api.gax.rpc.ApiCallContext)2 ApiException (com.google.api.gax.rpc.ApiException)2 FakeChannel (com.google.api.gax.rpc.testing.FakeChannel)2 CancellationException (java.util.concurrent.CancellationException)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 BatchingSettings (com.google.api.gax.batching.BatchingSettings)1