public int dequeueWithRunStrategy(@NotNull String workflowId, @NotNull RunStrategy runStrategy) {
    return withMetricLogError(
        () -> {
          switch (runStrategy.getRule()) {
            case SEQUENTIAL:
            case PARALLEL:
            case STRICT_SEQUENTIAL:
              return dequeueWorkflowInstances(
                  workflowId,
                  runStrategy.getWorkflowConcurrency(),
                  runStrategy.getRule() == RunStrategy.Rule.STRICT_SEQUENTIAL);
            case FIRST_ONLY:
            case LAST_ONLY:
              return 0; // no queueing support
            default:
              throw new MaestroInternalError(
                  "When dequeue, run strategy [%s] hasn't been implemented yet", runStrategy);
          }
        },
        "runWithStrategy",
        "Failed to run workflow [{}] with run strategy [{}]",
        workflowId,
        runStrategy);
  }
@Test
  public void testDequeueWithRunStrategy() {
    wfi.setWorkflowInstanceId(0L);
    wfi.setWorkflowRunId(0L);
    wfi.setWorkflowUuid("test-uuid");
    int res = runStrategyDao.startWithRunStrategy(wfi, RunStrategy.create("PARALLEL"));
    assertEquals(1, res);
    assertEquals(2, wfi.getWorkflowInstanceId());
    assertEquals(1, wfi.getWorkflowRunId());
    assertEquals("test-uuid", wfi.getWorkflowUuid());
    WorkflowInstance latestRun =
        dao.getLatestWorkflowInstanceRun(wfi.getWorkflowId(), wfi.getWorkflowInstanceId());
    assertEquals(2, latestRun.getWorkflowInstanceId());
    assertEquals("test-uuid", latestRun.getWorkflowUuid());
    verifyPublish(1, 0, 0, 0, 0);

    res = runStrategyDao.dequeueWithRunStrategy(TEST_WORKFLOW_ID, RunStrategy.create("SEQUENTIAL"));
    assertEquals(1, res);
    res = runStrategyDao.dequeueWithRunStrategy(TEST_WORKFLOW_ID, RunStrategy.create("PARALLEL"));
    assertEquals(2, res);
    res =
        runStrategyDao.dequeueWithRunStrategy(
            TEST_WORKFLOW_ID, RunStrategy.create("STRICT_SEQUENTIAL"));
    assertEquals(1, res);

    WorkflowSummary summary = new WorkflowSummary();
    summary.setWorkflowId(TEST_WORKFLOW_ID);
    summary.setWorkflowInstanceId(1L);
    summary.setWorkflowRunId(1L);
    dao.updateWorkflowInstance(summary, null, null, WorkflowInstance.Status.FAILED, 123L);
    res =
        runStrategyDao.dequeueWithRunStrategy(
            TEST_WORKFLOW_ID, RunStrategy.create("STRICT_SEQUENTIAL"));
    assertEquals(0, res);

    res = runStrategyDao.dequeueWithRunStrategy(TEST_WORKFLOW_ID, RunStrategy.create("FIRST_ONLY"));
    assertEquals(0, res);
    res = runStrategyDao.dequeueWithRunStrategy(TEST_WORKFLOW_ID, RunStrategy.create("LAST_ONLY"));
    assertEquals(0, res);

    MaestroTestHelper.removeWorkflowInstance(dataSource, TEST_WORKFLOW_ID, 2);
  }