Move file exists logic into onStart + move file delete before unlock

This commit is contained in:
dotasek 2024-09-18 19:23:00 -04:00
parent b30134abfc
commit 0fd25a6a98
3 changed files with 26 additions and 30 deletions

View File

@ -261,11 +261,12 @@ public class FilesystemPackageCacheManagerLocks {
try {
result = function.get();
} finally {
fileLock.release();
channel.close();
if (!lockFile.delete()) {
lockFile.deleteOnExit();
}
fileLock.release();
channel.close();
lock.writeLock().unlock();
cacheLock.getLock().writeLock().unlock();
}

View File

@ -218,17 +218,6 @@ public class FilesystemPackageManagerLockTests {
lockThread.join();
}
@Test
public void apacheFileAlterationMonitorTest() {
// Use Apache FileAlterationMonitor to monitor the cache directory for file deletions
// and create a lock file in a separate thread
// Create a lock file in a separate thread
}
@Test
public void testReadWhenLockFileIsDeleted() throws InterruptedException, TimeoutException, IOException {

View File

@ -29,14 +29,20 @@ public class LockfileTestUtility {
* @throws TimeoutException If the lock file is not created within 10 seconds
*/
public static void waitForLockfileCreation(String path, String lockFileName) throws InterruptedException, TimeoutException {
if (Files.exists(Paths.get(path, lockFileName))) {
return;
}
CountDownLatch latch = new CountDownLatch(1);
FileAlterationMonitor monitor = new FileAlterationMonitor(100);
FileAlterationObserver observer = new FileAlterationObserver(path);
observer.addListener(new FileAlterationListenerAdaptor(){
@Override
public void onStart(FileAlterationObserver observer) {
if (Files.exists(Paths.get(path, lockFileName))) {
latch.countDown();
}
}
@Override
public void onFileCreate(File file) {
System.out.println("File created: " + file.getName());
@ -45,21 +51,21 @@ public class LockfileTestUtility {
});
monitor.addObserver(observer);
try {
monitor.start();
} catch (Exception e) {
throw new RuntimeException(e);
}
latch.await(10, TimeUnit.SECONDS);
try {
monitor.stop();
} catch (Exception e) {
throw new RuntimeException(e);
}
try {
monitor.start();
} catch (Exception e) {
throw new RuntimeException(e);
}
boolean success = latch.await(10, TimeUnit.SECONDS);
try {
monitor.stop();
} catch (Exception e) {
throw new RuntimeException(e);
}
if (!Files.exists(Paths.get(path, lockFileName))) {
throw new TimeoutException("Lock file not created within 10 seconds");
}
if (!success) {
throw new TimeoutException("Timed out waiting for lock file creation: " + lockFileName);
}
}