Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
corgiboygsj committed Jun 17, 2021
1 parent cea7c08 commit 1c9c0e9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public long tick() {
long current = now();
for (Iterator<CacheNode<K, V>> it = this.nodes(); it.hasNext();) {
CacheNode<K, V> node = it.next();
if (current - node.time() > expireTime) {
if (current - node.time() >= expireTime) {
// Remove item while iterating map (it must be ConcurrentMap)
this.remove(node.key());
expireItems++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void teardown() throws Exception {

protected abstract Cache<Id, Object> newCache();

protected abstract Cache<Id, Object> newCache(long capacity) ;
protected abstract Cache<Id, Object> newCache(long capacity);

protected abstract void checkSize(Cache<Id, Object> cache, long size,
Map<Id, Object> kvs);
Expand Down Expand Up @@ -673,5 +673,24 @@ public void testMutiThreadsGetAndUpdateWithGtCapacity() {
// In fact, the size may be any value(such as 43)
Assert.assertTrue(cache.size() < 10 + THREADS_NUM);
}

@Test
public void testKeyExpired() {
Cache<Id, Object> cache = newCache();
cache.expire(2000L);

Id key = IdGenerator.of("key");
cache.update(key, "value", -1000L);

waitTillNext(1);
cache.tick();

Assert.assertFalse(cache.containsKey(key));

cache.update(key, "value", -2000L);
cache.tick();

Assert.assertFalse(cache.containsKey(key));
}
}

0 comments on commit 1c9c0e9

Please sign in to comment.