public int terminateRunningInstances(
      String workflowId,
      int limit,
      Actions.WorkflowInstanceAction action,
      User caller,
      String reason) {
    return Stream.of(GET_RUNNING_CREATED_INSTANCES_QUERY, GET_RUNNING_IN_PROGRESS_INSTANCES_QUERY)
        .mapToInt(
            sql -> {
              AtomicLong lastInstanceId = new AtomicLong(0L);
              int stoppedRunning = 0;
              int stopped = limit;
              while (stopped == limit) {
                TerminateInstancesJobEvent jobEvent =
                    TerminateInstancesJobEvent.init(workflowId, action, caller, reason);
                withRetryableQuery(
                    sql,
                    wfiStmt -> {
                      int idx = 0;
                      wfiStmt.setString(++idx, workflowId);
                      wfiStmt.setLong(++idx, lastInstanceId.get());
                      wfiStmt.setInt(++idx, limit);
                    },
                    result -> {
                      while (result.next()) {
                        jobEvent.addOneRun(
                            result.getLong(INSTANCE_ID_COLUMN),
                            result.getLong("run_id"),
                            result.getString("uuid"));
                      }
                      return null;
                    });
                stopped = jobEvent.size();
                stoppedRunning += stopped;
                if (stopped > 0) {
                  publisher.publishOrThrow(
                      jobEvent, "Failed to send terminate job event for workflow " + workflowId);
                  lastInstanceId.set(
                      jobEvent.getInstanceRunUuids().get(stopped - 1).getInstanceId());
                }
              }
              return stoppedRunning;
            })
        .sum();
  }
@Test
  public void testTerminateRunningInstancesNoOp() {
    int cnt =
        instanceDao.terminateRunningInstances(
            TEST_WORKFLOW_ID, 2, Actions.WorkflowInstanceAction.STOP, TEST_CALLER, "test-reason");
    assertEquals(0, cnt);
    verify(publisher, times(0)).publishOrThrow(any(), any());
  }