git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [JGIT PATCH 0/4] RepositoryTestCase cleanups
@ 2008-11-27 21:13 Robin Rosenberg
  2008-11-27 21:49 ` Shawn O. Pearce
  0 siblings, 1 reply; 10+ messages in thread
From: Robin Rosenberg @ 2008-11-27 21:13 UTC (permalink / raw)
  To: spearce; +Cc: git, fonseca, Robin Rosenberg

Ok, so here is an attempt to improve the ability of the JGit's unit
tests to delete temporary repositories. This has probably been seen
by many, but Jonas Fonseca raised the issue.

The background is that on Windows you cannot delete files that are
open and mmapped files are open until they get unmapped, which in
Java is beyond explicit programmer control. You can only free the
resources and pray that the GC does the work. Fortunately it usually
does. It turned out our testcases weren't even trying to clean up
properly. 

-- robin

Robin Rosenberg (4):
  Make the cleanup less verbose when it fails to delete temporary
    stuff.
  Add shutdown hooks to try to clean up after unit tests anyway
  Cleanup malformed test cases
  Automatically clean up any repositories created by the test cases

 .../tst/org/spearce/jgit/lib/PackWriterTest.java   |    3 +
 .../org/spearce/jgit/lib/RepositoryTestCase.java   |   82 +++++++++++++++++---
 2 files changed, 73 insertions(+), 12 deletions(-)

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [JGIT PATCH 0/4] RepositoryTestCase cleanups
@ 2008-11-27 21:15 Robin Rosenberg
  2008-11-27 21:15 ` [JGIT PATCH 1/4] Make the cleanup less verbose when it fails to delete temporary stuff Robin Rosenberg
  0 siblings, 1 reply; 10+ messages in thread
From: Robin Rosenberg @ 2008-11-27 21:15 UTC (permalink / raw)
  To: spearce; +Cc: git, fonseca, Robin Rosenberg

Ok, so here is an attempt to improve the ability of the JGit's unit
tests to delete temporary repositories. This has probably been seen
by many, but Jonas Fonseca raised the issue.

The background is that on Windows you cannot delete files that are
open and mmapped files are open until they get unmapped, which in
Java is beyond explicit programmer control. You can only free the
resources and pray that the GC does the work. Fortunately it usually
does. It turned out our testcases weren't even trying to clean up
properly. 

-- robin

Robin Rosenberg (4):
  Make the cleanup less verbose when it fails to delete temporary
    stuff.
  Add shutdown hooks to try to clean up after unit tests anyway
  Cleanup malformed test cases
  Automatically clean up any repositories created by the test cases

 .../tst/org/spearce/jgit/lib/PackWriterTest.java   |    3 +
 .../org/spearce/jgit/lib/RepositoryTestCase.java   |   82 +++++++++++++++++---
 2 files changed, 73 insertions(+), 12 deletions(-)

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [JGIT PATCH 1/4] Make the cleanup less verbose when it fails to delete temporary stuff.
  2008-11-27 21:15 [JGIT PATCH 0/4] RepositoryTestCase cleanups Robin Rosenberg
@ 2008-11-27 21:15 ` Robin Rosenberg
  2008-11-27 21:15   ` [JGIT PATCH 2/4] Add shutdown hooks to try to clean up after unit tests anyway Robin Rosenberg
  0 siblings, 1 reply; 10+ messages in thread
From: Robin Rosenberg @ 2008-11-27 21:15 UTC (permalink / raw)
  To: spearce; +Cc: git, fonseca, Robin Rosenberg

Pass the test case name for easier tracking of the test case that
causes problems.

Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
---
 .../org/spearce/jgit/lib/RepositoryTestCase.java   |   59 ++++++++++++++-----
 1 files changed, 43 insertions(+), 16 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 9d7d133..9c272f6 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
@@ -66,22 +66,43 @@
 		jcommitter = new PersonIdent("J. Committer", "jcommitter@example.com");
 	}
 
