Browse Source

Merge branch 'stable-4.5'

* stable-4.5:
  Unconditionally close repositories in RepositoryCache.clear()
  Fix eviction of repositories with negative usage count

Adapt to parameter removed from
RepositoryCache.unregisterAndCloseRepository().

Change-Id: I7087667056ced401a3b3a027977f2715cd77a1c5
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
stable-4.6
Matthias Sohn 8 years ago
parent
commit
ba7ba7a816
  1. 21
      org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryCacheTest.java
  2. 10
      org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryCache.java

21
org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryCacheTest.java

@ -209,6 +209,27 @@ public class RepositoryCacheTest extends RepositoryTestCase {
assertTrue(RepositoryCache.isCached(d2)); 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 @Test
public void testRepositoryUnregisteringWhenExpired() throws Exception { public void testRepositoryUnregisteringWhenExpired() throws Exception {
Repository repoA = createBareRepository(); Repository repoA = createBareRepository();

10
org.eclipse.jgit/src/org/eclipse/jgit/lib/RepositoryCache.java

@ -298,7 +298,7 @@ public class RepositoryCache {
} }
private boolean isExpired(Repository db) { 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); && (System.currentTimeMillis() - db.closedAt.get() > expireAfter);
} }
@ -325,15 +325,9 @@ public class RepositoryCache {
} }
private void clearAll() { private void clearAll() {
for (int stage = 0; stage < 2; stage++) {
for (Iterator<Map.Entry<Key, Reference<Repository>>> i = cacheMap for (Iterator<Map.Entry<Key, Reference<Repository>>> i = cacheMap
.entrySet().iterator(); i.hasNext();) { .entrySet().iterator(); i.hasNext();) {
final Map.Entry<Key, Reference<Repository>> e = i.next(); unregisterAndCloseRepository(i.next().getKey());
final Repository db = e.getValue().get();
if (db != null)
db.close();
i.remove();
}
} }
} }

Loading…
Cancel
Save