diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryCacheTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryCacheTest.java index 6bea32012..b44b4c34a 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryCacheTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryCacheTest.java @@ -209,6 +209,27 @@ public class RepositoryCacheTest extends RepositoryTestCase { assertTrue(RepositoryCache.isCached(d2)); } + @Test + public void testRepositoryUnregisteringWhenExpiredAndUsageCountNegative() + throws Exception { + Repository repoA = createBareRepository(); + RepositoryCache.register(repoA); + + assertEquals(1, RepositoryCache.getRegisteredKeys().size()); + assertTrue(RepositoryCache.isCached(repoA)); + + // close the repo twice to make usage count negative + repoA.close(); + repoA.close(); + // fake that repoA was closed more than 1 hour ago (default expiration + // time) + repoA.closedAt.set(System.currentTimeMillis() - 65 * 60 * 1000); + + RepositoryCache.clearExpired(); + + assertEquals(0, RepositoryCache.getRegisteredKeys().size()); + } + @Test public void testRepositoryUnregisteringWhenExpired() throws Exception { Repository repoA = createBareRepository(); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryCache.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryCache.java index e5a0553ab..9a57349f5 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryCache.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryCache.java @@ -298,7 +298,7 @@ public class RepositoryCache { } private boolean isExpired(Repository db) { - return db != null && db.useCnt.get() == 0 + return db != null && db.useCnt.get() <= 0 && (System.currentTimeMillis() - db.closedAt.get() > expireAfter); } @@ -325,15 +325,9 @@ public class RepositoryCache { } private void clearAll() { - for (int stage = 0; stage < 2; stage++) { - for (Iterator>> i = cacheMap - .entrySet().iterator(); i.hasNext();) { - final Map.Entry> e = i.next(); - final Repository db = e.getValue().get(); - if (db != null) - db.close(); - i.remove(); - } + for (Iterator>> i = cacheMap + .entrySet().iterator(); i.hasNext();) { + unregisterAndCloseRepository(i.next().getKey()); } }