cache = new Cache<>(12, 50, true);
cache = new Cache<>(12, 50, true);
cache = new Cache<>(20, maxStrongReferences, false); cache.setKeyCollisionAllowed(true);
cache = new Cache<>(20, maxStrongReferences, false); cache.setKeyCollisionAllowed(true);
/** * Tests {@link Cache} as a {@link java.util.Map} using strong references. * The tested {@code Cache} shall behave like a standard {@link HashMap}, * except for element order. * * @see WeakValueHashMapTest#testStrongReferences() */ @Test public void testStrongReferences() { WeakValueHashMapTest.testStrongReferences(new Cache<>(WeakValueHashMapTest.SAMPLE_SIZE, 0, false)); }
/** * Tests {@link Cache} as a {@link java.util.Map} using weak references. In this test, we * have to keep in mind than some elements in {@code weakMap} may disappear at any time. * * @throws InterruptedException if the test has been interrupted. * * @see WeakValueHashMapTest#testWeakReferences() */ @Test @DependsOnMethod("testStrongReferences") public void testWeakReferences() throws InterruptedException { WeakValueHashMapTest.testWeakReferences(new Cache<>(WeakValueHashMapTest.SAMPLE_SIZE, 0, false)); }
public void stress() throws InterruptedException { final int count = 5000; final Cache<Integer,Integer> cache = new Cache<>(); final AtomicReference<Throwable> failures = new AtomicReference<>(); final class WriterThread extends Thread {
/** * Tests adding a single value using the {@link Cache.Handler#putAndUnlock(Object)} method. * This method does all the operations in a single thread. */ @Test public void testPutAndUnlock() { final String key = "The key"; final String value = "The value"; final Cache<String,String> cache = new Cache<>(); assertTrue("No initial value expected.", cache.isEmpty()); assertNull("No initial value expected.", cache.peek(key)); final Cache.Handler<String> handler = cache.lock(key); assertNull("No initial value expected.", handler.peek()); handler.putAndUnlock(value); assertEquals(1, cache.size()); assertEquals(value, cache.peek(key)); assertEquals(singleton(key), cache.keySet()); assertEquals(singleton(new SimpleEntry<>(key, value)), cache.entrySet()); }
final String keyByOtherThread = "keyByOtherThread"; final String valueByOtherThread = "valueByOtherThread"; final Cache<String,String> cache = new Cache<>(); final class OtherThread extends Thread {