Skip to content

Commit

Permalink
cover lruEvictionTest
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzad16 committed Aug 14, 2024
1 parent 7c88aac commit 82cacd5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -20,6 +21,7 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
Expand Down Expand Up @@ -49,6 +51,41 @@ public void flushAllTest() {
}
}

@Test // T.4.1
public void lruEvictionTest() {
final int count = 100;
final int extra = 10;

// Add 100 + 10 keys to Redis (e.g., SET key:1 ... SET key:100)
for (int i = 0; i < count + extra; i++) {
control.set("key:" + i, "value" + i);
}

Map<CacheKey, CacheEntry> map = new LinkedHashMap<>(count);
Cache cache = new DefaultCache(count, map);
try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), cache)) {

// Retrieve the 100 keys in the same order (e.g., GET key:1 ... GET key:100)
for (int i = 0; i < count; i++) {
jedis.get("key:" + i);
}
assertThat(map, Matchers.aMapWithSize(count));

List<CacheKey> earlierKeys = new ArrayList<>(map.keySet()).subList(0, extra);
// earlier keys in map
earlierKeys.forEach(cacheKey -> assertThat(map, Matchers.hasKey(cacheKey)));

// Retrieve the 10 extra keys
for (int i = count; i < count + extra; i++) {
jedis.get("key:" + i);
}

// earlier keys NOT in map
earlierKeys.forEach(cacheKey -> assertThat(map, Matchers.not(Matchers.hasKey(cacheKey))));
assertThat(map, Matchers.aMapWithSize(count));
}
}

@Test // T.5.2
public void deleteByKeyTest() {
final int count = 100;
Expand Down
1 change: 0 additions & 1 deletion src/test/java/redis/clients/jedis/csc/TestCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public TestCache(Map<CacheKey, CacheEntry> map) {
}

public TestCache(Map<CacheKey, CacheEntry> map, Cacheable cacheable) {

super(10000, map, cacheable, new LRUEviction(10000));
}

Expand Down

0 comments on commit 82cacd5

Please sign in to comment.