From 642f160236817ff1392ac96085cd5a3f3a96bc1f Mon Sep 17 00:00:00 2001 From: Matthias Sohn Date: Fri, 8 Sep 2023 22:57:05 +0200 Subject: 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 --- org.eclipse.jgit.junit/META-INF/MANIFEST.MF | 1 + .../jgit/junit/LocalDiskRepositoryTestCase.java | 34 +++++++++++----------- 2 files changed, 18 insertions(+), 17 deletions(-) (limited to 'org.eclipse.jgit.junit') 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 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 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 -- cgit v1.2.3