use of com.google.type.Money in project gax-java by googleapis.
the class GrpcDirectServerStreamingCallableTest method setUp.
@Before
public void setUp() throws InstantiationException, IllegalAccessException, IOException {
String serverName = "fakeservice";
FakeServiceImpl serviceImpl = new FakeServiceImpl();
inprocessServer = new InProcessServer<>(serviceImpl, serverName);
inprocessServer.start();
channel = InProcessChannelBuilder.forName(serverName).directExecutor().usePlaintext().build();
clientContext = ClientContext.newBuilder().setTransportChannel(GrpcTransportChannel.create(channel)).setDefaultCallContext(GrpcCallContext.of(channel, CallOptions.DEFAULT)).build();
streamingCallSettings = ServerStreamingCallSettings.<Color, Money>newBuilder().build();
streamingCallable = GrpcCallableFactory.createServerStreamingCallable(GrpcCallSettings.create(METHOD_SERVER_STREAMING_RECOGNIZE), streamingCallSettings, clientContext);
}
use of com.google.type.Money in project gax-java by googleapis.
the class GrpcDirectServerStreamingCallableTest method testBlockingServerStreaming.
@Test
public void testBlockingServerStreaming() {
Color request = Color.newBuilder().setRed(0.5f).build();
ServerStream<Money> response = streamingCallable.call(request);
List<Money> responseData = Lists.newArrayList(response);
Money expected = Money.newBuilder().setCurrencyCode("USD").setUnits(127).build();
Truth.assertThat(responseData).containsExactly(expected);
}
use of com.google.type.Money in project gax-java by googleapis.
the class GrpcDirectServerStreamingCallableTest method testObserverErrorCancelsCall.
@Test
public void testObserverErrorCancelsCall() throws Throwable {
final RuntimeException expectedCause = new RuntimeException("some error");
final SettableApiFuture<Throwable> actualErrorF = SettableApiFuture.create();
ResponseObserver<Money> moneyObserver = new StateCheckingResponseObserver<Money>() {
@Override
protected void onStartImpl(StreamController controller) {
}
@Override
protected void onResponseImpl(Money response) {
throw expectedCause;
}
@Override
protected void onErrorImpl(Throwable t) {
actualErrorF.set(t);
}
@Override
protected void onCompleteImpl() {
actualErrorF.set(null);
}
};
streamingCallable.call(DEFAULT_REQUEST, moneyObserver);
Throwable actualError = actualErrorF.get(500, TimeUnit.MILLISECONDS);
Truth.assertThat(actualError).isInstanceOf(ApiException.class);
Truth.assertThat(((ApiException) actualError).getStatusCode().getCode()).isEqualTo(StatusCode.Code.CANCELLED);
// grpc is responsible for the immediate cancellation
Truth.assertThat(actualError.getCause()).isInstanceOf(StatusRuntimeException.class);
// and the client error is cause for grpc to cancel it
Truth.assertThat(actualError.getCause().getCause()).isSameInstanceAs(expectedCause);
}
use of com.google.type.Money in project gax-java by googleapis.
the class GrpcDirectStreamingCallableTest method testBidiStreamingClientError.
@Test
public void testBidiStreamingClientError() throws Exception {
BidiStreamingCallable<Color, Money> streamingCallable = GrpcCallableFactory.createBidiStreamingCallable(GrpcCallSettings.create(METHOD_STREAMING_RECOGNIZE_ERROR), null, clientContext);
CountDownLatch latch = new CountDownLatch(1);
GrpcDirectServerStreamingCallableTest.MoneyObserver moneyObserver = new GrpcDirectServerStreamingCallableTest.MoneyObserver(true, latch);
ClientStream<Color> stream = streamingCallable.splitCall(moneyObserver);
Throwable clientError = new StatusRuntimeException(Status.CANCELLED);
stream.closeSendWithError(clientError);
latch.await(20, TimeUnit.SECONDS);
assertThat(moneyObserver.error).isNotNull();
assertThat(moneyObserver.error).isInstanceOf(ApiException.class);
assertThat(((ApiException) moneyObserver.error).getStatusCode().getCode()).isEqualTo(Code.CANCELLED);
assertThat(moneyObserver.response).isNull();
// As of gRPC 1.8, when the client closes, the server gRPC issues
// io.grpc.StatusRuntimeException: CANCELLED: cancelled before receiving half close
// to the server application, and our error is not propagated.
// We don't check the error received by the server; we can't round-trip it.
}
use of com.google.type.Money in project gax-java by googleapis.
the class GrpcLongRunningTest method testFutureCallPollDoneOnFirst.
@Test
public void testFutureCallPollDoneOnFirst() throws Exception {
String opName = "testFutureCallPollDoneOnFirst";
Color resp = getColor(0.5f);
Money meta = getMoney("UAH");
Operation initialOperation = getOperation(opName, null, null, false);
Operation resultOperation = getOperation(opName, resp, meta, true);
mockResponse(channel, Code.OK, initialOperation, resultOperation);
OperationCallable<Integer, Color, Money> callable = GrpcCallableFactory.createOperationCallable(createGrpcSettings(), callSettings, initialContext, operationsStub);
OperationFuture<Color, Money> future = callable.futureCall(2);
assertFutureSuccessMetaSuccess(opName, future, resp, meta);
assertThat(executor.getIterationsCount()).isEqualTo(0);
}
Aggregations