From 048aa2abe5cb4dc94938175835eae5c6ba3390b6 Mon Sep 17 00:00:00 2001 From: dotasek Date: Wed, 18 Sep 2024 16:07:00 -0400 Subject: [PATCH] More testing --- .../FilesystemPackageManagerLockTests.java | 19 +++++++++++++++ .../npm/FilesystemPackageManagerTests.java | 23 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/FilesystemPackageManagerLockTests.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/FilesystemPackageManagerLockTests.java index e20ecd747..34bde4ce8 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/FilesystemPackageManagerLockTests.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/FilesystemPackageManagerLockTests.java @@ -1,6 +1,7 @@ package org.hl7.fhir.utilities.npm; import org.hl7.fhir.utilities.filesystem.ManagedFileAccess; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -16,6 +17,7 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertThrows; public class FilesystemPackageManagerLockTests { @@ -116,6 +118,23 @@ public class FilesystemPackageManagerLockTests { } } + @Test void testWhenLockIsntHeld_canLockFileBeHeldByThisProcessIsTrue() throws IOException { + File lockFile = getPackageLockFile(); + lockFile.createNewFile(); + Assertions.assertTrue(filesystemPackageCacheLockManager.getCacheLock().canLockFileBeHeldByThisProcess(lockFile)); + } + + @Test void testWhenLockIsHelp_canLockFileBeHeldByThisProcessIsFalse() throws IOException, InterruptedException, TimeoutException { + File lockFile = getPackageLockFile(); + Thread lockThread = LockfileTestUtility.lockWaitAndDeleteInNewProcess(cachePath, DUMMY_PACKAGE + ".lock", 2); + + LockfileTestUtility.waitForLockfileCreation(cacheDirectory.getAbsolutePath(), DUMMY_PACKAGE + ".lock"); + + Assertions.assertFalse(filesystemPackageCacheLockManager.getCacheLock().canLockFileBeHeldByThisProcess(lockFile)); + + lockThread.join(); + } + @Test void testSinglePackageWriteMultiPackageRead() throws IOException { final FilesystemPackageCacheManagerLocks.PackageLock packageLock = filesystemPackageCacheLockManager.getPackageLock(DUMMY_PACKAGE); AtomicInteger writeCounter = new AtomicInteger(0); diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/FilesystemPackageManagerTests.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/FilesystemPackageManagerTests.java index d7565a348..dea38be40 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/FilesystemPackageManagerTests.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/npm/FilesystemPackageManagerTests.java @@ -134,6 +134,29 @@ public class FilesystemPackageManagerTests { assertThat(dummyLockFile).doesNotExist(); } + @Test + public void testLockedPackageIsntCleanedUp() throws IOException, InterruptedException, TimeoutException { + File cacheDirectory = ManagedFileAccess.fromPath(Files.createTempDirectory("fpcm-multithreadingTest")); + + File dummyPackage = createDummyPackage(cacheDirectory, "example.fhir.uv.myig", "1.2.3"); + + Thread lockThread = LockfileTestUtility.lockWaitAndDeleteInNewProcess(cacheDirectory.getAbsolutePath(), "example.fhir.uv.myig#1.2.3.lock", 2); + + LockfileTestUtility.waitForLockfileCreation(cacheDirectory.getAbsolutePath(), "example.fhir.uv.myig#1.2.3.lock"); + File dummyLockFile = ManagedFileAccess.file(cacheDirectory.getAbsolutePath(), "example.fhir.uv.myig#1.2.3.lock"); + + assertThat(dummyPackage).isDirectory(); + assertThat(dummyPackage).exists(); + assertThat(dummyLockFile).exists(); + + FilesystemPackageCacheManager filesystemPackageCacheManager = new FilesystemPackageCacheManager.Builder().withCacheFolder(cacheDirectory.getAbsolutePath()).build(); + + assertThat(dummyPackage).exists(); + assertThat(dummyLockFile).exists(); + + lockThread.join(); + } + @Test public void testTimeoutForLockedPackageRead() throws IOException, InterruptedException, TimeoutException { String pcmPath = ManagedFileAccess.fromPath(Files.createTempDirectory("fpcm-multithreadingTest")).getAbsolutePath();