From ec744115a1bf7e32244e2f15bdfc214ddb01452e Mon Sep 17 00:00:00 2001 From: Robin Rosenberg Date: Mon, 1 Dec 2008 00:40:33 +0100 Subject: [PATCH] Cleanup after each test. Automatically clean up any repositories created by the test cases. Cleanup is attempted at the end of each test, but if that fails Shutdown hooks attempt to clean up when the JVM exits. If memmory mapping is enabled (disabled by default in unit tests), gc is invoked to make it more likely that cleanup will occur successfully. The drawback is that this is much slower, which is the reason we disble memory mapping by default in unit tests. Signed-off-by: Robin Rosenberg Signed-off-by: Shawn O. Pearce --- .../org/spearce/jgit/lib/RepositoryTestCase.java | 62 +++++++++++++++++----- 1 file changed, 50 insertions(+), 12 deletions(-) diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java index 8e23bc17..aaa35926 100644 --- a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java +++ b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java @@ -45,6 +45,8 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Reader; +import java.util.ArrayList; +import java.util.List; import junit.framework.TestCase; import org.spearce.jgit.util.JGitTestUtil; @@ -95,8 +97,8 @@ public abstract class RepositoryTestCase extends TestCase { * * @param dir */ - protected static void recursiveDelete(final File dir) { - recursiveDelete(dir, false, null); + protected void recursiveDelete(final File dir) { + recursiveDelete(dir, false, getClass().getName() + "." + getName()); } protected static boolean recursiveDelete(final File dir, boolean silent, @@ -170,9 +172,12 @@ public abstract class RepositoryTestCase extends TestCase { } protected Repository db; - + private static Thread shutdownhook; + private static List shutDownCleanups = new ArrayList(); private static int testcount; + private ArrayList repositoriesToClose = new ArrayList(); + public void setUp() throws Exception { super.setUp(); configure(); @@ -180,14 +185,23 @@ public abstract class RepositoryTestCase extends TestCase { recursiveDelete(trashParent, true, name); trash = new File(trashParent,"trash"+System.currentTimeMillis()+"."+(testcount++)); trash_git = new File(trash, ".git"); - - Runtime.getRuntime().addShutdownHook(new Thread() { - @Override - public void run() { - recursiveDelete(trashParent, false, name); - } - }); - + if (shutdownhook == null) { + shutdownhook = new Thread() { + @Override + public void run() { + // This may look superfluous, but is an extra attempt + // to clean up. First GC to release as many resources + // as possible and then try to clean up one test repo + // at a time (to record problems) and finally to drop + // the directory containing all test repositories. + System.gc(); + for (Runnable r : shutDownCleanups) + r.run(); + recursiveDelete(trashParent, false, null); + } + }; + Runtime.getRuntime().addShutdownHook(shutdownhook); + } db = new Repository(trash_git); db.create(); @@ -213,6 +227,22 @@ public abstract class RepositoryTestCase extends TestCase { protected void tearDown() throws Exception { db.close(); + for (Repository r : repositoriesToClose) + r.close(); + + // Since memory mapping is controlled by the GC we need to + // tell it this is a good time to clean up and unlock + // memory mapped files. + if (packedGitMMAP) + System.gc(); + + final String name = getClass().getName() + "." + getName(); + recursiveDelete(trash, false, name); + for (Repository r : repositoriesToClose) + recursiveDelete(r.getWorkDir(), false, name); + + repositoriesToClose.clear(); + super.tearDown(); } @@ -224,10 +254,18 @@ public abstract class RepositoryTestCase extends TestCase { * @throws IOException */ protected Repository createNewEmptyRepo() throws IOException { - File newTestRepo = new File(trashParent, "new"+System.currentTimeMillis()+"."+(testcount++)+"/.git"); + final File newTestRepo = new File(trashParent, "new" + + System.currentTimeMillis() + "." + (testcount++) + "/.git"); assertFalse(newTestRepo.exists()); final Repository newRepo = new Repository(newTestRepo); newRepo.create(); + final String name = getClass().getName() + "." + getName(); + shutDownCleanups.add(new Runnable() { + public void run() { + recursiveDelete(newTestRepo, false, name); + } + }); + repositoriesToClose.add(newRepo); return newRepo; } -- 2.11.4.GIT