Search in sources :

Example 11 with Color

use of com.google.type.Color 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 12 with Color

use of com.google.type.Color 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 13 with Color

use of com.google.type.Color 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 14 with Color

use of com.google.type.Color 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 15 with Color

use of com.google.type.Color 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

Color (com.google.type.Color)19 Money (com.google.type.Money)19 Test (org.junit.Test)15 ManagedChannel (io.grpc.ManagedChannel)8 Metadata (io.grpc.Metadata)6 CallOptions (io.grpc.CallOptions)5 Channel (io.grpc.Channel)5 ClientCall (io.grpc.ClientCall)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Deadline (io.grpc.Deadline)3 Before (org.junit.Before)3 Duration (org.threeten.bp.Duration)3 FakeChannelFactory (com.google.api.gax.grpc.testing.FakeChannelFactory)2 Operation (com.google.longrunning.Operation)2 StatusRuntimeException (io.grpc.StatusRuntimeException)2 FakeApiClock (com.google.api.gax.core.FakeApiClock)1 FakeServiceImplBase (com.google.api.gax.grpc.testing.FakeServiceGrpc.FakeServiceImplBase)1 FakeServiceImpl (com.google.api.gax.grpc.testing.FakeServiceImpl)1 OperationSnapshot (com.google.api.gax.longrunning.OperationSnapshot)1 TransportChannel (com.google.api.gax.rpc.TransportChannel)1