diff options
author | Matthias Sohn <matthias.sohn@sap.com> | 2023-09-08 22:57:05 +0200 |
---|---|---|
committer | Matthias Sohn <matthias.sohn@sap.com> | 2023-09-12 22:43:15 +0200 |
commit | 642f160236817ff1392ac96085cd5a3f3a96bc1f (patch) | |
tree | 32708d391ba3d2dbbde99134047631987e531f5d /org.eclipse.jgit.junit | |
parent | d4d6c2b5af9b984ae824fb0073e1b368b39b1aa4 (diff) | |
download | jgit-642f160236817ff1392ac96085cd5a3f3a96bc1f.tar.gz jgit-642f160236817ff1392ac96085cd5a3f3a96bc1f.zip |
Use ShutdownHook to gracefully handle JVM shutdown
in all classes which already registered their own shutdown hook
- CloneCommand
- GC#PidLock
- FS#FileStoreAttributes
- LocalDiskRepositoryTestCase#Cleanup
Change-Id: I3efc1f83f3cbbf43eeeaaedcd2bee1ef31971a72
Diffstat (limited to 'org.eclipse.jgit.junit')
-rw-r--r-- | org.eclipse.jgit.junit/META-INF/MANIFEST.MF | 1 | ||||
-rw-r--r-- | org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java | 34 |
2 files changed, 18 insertions, 17 deletions
diff --git a/org.eclipse.jgit.junit/META-INF/MANIFEST.MF b/org.eclipse.jgit.junit/META-INF/MANIFEST.MF index 7f85f0c063..addb737f59 100644 --- a/org.eclipse.jgit.junit/META-INF/MANIFEST.MF +++ b/org.eclipse.jgit.junit/META-INF/MANIFEST.MF @@ -15,6 +15,7 @@ Import-Package: org.eclipse.jgit.annotations;version="[6.8.0,6.9.0)", org.eclipse.jgit.errors;version="[6.8.0,6.9.0)", org.eclipse.jgit.internal.storage.file;version="[6.8.0,6.9.0)", org.eclipse.jgit.internal.storage.pack;version="[6.8.0,6.9.0)", + org.eclipse.jgit.internal.util;version="[6.8.0,6.9.0)", org.eclipse.jgit.lib;version="[6.8.0,6.9.0)", org.eclipse.jgit.merge;version="[6.8.0,6.9.0)", org.eclipse.jgit.revwalk;version="[6.8.0,6.9.0)", diff --git a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java index f816158b10..407290a399 100644 --- a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java +++ b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java @@ -20,7 +20,6 @@ import java.io.File; import java.io.IOException; import java.io.PrintStream; import java.time.Instant; -import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -28,10 +27,12 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeSet; +import java.util.concurrent.ConcurrentHashMap; import org.eclipse.jgit.dircache.DirCache; import org.eclipse.jgit.dircache.DirCacheEntry; import org.eclipse.jgit.internal.storage.file.FileRepository; +import org.eclipse.jgit.internal.util.ShutdownHook; import org.eclipse.jgit.lib.ConfigConstants; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.ObjectId; @@ -114,7 +115,7 @@ public abstract class LocalDiskRepositoryTestCase { @Before public void setUp() throws Exception { tmp = File.createTempFile("jgit_" + getTestName() + '_', "_tmp"); - CleanupThread.deleteOnShutdown(tmp); + Cleanup.deleteOnShutdown(tmp); if (!tmp.delete() || !tmp.mkdir()) { throw new IOException("Cannot create " + tmp); } @@ -222,7 +223,7 @@ public abstract class LocalDiskRepositoryTestCase { recursiveDelete(tmp, false, true); } if (tmp != null && !tmp.exists()) { - CleanupThread.removed(tmp); + Cleanup.removed(tmp); } SystemReader.setInstance(null); } @@ -623,29 +624,28 @@ public abstract class LocalDiskRepositoryTestCase { return new HashMap<>(System.getenv()); } - private static final class CleanupThread extends Thread { - private static final CleanupThread me; + private static final class Cleanup { + private static final Cleanup INSTANCE = new Cleanup(); + static { - me = new CleanupThread(); - Runtime.getRuntime().addShutdownHook(me); + ShutdownHook.INSTANCE.register(() -> INSTANCE.onShutdown()); + } + + private final Set<File> toDelete = ConcurrentHashMap.newKeySet(); + + private Cleanup() { + // empty } static void deleteOnShutdown(File tmp) { - synchronized (me) { - me.toDelete.add(tmp); - } + INSTANCE.toDelete.add(tmp); } static void removed(File tmp) { - synchronized (me) { - me.toDelete.remove(tmp); - } + INSTANCE.toDelete.remove(tmp); } - private final List<File> toDelete = new ArrayList<>(); - - @Override - public void run() { + private void onShutdown() { // On windows accidentally open files or memory // mapped regions may prevent files from being deleted. // Suggesting a GC increases the likelihood that our |