Search in sources :

Example 21 with Money

use of com.google.type.Money 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 22 with Money

use of com.google.type.Money in project gax-java by googleapis.

the class GrpcLongRunningTest method testCall.

@Test
public void testCall() {
    Color resp = getColor(1.0f);
    Money meta = getMoney("UAH");
    Operation resultOperation = getOperation("testCall", resp, meta, true);
    mockResponse(channel, Code.OK, resultOperation);
    OperationCallable<Integer, Color, Money> callable = GrpcCallableFactory.createOperationCallable(createGrpcSettings(), callSettings, initialContext, operationsStub);
    Color response = callable.call(2, GrpcCallContext.createDefault());
    assertThat(response).isEqualTo(resp);
    assertThat(executor.getIterationsCount()).isEqualTo(0);
}
Also used : Money(com.google.type.Money) Color(com.google.type.Color) Operation(com.google.longrunning.Operation) Test(org.junit.Test)

Example 23 with Money

use of com.google.type.Money in project gax-java by googleapis.

the class ChannelPoolTest method ensureEvenDistribution.

@Test
public void ensureEvenDistribution() throws InterruptedException, IOException {
    int numChannels = 10;
    final ManagedChannel[] channels = new ManagedChannel[numChannels];
    final AtomicInteger[] counts = new AtomicInteger[numChannels];
    final MethodDescriptor<Color, Money> methodDescriptor = FakeServiceGrpc.METHOD_RECOGNIZE;
    final CallOptions callOptions = CallOptions.DEFAULT;
    @SuppressWarnings("unchecked") final ClientCall<Color, Money> clientCall = Mockito.mock(ClientCall.class);
    for (int i = 0; i < numChannels; i++) {
        final int index = i;
        counts[i] = new AtomicInteger();
        channels[i] = Mockito.mock(ManagedChannel.class);
        Mockito.when(channels[i].newCall(methodDescriptor, callOptions)).thenAnswer((ignored) -> {
            counts[index].incrementAndGet();
            return clientCall;
        });
    }
    final ChannelPool pool = ChannelPool.create(ChannelPoolSettings.staticallySized(numChannels), new FakeChannelFactory(Arrays.asList(channels)));
    int numThreads = 20;
    final int numPerThread = 1000;
    ExecutorService executor = Executors.newFixedThreadPool(numThreads);
    for (int i = 0; i < numThreads; i++) {
        executor.submit(() -> {
            for (int j = 0; j < numPerThread; j++) {
                pool.newCall(methodDescriptor, callOptions);
            }
        });
    }
    executor.shutdown();
    boolean shutdown = executor.awaitTermination(1, TimeUnit.MINUTES);
    assertThat(shutdown).isTrue();
    int expectedCount = (numThreads * numPerThread) / numChannels;
    for (AtomicInteger count : counts) {
        assertThat(count.get()).isAnyOf(expectedCount, expectedCount + 1);
    }
}
Also used : Color(com.google.type.Color) FakeChannelFactory(com.google.api.gax.grpc.testing.FakeChannelFactory) CallOptions(io.grpc.CallOptions) Money(com.google.type.Money) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) ManagedChannel(io.grpc.ManagedChannel) Test(org.junit.Test)

Example 24 with Money

use of com.google.type.Money in project gax-java by googleapis.

the class ChannelPoolTest method verifyTargetChannel.

