BAEL-4464 : how to implement LRU-Cache in java codes added (#11036)
* BAEL-4464 : how to implement LRU-Cache in java codes added * BAEL-4464 : how to implement LRU-Cache in java codes added - package named fixed * BAEL-4464 : how to implement LRU-Cache in java codes added - package named changed * BAEL-4464 : how to implement LRU-Cache in java codes added - unitTest fixed
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.lrucache;
|
||||
|
||||
import com.baeldung.lrucache.Cache;
|
||||
import com.baeldung.lrucache.LRUCache;
|
||||
import org.hamcrest.core.AllOf;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import static org.hamcrest.core.Is.is;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class LRUCacheUnitTest {
|
||||
|
||||
@Test
|
||||
public void addSomeDataToCache_WhenGetData_ThenIsEqualWithCacheElement() {
|
||||
LRUCache<String, String> lruCache = new LRUCache<>(3);
|
||||
lruCache.put("1", "test1");
|
||||
lruCache.put("2", "test2");
|
||||
lruCache.put("3", "test3");
|
||||
assertEquals(lruCache.get("1").get(), "test1");
|
||||
assertEquals(lruCache.get("2").get(), "test2");
|
||||
assertEquals(lruCache.get("3").get(), "test3");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addDataToCacheToTheNumberOfSize_WhenAddOneMoreData_ThenLeastRecentlyDataWillEvict() {
|
||||
LRUCache<String, String> lruCache = new LRUCache<>(3);
|
||||
lruCache.put("1", "test1");
|
||||
lruCache.put("2", "test2");
|
||||
lruCache.put("3", "test3");
|
||||
lruCache.put("4", "test4");
|
||||
assertEquals(lruCache.get("1").isPresent(), false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runMultiThreadTask_WhenPutDataInConcurrentToCache_ThenNoDataLost() throws Exception {
|
||||
final int size = 50;
|
||||
final ExecutorService executorService = Executors.newFixedThreadPool(5);
|
||||
Cache<Integer, String> cache = new LRUCache<>(size);
|
||||
CountDownLatch countDownLatch = new CountDownLatch(size);
|
||||
try {
|
||||
IntStream.range(0, size).<Runnable>mapToObj(key -> () -> {
|
||||
cache.put(key, "value" + key);
|
||||
System.out.println(Thread.currentThread().getName() + " " + key);
|
||||
countDownLatch.countDown();
|
||||
}).forEach(executorService::submit);
|
||||
countDownLatch.await();
|
||||
} finally {
|
||||
executorService.shutdown();
|
||||
}
|
||||
assertEquals(cache.size(), size);
|
||||
IntStream.range(0, size).forEach(i -> assertEquals(cache.get(i).get(), "value" + i));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user