HDFSSynchronization implements ExternalSynchronization { @Override public boolean tryAcquireJobLock(Configuration conf) { Path path = new Path(locksDir, String.format(LOCKS_DIR_JOB_FILENAME, getJobJtIdentifier(conf))); return tryCreateFile(conf, path); } HDFSSynchronization(String locksDir);   HDFSSynchronization(
      String locksDir, ThrowingFunction<Configuration, FileSystem, IOException> fileSystemFactory); @Override boolean tryAcquireJobLock(Configuration conf); @Override void releaseJobIdLock(Configuration conf); @Override TaskID acquireTaskIdLock(Configuration conf); @Override TaskAttemptID acquireTaskAttemptIdLock(Configuration conf, int taskId);  }
@Test public void tryAcquireJobLockTest() { boolean firstAttempt = tested.tryAcquireJobLock(configuration); boolean secondAttempt = tested.tryAcquireJobLock(configuration); boolean thirdAttempt = tested.tryAcquireJobLock(configuration); assertTrue(isFileExists(getJobLockPath())); tested.releaseJobIdLock(configuration); boolean fourthAttempt = tested.tryAcquireJobLock(configuration); boolean fifthAttempt = tested.tryAcquireJobLock(configuration); assertTrue(firstAttempt); assertFalse(secondAttempt); assertFalse(thirdAttempt); assertTrue(fourthAttempt); assertFalse(fifthAttempt); }
@Test(expected = NullPointerException.class) public void testMissingJobId() { Configuration conf = new Configuration(); tested.tryAcquireJobLock(conf); }
@Test public void testCatchingRemoteException() throws IOException { FileSystem mockedFileSystem = Mockito.mock(FileSystem.class); RemoteException thrownException = new RemoteException(AlreadyBeingCreatedException.class.getName(), "Failed to CREATE_FILE"); Mockito.when(mockedFileSystem.createNewFile(Mockito.any())).thenThrow(thrownException); HDFSSynchronization synchronization = new HDFSSynchronization("someDir", (conf) -> mockedFileSystem); assertFalse(synchronization.tryAcquireJobLock(configuration)); }