use of org.apache.solr.util.ConcurrentLFUCache in project lucene-solr by apache.
the class TestLFUCache method testConcurrentAccess.
@Test
public void testConcurrentAccess() throws InterruptedException {
/* Set up a thread pool with twice as many threads as there are CPUs. */
final ConcurrentLFUCache<Integer, Long> cache = new ConcurrentLFUCache<>(10, 9);
ExecutorService executorService = ExecutorUtil.newMDCAwareFixedThreadPool(10, new DefaultSolrThreadFactory("testConcurrentAccess"));
final AtomicReference<Throwable> error = new AtomicReference<>();
/*
* Use the thread pool to execute at least two million puts into the cache.
* Without the fix on SOLR-7585, NoSuchElementException is thrown.
* Simultaneous calls to markAndSweep are protected from each other by a
* lock, so they run sequentially, and due to a problem in the previous
* design, the cache eviction doesn't work right.
*/
for (int i = 0; i < atLeast(2_000_000); ++i) {
executorService.submit(() -> {
try {
cache.put(random().nextInt(100), random().nextLong());
} catch (Throwable t) {
error.compareAndSet(null, t);
}
});
}
executorService.shutdown();
executorService.awaitTermination(1, TimeUnit.MINUTES);
// then:
assertNull("Exception during concurrent access: " + error.get(), error.get());
}
Aggregations