Fix eviction of repositories with negative usage count

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 <hugo.ares@ericsson.com>
This commit is contained in:
Hugo Arès 2016-10-12 06:54:52 -04:00
parent 15341ce577
commit 5a1e147eaa
2 changed files with 22 additions and 1 deletions

View File

@ -209,6 +209,27 @@ public void testRepositoryNotUnregisteringWhenClosing() throws Exception {
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();

View File

@ -298,7 +298,7 @@ private Repository unregisterRepository(final Key location) {
}
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);
}