/**
   * Lock provider for batch.
   *
   * @throws Exception if failed
   */
  @Test
  public void batch() throws Exception {
    Map<String, String> conf = new HashMap<>();
    conf.put(BasicLockProvider.KEY_DIRECTORY, folder.getRoot().getAbsolutePath());
    conf.put(ExecutionLockProvider.KEY_SCOPE, ExecutionLock.Scope.BATCH.getSymbol());

    ServiceProfile<ExecutionLockProvider> profile =
        new ServiceProfile<>(
            "testing",
            BasicLockProvider.class,
            conf,
            ProfileContext.system(getClass().getClassLoader()));
    ExecutionLockProvider instance1 = profile.newInstance();
    ExecutionLockProvider instance2 = profile.newInstance();

    try (ExecutionLock lock = instance1.newInstance("batch1")) {
      lock.beginFlow("flow1", "exec1");
      try {
        instance2.newInstance("batch1");
        fail("cannot run same batch");
      } catch (IOException e) {
        // ok.
      }
      try (ExecutionLock other = instance2.newInstance("batch2")) {
        // can acquire any flow/exec lock
        other.beginFlow("flow2", "exec1");
        other.endFlow("flow2", "exec1");
        other.beginFlow("flow1", "exec2");
        other.endFlow("flow1", "exec2");
        other.beginFlow("flow2", "exec2");
        other.endFlow("flow2", "exec2");
      }
    }
  }
 /**
  * Missing directory config.
  *
  * @throws Exception if failed
  */
 @Test(expected = IOException.class)
 public void missing_directory_config() throws Exception {
   Map<String, String> conf = new HashMap<>();
   ServiceProfile<ExecutionLockProvider> profile =
       new ServiceProfile<>(
           "testing",
           BasicLockProvider.class,
           conf,
           ProfileContext.system(getClass().getClassLoader()));
   ExecutionLockProvider instance = profile.newInstance();
   instance.newInstance("batch");
 }
  /**
   * Missing directory config.
   *
   * @throws Exception if failed
   */
  @Test(expected = IOException.class)
  public void invalid_directory() throws Exception {
    File lockDir = folder.newFile("INVALID");

    Map<String, String> conf = new HashMap<>();
    conf.put(BasicLockProvider.KEY_DIRECTORY, lockDir.getAbsolutePath());

    ServiceProfile<ExecutionLockProvider> profile =
        new ServiceProfile<>(
            "testing",
            BasicLockProvider.class,
            conf,
            ProfileContext.system(getClass().getClassLoader()));
    ExecutionLockProvider instance = profile.newInstance();
    instance.newInstance("batch");
  }
 private ExecutionLock acquireExecutionLock(String batchId) throws IOException {
   assert batchId != null;
   if (runtimeContext.canExecute(locks)) {
     return locks.newInstance(batchId);
   } else {
     return ExecutionLock.NULL;
   }
 }
  /**
   * Missing directory config.
   *
   * @throws Exception if failed
   */
  @Test
  public void missing_directory() throws Exception {
    File lockDir = folder.newFolder("missing");
    Assume.assumeThat(lockDir.delete(), is(true));

    Map<String, String> conf = new HashMap<>();
    conf.put(BasicLockProvider.KEY_DIRECTORY, lockDir.getAbsolutePath());

    ServiceProfile<ExecutionLockProvider> profile =
        new ServiceProfile<>(
            "testing",
            BasicLockProvider.class,
            conf,
            ProfileContext.system(getClass().getClassLoader()));
    ExecutionLockProvider instance = profile.newInstance();
    try (ExecutionLock lock = instance.newInstance("batch")) {
      lock.beginFlow("a", "b");
      lock.endFlow("a", "b");
    }
  }
  /**
   * Simple testing.
   *
   * @throws Exception if failed
   */
  @Test
  public void simple() throws Exception {
    File lockDir = folder.getRoot();
    int start = lockDir.list().length;

    Map<String, String> conf = new HashMap<>();
    conf.put(BasicLockProvider.KEY_DIRECTORY, lockDir.getAbsolutePath());

    ServiceProfile<ExecutionLockProvider> profile =
        new ServiceProfile<>(
            "testing",
            BasicLockProvider.class,
            conf,
            ProfileContext.system(getClass().getClassLoader()));
    ExecutionLockProvider instance = profile.newInstance();
    try (ExecutionLock lock = instance.newInstance("batch")) {
      lock.beginFlow("flow", "exec");
      assertThat(lockDir.list().length, is(greaterThan(start)));
    }
    assertThat(lockDir.list().length, is(start));
  }
  /**
   * Configure using variable.
   *
   * @throws Exception if failed
   */
  @Test
  public void with_variable() throws Exception {
    File lockDir = folder.getRoot();
    int start = lockDir.list().length;
    VariableResolver var =
        new VariableResolver(Collections.singletonMap("LOCATION", lockDir.getAbsolutePath()));
    Map<String, String> conf = new HashMap<>();
    conf.put(BasicLockProvider.KEY_DIRECTORY, "${LOCATION}");

    ServiceProfile<ExecutionLockProvider> profile =
        new ServiceProfile<>(
            "testing",
            BasicLockProvider.class,
            conf,
            new ProfileContext(getClass().getClassLoader(), var));
    ExecutionLockProvider instance = profile.newInstance();
    try (ExecutionLock lock = instance.newInstance("batch")) {
      lock.beginFlow("flow", "exec");
      assertThat(lockDir.list().length, is(greaterThan(start)));
    }
    assertThat(lockDir.list().length, is(start));
  }