Example #1
0
 @Test
 public void testWithNullAsKeyAndValue() throws Exception {
   final T cache = getInstance(1000, 1000);
   assertEquals(0, (long) cache.size());
   assertFalse(cache.contains(null));
   assertNull(cache.get(null));
   // Add entry with null as key and value ...
   cache.put(null, null);
   assertEquals(1, (long) cache.size());
   assertTrue(cache.contains(null));
   assertNull(cache.get(null));
   // Add another entry ...
   cache.put(new Object(), new Object());
   assertEquals(2, (long) cache.size());
   // Remove entry with null as key ...
   assertNull(cache.remove(null).getValue());
   assertEquals(1, (long) cache.size());
   assertFalse(cache.contains(null));
   assertNull(cache.get(null));
   // Try to remove entry with null as key again ...
   assertNull(cache.remove(null));
   assertEquals(1, (long) cache.size());
   assertFalse(cache.contains(null));
   assertNull(cache.get(null));
 }
Example #2
0
 @Test
 public void testShouldNotGrowLargerThanMaxSize() throws Exception {
   final long maxCacheSize = 1000;
   final T cache = getInstance(Integer.MAX_VALUE, (int) maxCacheSize);
   final List<ParallelTestRunner.Worker> workers = new ArrayList<>();
   final int numThreads = 50;
   final int numRoundsPerThread = 50000;
   final int minKey = 10000;
   final int maxKey = 99999;
   for (int i = 0; i < numThreads; ++i) {
     final Random random = new Random(i);
     workers.add(
         new ParallelTestRunner.Worker() {
           @Override
           public void run() throws Exception {
             for (int i = 0; i < numRoundsPerThread; ++i) {
               final int key = random.nextInt(maxKey - minKey) + minKey;
               cache.put(key, Integer.toBinaryString(key));
             }
           }
         });
   }
   final ParallelTestRunner parallelTestRunner = new ParallelTestRunner(workers);
   parallelTestRunner.run();
   assertThat(cache.size(), isLessThanOrEqualTo(maxCacheSize));
 }
Example #3
0
 @Test
 public void testWithMaxSize1() {
   final T cache = getInstance(1000, 1);
   final Object o1 = new Object();
   cache.put(o1, o1);
   final Object o2 = new Object();
   cache.put(o2, o2);
   assertEquals(1, (long) cache.size());
   assertFalse(cache.contains(o1));
   assertTrue(cache.contains(o2));
 }
Example #4
0
 @Test
 public void testWithNullAsValue() throws Exception {
   final T cache = getInstance(1000, 1000);
   final Object o = new Object();
   assertEquals(0, (long) cache.size());
   assertFalse(cache.contains(o));
   assertNull(cache.get(o));
   // Add entry with null as value ...
   cache.put(o, null);
   assertEquals(1, (long) cache.size());
   assertTrue(cache.contains(o));
   assertNull(cache.get(o));
   // Remove entry with null as value ...
   assertNull(cache.remove(o).getValue());
   assertEquals(0, (long) cache.size());
   assertFalse(cache.contains(o));
   assertNull(cache.get(o));
   // Try to remove entry with null as value again ...
   assertNull(cache.remove(o));
   assertEquals(0, (long) cache.size());
   assertFalse(cache.contains(o));
   assertNull(cache.get(o));
 }