-	protected static void recursiveDelete(final File dir) {
+	protected static boolean recursiveDelete(final File dir) {
+		return recursiveDelete(dir, false, null);
+	}
+
+	protected static boolean recursiveDelete(final File dir, boolean silent,
+			final String name) {
+		if (!dir.exists())
+			return silent;
 		final File[] ls = dir.listFiles();
 		if (ls != null) {
 			for (int k = 0; k < ls.length; k++) {
 				final File e = ls[k];
 				if (e.isDirectory()) {
-					recursiveDelete(e);
+					silent = recursiveDelete(e, silent, name);
 				} else {
-					e.delete();
+					if (!e.delete()) {
+						if (!silent) {
+							String msg = "Warning: Failed to delete " + e;
+							if (name != null)
+								msg += " in " + name;
+							System.out.println(msg);
+						}
+						silent = true;
+					}
 				}
 			}
 		}
-		dir.delete();
-		if (dir.exists()) {
-			System.out.println("Warning: Failed to delete " + dir);
+		if (!dir.delete()) {
+			if (!silent) {
+				String msg = "Warning: Failed to delete " + dir;
+				if (name != null)
+					msg += " in " + name;
+				System.out.println(msg);
+			}
+			silent = true;
 		}
+		return silent;
 	}
 
 	protected static void copyFile(final File src, final File dst)
@@ -121,19 +142,25 @@ protected static void checkFile(File f, final String checkData)
 
 	protected Repository db;
 
+	private static int testcount;
+	private static Thread shutdownhook;
+
 	public void setUp() throws Exception {
 		super.setUp();
-		recursiveDelete(trashParent);
-		trash = new File(trashParent,"trash"+System.currentTimeMillis());
+		System.gc();
+		trash = new File(trashParent,"trash"+System.currentTimeMillis()+"."+(testcount++));
+		final String name = getClass().getName() + "." + getName();
+		recursiveDelete(trashParent, true, name);
 		trash_git = new File(trash, ".git");
-
-		Runtime.getRuntime().addShutdownHook(new Thread() {
-			@Override
-			public void run() {
-				recursiveDelete(trashParent);
-			}
-		});
-
+		if (shutdownhook == null) {
+			shutdownhook = new Thread() {
+				@Override
+				public void run() {
+					recursiveDelete(trashParent, false, name);
+				}
+			};
+			Runtime.getRuntime().addShutdownHook(shutdownhook);
+		}
 		db = new Repository(trash_git);
 		db.create();
 
-- 
1.6.0.3.640.g6331a

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [JGIT PATCH 2/4] Add shutdown hooks to try to clean up after unit tests anyway
  2008-11-27 21:15 ` [JGIT PATCH 1/4] Make the cleanup less verbose when it fails to delete temporary stuff Robin Rosenberg
@ 2008-11-27 21:15   ` Robin Rosenberg
  2008-11-27 21:15     ` [JGIT PATCH 3/4] Cleanup malformed test cases Robin Rosenberg
  0 siblings, 1 reply; 10+ messages in thread
From: Robin Rosenberg @ 2008-11-27 21:15 UTC (permalink / raw)
  To: spearce; +Cc: git, fonseca, Robin Rosenberg

Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
---
 .../org/spearce/jgit/lib/RepositoryTestCase.java   |   18 +++++++++++++++---
 1 files changed, 15 insertions(+), 3 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 9c272f6..ef4fd1b 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
@@ -66,8 +66,8 @@
 		jcommitter = new PersonIdent("J. Committer", "jcommitter@example.com");
 	}
 
-	protected static boolean recursiveDelete(final File dir) {
-		return recursiveDelete(dir, false, null);
+	protected boolean recursiveDelete(final File dir) {
+		return recursiveDelete(dir, false, getClass().getName() + "." + getName());
 	}
 
 	protected static boolean recursiveDelete(final File dir, boolean silent,
@@ -161,6 +161,12 @@ public void run() {
 			};
 			Runtime.getRuntime().addShutdownHook(shutdownhook);
 		}
+		Runtime.getRuntime().addShutdownHook(new Thread() {
+			@Override
+			public void run() {
+				recursiveDelete(trash);
+			}
+		});
 		db = new Repository(trash_git);
 		db.create();
 
@@ -197,12 +203,18 @@ protected void tearDown() throws Exception {
 	 * @throws IOException
 	 */
 	protected Repository createNewEmptyRepo() throws IOException {
-		File newTestRepo = new File(trashParent, "new"+System.currentTimeMillis()+"/.git");
+		final File newTestRepo = new File(trashParent, "new"+System.currentTimeMillis()+"/.git");
 		assertFalse(newTestRepo.exists());
 		File unusedDir = new File(trashParent, "tmp"+System.currentTimeMillis());
 		assertTrue(unusedDir.mkdirs());
 		final Repository newRepo = new Repository(newTestRepo);
 		newRepo.create();
+		Runtime.getRuntime().addShutdownHook(new Thread() {
+			@Override
+			public void run() {
+				recursiveDelete(newTestRepo);
+			}
+		});
 		return newRepo;
 	}
 
-- 
1.6.0.3.640.g6331a

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [JGIT PATCH 3/4] Cleanup malformed test cases
  2008-11-27 21:15   ` [JGIT PATCH 2/4] Add shutdown hooks to try to clean up after unit tests anyway Robin Rosenberg
@ 2008-11-27 21:15     ` Robin Rosenberg
  2008-11-27 21:15       ` [JGIT PATCH 4/4] Automatically clean up any repositories created by the " Robin Rosenberg
  0 siblings, 1 reply; 10+ messages in thread
From: Robin Rosenberg @ 2008-11-27 21:15 UTC (permalink / raw)
  To: spearce; +Cc: git, fonseca, Robin Rosenberg

---
 .../tst/org/spearce/jgit/lib/PackWriterTest.java   |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)

diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/PackWriterTest.java b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/PackWriterTest.java
index e5bce4d..3c02955 100644
--- a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/PackWriterTest.java
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/PackWriterTest.java
@@ -309,6 +309,7 @@ public void testWritePack4ThinPack() throws IOException {
 	public void testWritePack2SizeDeltasVsNoDeltas() throws Exception {
 		testWritePack2();
 		final int sizePack2NoDeltas = cos.getCount();
+		tearDown();
 		setUp();
 		testWritePack2DeltasReuseRefs();
 		final int sizePack2DeltasRefs = cos.getCount();
@@ -327,6 +328,7 @@ public void testWritePack2SizeDeltasVsNoDeltas() throws Exception {
 	public void testWritePack2SizeOffsetsVsRefs() throws Exception {
 		testWritePack2DeltasReuseRefs();
 		final int sizePack2DeltasRefs = cos.getCount();
+		tearDown();
 		setUp();
 		testWritePack2DeltasReuseOffsets();
 		final int sizePack2DeltasOffsets = cos.getCount();
@@ -344,6 +346,7 @@ public void testWritePack2SizeOffsetsVsRefs() throws Exception {
 	public void testWritePack4SizeThinVsNoThin() throws Exception {
 		testWritePack4();
 		final int sizePack4 = cos.getCount();
+		tearDown();
 		setUp();
 		testWritePack4ThinPack();
 		final int sizePack4Thin = cos.getCount();
-- 
1.6.0.3.640.g6331a

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [JGIT PATCH 4/4] Automatically clean up any repositories created by the test cases
  2008-11-27 21:15     ` [JGIT PATCH 3/4] Cleanup malformed test cases Robin Rosenberg
@ 2008-11-27 21:15       ` Robin Rosenberg
  0 siblings, 0 replies; 10+ messages in thread
From: Robin Rosenberg @ 2008-11-27 21:15 UTC (permalink / raw)
  To: spearce; +Cc: git, fonseca, Robin Rosenberg

Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
---
 .../org/spearce/jgit/lib/RepositoryTestCase.java   |   21 +++++++++++++++++++-
 1 files changed, 20 insertions(+), 1 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 ef4fd1b..cab65a0 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,7 @@
 import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
 import java.io.Reader;
+import java.util.ArrayList;
 
 import junit.framework.TestCase;
 import org.spearce.jgit.util.JGitTestUtil;
@@ -145,6 +146,8 @@ protected static void checkFile(File f, final String checkData)
 	private static int testcount;
 	private static Thread shutdownhook;
 
+	private ArrayList<Repository> repositoriesToClose = new ArrayList<Repository>();
+
 	public void setUp() throws Exception {
 		super.setUp();
 		System.gc();
@@ -192,6 +195,20 @@ copyFile(JGitTestUtil.getTestResourceFile(packs[k] + ".idx"), new File(packDir,
 
 	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
+		// mmemory mapped files.
+		System.gc();
+
+		recursiveDelete(trash, false, getName());
+		for (Repository r : repositoriesToClose) {
+			recursiveDelete(r.getWorkDir(), false, getName());
+		}
+		repositoriesToClose.clear();
+
 		super.tearDown();
 	}
 
@@ -209,12 +226,14 @@ protected Repository createNewEmptyRepo() throws IOException {
 		assertTrue(unusedDir.mkdirs());
 		final Repository newRepo = new Repository(newTestRepo);
 		newRepo.create();
+		final String name = getClass().getName() + "." + getName();
 		Runtime.getRuntime().addShutdownHook(new Thread() {
 			@Override
 			public void run() {
-				recursiveDelete(newTestRepo);
+				recursiveDelete(newTestRepo, false, name);
 			}
 		});
+		repositoriesToClose.add(newRepo);
 		return newRepo;
 	}
 
-- 
1.6.0.3.640.g6331a

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [JGIT PATCH 0/4] RepositoryTestCase cleanups
  2008-11-27 21:13 [JGIT PATCH 0/4] RepositoryTestCase cleanups Robin Rosenberg
@ 2008-11-27 21:49 ` Shawn O. Pearce
  2008-11-29 12:01   ` Robin Rosenberg
  2008-11-30 14:18   ` Robin Rosenberg
  0 siblings, 2 replies; 10+ messages in thread
From: Shawn O. Pearce @ 2008-11-27 21:49 UTC (permalink / raw)
  To: Robin Rosenberg; +Cc: git, fonseca

Robin Rosenberg <robin.rosenberg@dewire.com> wrote:
> Ok, so here is an attempt to improve the ability of the JGit's unit
> tests to delete temporary repositories. This has probably been seen
> by many, but Jonas Fonseca raised the issue.

Hmpph.  This takes 19 seconds to run the suite, where it used to be
only 2 seconds on the same system.  The slower run isn't something
I'm too happy about, actually I'd like to make the run even faster
than 2 seconds.
 
> The background is that on Windows you cannot delete files that are
> open and mmapped files are open until they get unmapped, which in
> Java is beyond explicit programmer control. You can only free the
> resources and pray that the GC does the work. Fortunately it usually
> does. It turned out our testcases weren't even trying to clean up
> properly. 

If the issue is mmap'd files, why don't we instead disable mmap
on Windows during JUnit tests, and use the non-mmap variant of
pack access?  At least do that for the bulk of the tests, and
then have a single test case which tests the mmap code path but
has careful System.gc calls in place to try and ensure we can
actually clean up the temporary files.

Another option is to refactor the Repository class a little so we
can replace local filesystem IO with something else, like say an
in-core repository.  E.g. a pack file and/or pack index stored in
a byte[], and refs stored in a HashMap.  We'd still need a couple
of tests to verify local disk IO, but many of the tests can be
validated against such a pure in-memory Repository concept.

I'd actually like to get that Repository refactoring done soon,
someone else was asking about it for the RefDatabase (to store the
refs in a SQL database so JGit ties into JTA) but I may also want
it for Gerrit 2 - I'm looking at doing something that would put
200,000 refs per year into a repository.  That's so large that
most operations can't afford to scan the entire ref database,
and it really cannot be loose.  ;-)


Refactoring repository is a fair chunk of work, disabling the mmap
feature under Windows in JUnit may be easier.  Hmm, according to
WindowCache's <clinit> its default by false.  Why is it enabling
on Windows?  The only code that calls WindowCache.reconfigure()
is in the Eclipse plugin, so pure JGit unit tests shouldn't be
turning on mmap code *at all*.

Which also points out a gap in our tests.  Nothing new, we have
lots of gaps.  *sigh*

-- 
Shawn.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [JGIT PATCH 0/4] RepositoryTestCase cleanups
  2008-11-27 21:49 ` Shawn O. Pearce
@ 2008-11-29 12:01   ` Robin Rosenberg
  2008-12-01 23:18     ` Johannes Schindelin
  2008-11-30 14:18   ` Robin Rosenberg
  1 sibling, 1 reply; 10+ messages in thread
From: Robin Rosenberg @ 2008-11-29 12:01 UTC (permalink / raw)
  To: Shawn O. Pearce; +Cc: git, fonseca

torsdag 27 november 2008 22:49:16 skrev Shawn O. Pearce:
> Robin Rosenberg <robin.rosenberg@dewire.com> wrote:
> > Ok, so here is an attempt to improve the ability of the JGit's unit
> > tests to delete temporary repositories. This has probably been seen
> > by many, but Jonas Fonseca raised the issue.
> 
> Hmpph.  This takes 19 seconds to run the suite, where it used to be
> only 2 seconds on the same system.  The slower run isn't something
> I'm too happy about, actually I'd like to make the run even faster
> than 2 seconds.

So maybe we should clean up less. Every new test repo we create has
a new name so we could do without cleaning up so much. The cleanup
however is a verification that we close (and can close) our resources, 
though it only works on Windows :/ On unix we could spawn lsof but that
is really really slow. 

> If the issue is mmap'd files, why don't we instead disable mmap
> on Windows during JUnit tests, and use the non-mmap variant of
> pack access?  At least do that for the bulk of the tests, and
> then have a single test case which tests the mmap code path but
> has careful System.gc calls in place to try and ensure we can
> actually clean up the temporary files.
We would then need some other really slow test to play rough with
memory mapping and gc. As I mentioned above it is actually about
closing resources in general, mmapped files being an especially 
nasty case.

> I'd actually like to get that Repository refactoring done soon,
> someone else was asking about it for the RefDatabase (to store the
> refs in a SQL database so JGit ties into JTA) but I may also want
> it for Gerrit 2 - I'm looking at doing something that would put
> 200,000 refs per year into a repository.  That's so large that
> most operations can't afford to scan the entire ref database,
> and it really cannot be loose.  ;-)

Would be cool, but having that diff engine is more important to me.

> 
> 
> Refactoring repository is a fair chunk of work, disabling the mmap
> feature under Windows in JUnit may be easier.  Hmm, according to
> WindowCache's <clinit> its default by false.  Why is it enabling
> on Windows?  The only code that calls WindowCache.reconfigure()
> is in the Eclipse plugin, so pure JGit unit tests shouldn't be
> turning on mmap code *at all*.
> 
> Which also points out a gap in our tests.  Nothing new, we have
> lots of gaps.  *sigh*

Yep.  :/

-- robin

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [JGIT PATCH 0/4] RepositoryTestCase cleanups
  2008-11-27 21:49 ` Shawn O. Pearce
  2008-11-29 12:01   ` Robin Rosenberg
@ 2008-11-30 14:18   ` Robin Rosenberg
  1 sibling, 0 replies; 10+ messages in thread
From: Robin Rosenberg @ 2008-11-30 14:18 UTC (permalink / raw)
  To: Shawn O. Pearce; +Cc: git, fonseca

torsdag 27 november 2008 22:49:16 skrev Shawn O. Pearce:
> Robin Rosenberg <robin.rosenberg@dewire.com> wrote:
> > Ok, so here is an attempt to improve the ability of the JGit's unit
> > tests to delete temporary repositories. This has probably been seen
> > by many, but Jonas Fonseca raised the issue.
> 
> Hmpph.  This takes 19 seconds to run the suite, where it used to be
> only 2 seconds on the same system.  The slower run isn't something
> I'm too happy about, actually I'd like to make the run even faster
> than 2 seconds.

Ok, that's the GC calls, I added. I'll post a completely new set of patches
disabling mmap by default, overridable using system properties.

-- robin

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [JGIT PATCH 0/4] RepositoryTestCase cleanups
  2008-11-29 12:01   ` Robin Rosenberg
@ 2008-12-01 23:18     ` Johannes Schindelin
  0 siblings, 0 replies; 10+ messages in thread
From: Johannes Schindelin @ 2008-12-01 23:18 UTC (permalink / raw)
  To: Robin Rosenberg; +Cc: Shawn O. Pearce, git, fonseca

Hi,

On Sat, 29 Nov 2008, Robin Rosenberg wrote:

> [Repository refactoring] Would be cool, but having that diff engine is 
> more important to me.

Stay tuned.  I have something that outputs something resembling a diff 
now.  Of course, the output is not correct yet, due to bugs I introduced 
cunnily when trying to fix another bug.

I'll keep you posted,
Dscho

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2008-12-01 23:20 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-11-27 21:15 [JGIT PATCH 0/4] RepositoryTestCase cleanups Robin Rosenberg
2008-11-27 21:15 ` [JGIT PATCH 1/4] Make the cleanup less verbose when it fails to delete temporary stuff Robin Rosenberg
2008-11-27 21:15   ` [JGIT PATCH 2/4] Add shutdown hooks to try to clean up after unit tests anyway Robin Rosenberg
2008-11-27 21:15     ` [JGIT PATCH 3/4] Cleanup malformed test cases Robin Rosenberg
2008-11-27 21:15       ` [JGIT PATCH 4/4] Automatically clean up any repositories created by the " Robin Rosenberg
  -- strict thread matches above, loose matches on Subject: below --
2008-11-27 21:13 [JGIT PATCH 0/4] RepositoryTestCase cleanups Robin Rosenberg
2008-11-27 21:49 ` Shawn O. Pearce
2008-11-29 12:01   ` Robin Rosenberg
2008-12-01 23:18     ` Johannes Schindelin
2008-11-30 14:18   ` Robin Rosenberg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).