private void verifyTargetChannel(ChannelPool pool, List<ManagedChannel> channels, ManagedChannel targetChannel) {
    MethodDescriptor<Color, Money> methodDescriptor = FakeServiceGrpc.METHOD_RECOGNIZE;
    CallOptions callOptions = CallOptions.DEFAULT;
    @SuppressWarnings("unchecked") ClientCall<Color, Money> expectedClientCall = Mockito.mock(ClientCall.class);
    channels.forEach(Mockito::reset);
    Mockito.doReturn(expectedClientCall).when(targetChannel).newCall(methodDescriptor, callOptions);
    ClientCall<Color, Money> actualCall = pool.newCall(methodDescriptor, callOptions);
    Mockito.verify(targetChannel, Mockito.times(1)).newCall(methodDescriptor, callOptions);
    actualCall.start(null, null);
    Mockito.verify(expectedClientCall, Mockito.times(1)).start(Mockito.any(), Mockito.any());
    for (ManagedChannel otherChannel : channels) {
        if (otherChannel != targetChannel) {
            Mockito.verify(otherChannel, Mockito.never()).newCall(methodDescriptor, callOptions);
        }
    }
}
Also used : Money(com.google.type.Money) Mockito(org.mockito.Mockito) Color(com.google.type.Color) ManagedChannel(io.grpc.ManagedChannel) CallOptions(io.grpc.CallOptions)

Example 25 with Money

use of com.google.type.Money in project gax-java by googleapis.

the class GrpcClientCallsTest method testTimeoutAfterDeadline.

@Test
public void testTimeoutAfterDeadline() {
    MethodDescriptor<Color, Money> descriptor = FakeServiceGrpc.METHOD_RECOGNIZE;
    @SuppressWarnings("unchecked") ClientCall<Color, Money> mockClientCall = Mockito.mock(ClientCall.class);
    @SuppressWarnings("unchecked") ClientCall.Listener<Money> mockListener = Mockito.mock(ClientCall.Listener.class);
    Channel mockChannel = Mockito.mock(ManagedChannel.class);
    ArgumentCaptor<CallOptions> capturedCallOptions = ArgumentCaptor.forClass(CallOptions.class);
    Mockito.when(mockChannel.newCall(Mockito.eq(descriptor), capturedCallOptions.capture())).thenReturn(mockClientCall);
    // Configure a timeout that occurs after the grpc deadline
    Deadline priorDeadline = Deadline.after(5, TimeUnit.SECONDS);
    Duration timeout = Duration.ofSeconds(10);
    GrpcCallContext context = GrpcCallContext.createDefault().withChannel(mockChannel).withCallOptions(CallOptions.DEFAULT.withDeadline(priorDeadline)).withTimeout(timeout);
    GrpcClientCalls.newCall(descriptor, context).start(mockListener, new Metadata());
    // Verify that the timeout is ignored
    Truth.assertThat(capturedCallOptions.getValue().getDeadline()).isEqualTo(priorDeadline);
}
Also used : Color(com.google.type.Color) ManagedChannel(io.grpc.ManagedChannel) Channel(io.grpc.Channel) Deadline(io.grpc.Deadline) Metadata(io.grpc.Metadata) Duration(org.threeten.bp.Duration) CallOptions(io.grpc.CallOptions) Money(com.google.type.Money) ClientCall(io.grpc.ClientCall) Test(org.junit.Test)

Aggregations

Money (com.google.type.Money)35 Test (org.junit.Test)28 Color (com.google.type.Color)19 OperationSnapshot (com.google.api.gax.longrunning.OperationSnapshot)13 ManagedChannel (io.grpc.ManagedChannel)8 UnavailableException (com.google.api.gax.rpc.UnavailableException)6 UnknownException (com.google.api.gax.rpc.UnknownException)6 Status (com.google.rpc.Status)6 Metadata (io.grpc.Metadata)6 CallOptions (io.grpc.CallOptions)5 Channel (io.grpc.Channel)5 ClientCall (io.grpc.ClientCall)4 DataLabelingJob (com.google.cloud.aiplatform.v1.DataLabelingJob)3 DatasetName (com.google.cloud.aiplatform.v1.DatasetName)3 JobServiceClient (com.google.cloud.aiplatform.v1.JobServiceClient)3 JobServiceSettings (com.google.cloud.aiplatform.v1.JobServiceSettings)3 LocationName (com.google.cloud.aiplatform.v1.LocationName)3 Value (com.google.protobuf.Value)3 Deadline (io.grpc.Deadline)3 StatusRuntimeException (io.grpc.StatusRuntimeException)3