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