@Test public void testCall() { Color resp = getColor(1.0f); Currency meta = Currency.getInstance("UAH"); OperationSnapshot resultOperation = getOperation("testCall", resp, null, meta, true); UnaryCallable<Integer, OperationSnapshot> initialCallable = mockGetOpSnapshotCallable(StatusCode.Code.OK, resultOperation); LongRunningClient longRunningClient = new UnsupportedOperationApi(); OperationCallable<Integer, Color, Currency> callable = FakeCallableFactory.createOperationCallable( initialCallable, callSettings, initialContext, longRunningClient); Color response = callable.call(2, FakeCallContext.createDefault()); Truth.assertThat(response).isEqualTo(resp); assertThat(executor.getIterationsCount()).isEqualTo(0); }
@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); }
@Test public void testFutureCallInitialDoneWrongType() throws Exception { String opName = "testFutureCallInitialDoneWrongType"; Currency resp = Currency.getInstance("USD"); Currency meta = Currency.getInstance("UAH"); OperationSnapshot resultOperation = getOperation(opName, resp, null, meta, true); 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()); assertFutureFailMetaSuccess( future, meta, FakeStatusCode.of(StatusCode.Code.OK), "type mismatch: expected java.awt.Color, found java.util.Currency"); assertThat(executor.getIterationsCount()).isEqualTo(0); }
@Test public void testFutureCallInitialDone() throws Exception { String opName = "testFutureCallInitialDone"; Color resp = getColor(0.5f); Currency meta = Currency.getInstance("UAH"); OperationSnapshot resultOperation = getOperation(opName, resp, null, meta, true); 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()); assertFutureSuccessMetaSuccess(opName, future, resp, meta); assertThat(executor.getIterationsCount()).isEqualTo(0); }
@Test public void testResumeFutureCall() throws Exception { String opName = "testResumeFutureCall"; Color resp = getColor(0.5f); Currency meta = Currency.getInstance("UAH"); OperationSnapshot resultOperation = getOperation(opName, resp, null, meta, true); LongRunningClient longRunningClient = mockGetOperation(StatusCode.Code.OK, resultOperation); ClientContext mockContext = getClientContext(new FakeChannel(), executor); OperationCallable<Integer, Color, Currency> callable = FakeCallableFactory.createOperationCallable( getUnexpectedStartCallable(), callSettings, mockContext, longRunningClient); OperationFuture<Color, Currency> future = callable.resumeFutureCall(opName); assertFutureSuccessMetaSuccess(opName, future, resp, meta); assertThat(executor.getIterationsCount()).isEqualTo(0); }
@Test public void testFutureCallInitialError() throws Exception { String opName = "testFutureCallInitialError"; Color resp = getColor(1.0f); Currency meta = Currency.getInstance("UAH"); OperationSnapshot resultOperation = getOperation(opName, resp, null, meta, true); UnaryCallable<Integer, OperationSnapshot> initialCallable = mockGetOpSnapshotCallable(StatusCode.Code.UNAVAILABLE, 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()); assertFutureFailMetaFail(future, null, FakeStatusCode.of(StatusCode.Code.UNAVAILABLE)); assertThat(executor.getIterationsCount()).isEqualTo(0); }
@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); }
@Test public void testFutureCallInitialOperationUnexpectedFail() throws Exception { String opName = "testFutureCallInitialOperationUnexpectedFail"; OperationSnapshot initialOperation = getOperation(opName, null, null, null, false); OperationSnapshot resultOperation = getOperation(opName, null, null, null, false); UnaryCallable<Integer, OperationSnapshot> initialCallable = mockGetOpSnapshotCallable(StatusCode.Code.OK, initialOperation); LongRunningClient longRunningClient = mockGetOperation(StatusCode.Code.OK, resultOperation); OperationCallableImpl<Integer, Color, Currency> callableImpl = Callables.longRunningOperationImpl( initialCallable, callSettings, initialContext, longRunningClient); RuntimeException thrownException = new RuntimeException(); ApiFuture<OperationSnapshot> initialFuture = ApiFutures.immediateFailedFuture(thrownException); OperationFuture<Color, Currency> future = callableImpl.futureCall(initialFuture, FakeCallContext.createDefault()); assertFutureFailMetaFail(future, RuntimeException.class, null); assertThat(executor.getIterationsCount()).isEqualTo(0); }
@Test public void testFutureCallPollDoneOnFirst() throws Exception { String opName = "testFutureCallPollDoneOnFirst"; Color resp = getColor(0.5f); Currency meta = Currency.getInstance("UAH"); 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 = mockGetOperation(StatusCode.Code.OK, resultOperation); OperationCallable<Integer, Color, Currency> callable = FakeCallableFactory.createOperationCallable( initialCallable, callSettings, initialContext, longRunningClient); OperationFuture<Color, Currency> future = callable.futureCall(2, FakeCallContext.createDefault()); assertFutureSuccessMetaSuccess(opName, future, resp, meta); assertThat(executor.getIterationsCount()).isEqualTo(0); }
@Test public void testFutureCallPollError() throws Exception { String opName = "testFutureCallPollError"; Currency meta = Currency.getInstance("UAH"); Color resp = getColor(1.0f); OperationSnapshot initialOperation = getOperation(opName, resp, null, meta, false); UnaryCallable<Integer, OperationSnapshot> initialCallable = mockGetOpSnapshotCallable(StatusCode.Code.OK, initialOperation); OperationSnapshot resultOperation = getOperation(opName, resp, null, meta, false); LongRunningClient longRunningClient = mockGetOperation(StatusCode.Code.ALREADY_EXISTS, resultOperation); OperationCallable<Integer, Color, Currency> callable = FakeCallableFactory.createOperationCallable( initialCallable, callSettings, initialContext, longRunningClient); OperationFuture<Color, Currency> future = callable.futureCall(2, FakeCallContext.createDefault()); assertFutureFailMetaFail(future, null, FakeStatusCode.of(StatusCode.Code.ALREADY_EXISTS)); assertThat(executor.getIterationsCount()).isEqualTo(0); }
@Test public void testFutureCallPollCancelOnTimeoutExceeded() throws Exception { String opName = "testFutureCallPollCancelOnPollingTimeoutExceeded"; OperationSnapshot initialOperation = getOperation(opName, null, null, null, false); OperationSnapshot resultOperation = getOperation(opName, null, null, null, false); UnaryCallable<Integer, OperationSnapshot> initialCallable = mockGetOpSnapshotCallable(StatusCode.Code.OK, initialOperation); LongRunningClient longRunningClient = mockGetOperation(StatusCode.Code.OK, resultOperation); OperationCallable<Integer, Color, Currency> callable = FakeCallableFactory.createOperationCallable( initialCallable, callSettings, initialContext, longRunningClient); OperationFuture<Color, Currency> future = callable.futureCall(2, FakeCallContext.createDefault()); assertFutureCancelMetaCancel(future); assertThat(executor.getIterationsCount()).isEqualTo(5); }
@Test public void testInitialServerSideCancel() throws Exception { String opName = "testInitialServerSideCancel"; StatusCode errorCode = FakeStatusCode.of(StatusCode.Code.CANCELLED); Currency meta = Currency.getInstance("UAH"); OperationSnapshot resultOperation = getOperation(opName, null, errorCode, meta, true); 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 = " + "null"; assertFutureFailMetaSuccess( future, meta, FakeStatusCode.of(StatusCode.Code.CANCELLED), errorMessage); assertThat(executor.getIterationsCount()).isEqualTo(0); }
@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); }
@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); }
@Test public void testFutureCallInitialDoneMetaWrongType() throws Exception { String opName = "testFutureCallInitialDoneMetaWrongType"; Color resp = getColor(1.0f); Color meta = getColor(1.0f); OperationSnapshot resultOperation = getOperation(opName, resp, null, meta, true); 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()); assertFutureSuccessMetaFail(future, resp, FakeStatusCode.of(StatusCode.Code.OK)); assertThat(executor.getIterationsCount()).isEqualTo(0); }
@Test public void testPollServerSideCancel() throws Exception { String opName = "testPollServerSideCancel"; StatusCode errorCode = FakeStatusCode.of(StatusCode.Code.CANCELLED); Currency meta = Currency.getInstance("UAH"); OperationSnapshot initialOperation = getOperation(opName, null, null, meta, false); UnaryCallable<Integer, OperationSnapshot> initialCallable = mockGetOpSnapshotCallable(StatusCode.Code.OK, initialOperation); OperationSnapshot resultOperation1 = getOperation(opName, null, null, null, false); OperationSnapshot resultOperation2 = getOperation(opName, null, errorCode, meta, true); 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()); String errorMessage = "Operation with name \"" + opName + "\" failed with status = " + errorCode + " and message = " + "null"; assertFutureFailMetaSuccess( future, meta, FakeStatusCode.of(StatusCode.Code.CANCELLED), errorMessage); assertThat(executor.getIterationsCount()).isEqualTo(1); }
@Test public void testFutureCallInitialCancel() throws Exception { String opName = "testFutureCallInitialCancel"; OperationSnapshot initialOperation = getOperation(opName, null, null, null, false); OperationSnapshot resultOperation = getOperation(opName, null, null, null, false); UnaryCallable<Integer, OperationSnapshot> initialCallable = mockGetOpSnapshotCallable(StatusCode.Code.OK, initialOperation); LongRunningClient longRunningClient = mockGetOperation(StatusCode.Code.OK, resultOperation); OperationCallableImpl<Integer, Color, Currency> callableImpl = Callables.longRunningOperationImpl( initialCallable, callSettings, initialContext, longRunningClient); OperationFutureImpl<Color, Currency> future = callableImpl.futureCall( new ListenableFutureToApiFuture<>( Futures.<OperationSnapshot>immediateCancelledFuture()), FakeCallContext.createDefault()); Exception exception = null; try { future.get(3, TimeUnit.SECONDS); } catch (CancellationException e) { exception = e; } assertThat(exception).isNotNull(); assertThat(future.isDone()).isTrue(); assertThat(future.isCancelled()).isTrue(); assertThat(future.getInitialFuture().isDone()).isTrue(); assertThat(future.getInitialFuture().isCancelled()).isTrue(); assertFutureCancelMetaCancel(future); assertThat(executor.getIterationsCount()).isEqualTo(0); }
@Test public void testFutureCallPollDoneWithError() throws Exception { String opName = "testFutureCallPollDoneWithError"; Currency meta = Currency.getInstance("UAH"); Color resp = getColor(1.0f); OperationSnapshot initialOperation = getOperation(opName, resp, null, meta, false, "Already exists error"); UnaryCallable<Integer, OperationSnapshot> initialCallable = mockGetOpSnapshotCallable(StatusCode.Code.OK, initialOperation); StatusCode errorCode = FakeStatusCode.of(StatusCode.Code.ALREADY_EXISTS); OperationSnapshot resultOperation = getOperation(opName, null, errorCode, meta, true, "Already exists error"); LongRunningClient longRunningClient = mockGetOperation(StatusCode.Code.OK, resultOperation); 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); }
assertFutureSuccessMetaSuccess(opName, future, resp, meta); assertThat(executor.getIterationsCount()).isEqualTo(iterationsCount - 1);
@Test public void testFutureCallPollCancelOnLongTimeoutExceeded() throws Exception { final int iterationsCount = 1000; String opName = "testFutureCallPollCancelOnLongTimeoutExceeded"; OperationSnapshot initialOperation = getOperation(opName, null, null, null, false); OperationSnapshot[] pollOperations = new OperationSnapshot[iterationsCount]; for (int i = 0; i < iterationsCount; i++) { pollOperations[i] = getOperation(opName, null, null, null, false); } UnaryCallable<Integer, OperationSnapshot> initialCallable = mockGetOpSnapshotCallable(StatusCode.Code.OK, initialOperation); LongRunningClient longRunningClient = mockGetOperation(StatusCode.Code.OK, pollOperations); pollingAlgorithm = OperationTimedPollAlgorithm.create( FAST_RECHECKING_SETTINGS.toBuilder().setTotalTimeout(Duration.ofMillis(1000L)).build(), clock); callSettings = callSettings.toBuilder().setPollingAlgorithm(pollingAlgorithm).build(); OperationCallable<Integer, Color, Currency> callable = FakeCallableFactory.createOperationCallable( initialCallable, callSettings, initialContext, longRunningClient); OperationFuture<Color, Currency> future = callable.futureCall(2, FakeCallContext.createDefault()); assertFutureCancelMetaCancel(future); assertThat(executor.getIterationsCount()).isEqualTo(iterationsCount); }