@Test
public void testFutureCancelInTheMiddle() throws Exception {

  int iterationsCount = 1000;
  String opName = "testCancelInTheMiddle";
  Color resp = getColor(0.5f);
  Currency meta = Currency.getInstance("UAH");
  OperationSnapshot resultOperation = getOperation(opName, null, null, null, false);
  UnaryCallable<Integer, OperationSnapshot> initialCallable =
      mockGetOpSnapshotCallable(StatusCode.Code.OK, resultOperation);
  OperationSnapshot[] pollOperations = new OperationSnapshot[iterationsCount];
  for (int i = 0; i < iterationsCount - 1; i++) {
    pollOperations[i] = getOperation(opName, null, null, null, false);
  }
  pollOperations[iterationsCount - 1] = getOperation(opName, resp, null, meta, true);
  LongRunningClient longRunningClient = mockGetOperation(StatusCode.Code.OK, pollOperations);
  CountDownLatch retryScheduledLatch = new CountDownLatch(10);
  LatchCountDownScheduler scheduler = LatchCountDownScheduler.get(retryScheduledLatch, 0L, 1L);
  ClientContext schedulerContext = getClientContext(initialChannel, scheduler);
  OperationCallable<Integer, Color, Currency> callable =
      FakeCallableFactory.createOperationCallable(
          initialCallable, callSettings, schedulerContext, longRunningClient);
  OperationFuture<Color, Currency> future =
      callable.futureCall(2, FakeCallContext.createDefault());
  CancellationHelpers.cancelInThreadAfterLatchCountDown(future, retryScheduledLatch);
  assertFutureCancelMetaCancel(future);
}