From 5a1e147eaa729ba0ba75bdeebcc745a55bff6232 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Hugo=20Ar=C3=A8s?= Date: Wed, 12 Oct 2016 06:54:52 -0400 Subject: [PATCH] Fix eviction of repositories with negative usage count MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit If the repository close method was called twice (or more) for one open, the usage count became negative and the repository was never be evicted from the cache because the method checking if repository is expired was not considering negative usage count. Change-Id: I18a80c415c54c37d1b9def2b311ff2d0afa455ca Signed-off-by: Hugo Arès --- .../eclipse/jgit/lib/RepositoryCacheTest.java | 21 +++++++++++++++++++ .../org/eclipse/jgit/lib/RepositoryCache.java | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) 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 6bea320120..b44b4c34a7 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 2e0ed16a55..98ac601130 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); } -- 2.39.5