public void whenNormalOperation(boolean partitionSpecific, boolean remoteCall) {
    BasicBackPressureService service = newEnabledBackPressureService();

    Operation op;
    if (partitionSpecific) {
      PartitionSpecificOperation partitionOp = new PartitionSpecificOperation();
      partitionOp.setPartitionId(0);
      op = partitionOp;
    } else {
      op = new GenericOperation();
      op.setPartitionId(-1);
    }

    Connection connection = null;
    if (remoteCall) {
      connection = mock(Connection.class);
      OperationAccessor.setConnection(op, connection);
    }

    int expected = SYNC_WINDOW - 1;
    for (int k = 0; k < SYNC_WINDOW; k++) {
      boolean result = service.isBackPressureNeeded(op);
      assertFalse("no back pressure expected", result);

      AtomicInteger syncDelay = service.getSyncDelay(connection, op.getPartitionId());
      assertEquals(expected, syncDelay.get());
      expected--;
    }

    boolean result = service.isBackPressureNeeded(op);
    assertTrue("back pressure expected", result);

    AtomicInteger syncDelay = service.getSyncDelay(connection, op.getPartitionId());
    assertValidSyncDelay(syncDelay);
  }
  @Test
  public void cleanup_whenConnectionIsNotAlive() {
    BasicBackPressureService service = newEnabledBackPressureService();
    GenericOperation op = new GenericOperation();

    Connection connection = mock(Connection.class);
    when(connection.isAlive()).thenReturn(false);
    OperationAccessor.setConnection(op, connection);

    service.isBackPressureNeeded(op);

    service.cleanup();

    assertNull(service.getSyncDelays(connection));
  }