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