示例#1
0
 /** Test that the memory store can fit 65k elements in a 64Mb heap. */
 @Test
 public void testMemoryStoreOutOfMemoryLimit() throws Exception {
   Assume.assumeThat(JvmInformation.isJRockit(), is(false));
   LOG.info("Starting out of memory limit test");
   // Set size so the second element overflows to disk.
   cache = manager.getCache("memoryLimitTest");
   if (cache == null) {
     cache = new Cache(new CacheConfiguration("memoryLimitTest", 0));
     manager.addCache(cache);
   }
   for (int i = 0; i < 65000; i++) {
     cache.put(new Element(Integer.valueOf(i), new String(new char[218])));
   }
   assertEquals(65000, cache.getSize());
 }
示例#2
0
  @Test
  public void testOnRemove() {
    final MutableInteger mutableInteger = new MutableInteger();
    Cache<String, String> cache =
        new LFUCache<String, String>(2) {
          @Override
          protected void onRemove(String key, String cachedObject) {
            mutableInteger.value++;
          }
        };

    cache.put("1", "val1");
    cache.put("2", "val2");
    assertEquals(0, mutableInteger.value);
    cache.put("3", "val3");
    assertEquals(2, mutableInteger.value);
  }
  @Test
  public void spr14853AdaptsToOptionalWithSync() {
    AnnotationConfigApplicationContext context =
        new AnnotationConfigApplicationContext(Spr14853Config.class);
    Spr14853Service bean = context.getBean(Spr14853Service.class);
    Cache cache = context.getBean(CacheManager.class).getCache("itemCache");

    TestBean tb = new TestBean("tb1");
    bean.insertItem(tb);
    assertSame(tb, bean.findById("tb1").get());
    assertSame(tb, cache.get("tb1").get());

    cache.clear();
    TestBean tb2 = bean.findById("tb1").get();
    assertNotSame(tb, tb2);
    assertSame(tb2, cache.get("tb1").get());
  }
示例#4
0
 /**
  * Creates a cache with the given policy with a MemoryStore only and adds it to the manager.
  *
  * @param evictionPolicy
  * @throws CacheException
  */
 protected void createMemoryOnlyStore(
     MemoryStoreEvictionPolicy evictionPolicy, int memoryStoreSize) throws CacheException {
   manager.removeCache("test");
   cache =
       new Cache(
           "test", memoryStoreSize, evictionPolicy, false, null, false, 60, 30, false, 60, null);
   manager.addCache(cache);
   store = cache.getStore();
 }
示例#5
0
  @Test
  public void testPrune() {
    Cache<String, String> cache = new LFUCache<>(3);
    cache.put("1", "1");
    cache.put("2", "2");
    cache.put("3", "3");

    assertEquals(3, cache.size());
    assertEquals(3, cache.prune());
    assertEquals(0, cache.size());

    cache.put("4", "4");
    assertEquals(0, cache.prune());
    assertEquals(1, cache.size());
  }
示例#6
0
  @Test
  public void testEndless() {
    Cache<String, String> cache = new LRUCache<String, String>(0);
    assertFalse(cache.isFull());
    cache.put("1", "1");
    assertEquals(1, cache.size());
    assertFalse(cache.isFull());

    cache.put("2", "2");
    assertEquals(2, cache.size());
    assertFalse(cache.isFull());
  }
示例#7
0
  /** Test elements can be put in the store */
  protected void putTest() throws IOException {
    Element element;

    assertEquals(0, store.getSize());

    element = new Element("key1", "value1");
    store.put(element);
    assertEquals(1, store.getSize());
    assertEquals("value1", store.get("key1").getObjectValue());

    element = new Element("key2", "value2");
    store.put(element);
    assertEquals(2, store.getSize());
    assertEquals("value2", store.get("key2").getObjectValue());

    for (int i = 0; i < 1999; i++) {
      store.put(new Element("" + i, new Date()));
    }

    assertEquals(4, store.getSize());
    assertEquals(2001, cache.getSize());
    assertEquals(3998, cache.getStatistics().getLocalDiskSize());

    /** Non serializable test class */
    class NonSerializable {
      //
    }

    store.put(new Element(new NonSerializable(), new NonSerializable()));

    assertEquals(4, store.getSize());
    assertEquals(2002, cache.getSize());
    assertEquals(1999, cache.getStatistics().getLocalDiskSize());
    //        assertEquals(1998, cache.getStatistics().getLocalDiskSize());    ???

    // smoke test
    for (int i = 0; i < 2000; i++) {
      store.get("" + i);
    }
  }
示例#8
0
  @Test
  public void testCacheTime() {
    Cache<String, String> cache = new LRUCache<String, String>(3);
    cache.put("3", "3");
    cache.put("2", "2");
    assertNotNull(cache.get("2"));
    cache.put("1", "1", 50);
    assertNotNull(cache.get("1"));
    assertTrue(cache.isFull());

    ThreadUtil.sleep(100);
    assertNull(cache.get("1")); // expired
    assertFalse(cache.isFull());
  }
示例#9
0
  @Test
  public void testShrinkingAndGrowingMemoryStore() {
    cache = new Cache("testShrinkingAndGrowingMemoryStore", 50, false, false, 120, 120);
    manager.addCache(cache);
    store = cache.getStore();

    if (!(store instanceof MemoryStore)) {
      LOG.info(
          "Skipping Growing/Shrinking Memory Store Test - Store is not a subclass of MemoryStore!");
      return;
    }

    int i = 0;
    for (; ; ) {
      int size = store.getSize();
      store.put(new Element(Integer.valueOf(i++), new byte[100]));
      if (store.getSize() <= size) break;
    }

    final int initialSize = store.getSize();
    final int shrinkSize = initialSize / 2;
    ((MemoryStore) store).memoryCapacityChanged(initialSize, shrinkSize);

    for (; ; ) {
      int size = store.getSize();
      store.put(new Element(Integer.valueOf(i++), new byte[100]));
      if (store.getSize() >= size) break;
    }

    {
      int size = store.getSize();
      assertTrue(size < (shrinkSize * 1.1));
      assertTrue(size > (shrinkSize * 0.9));
    }

    final int growSize = initialSize * 2;
    ((MemoryStore) store).memoryCapacityChanged(shrinkSize, growSize);

    for (; ; ) {
      int size = store.getSize();
      store.put(new Element(Integer.valueOf(i++), new byte[100]));
      if (store.getSize() <= size) break;
    }

    {
      int size = store.getSize();
      assertTrue(size < (growSize * 1.1));
      assertTrue(size > (growSize * 0.9));
    }
  }
示例#10
0
 /**
  * Creates a cache with the given policy and adds it to the manager.
  *
  * @param evictionPolicy
  * @throws CacheException
  */
 protected void createMemoryOnlyStore(MemoryStoreEvictionPolicy evictionPolicy)
     throws CacheException {
   manager.removeCache("testMemoryOnly");
   cache =
       new Cache(
           "testMemoryOnly",
           12000,
           evictionPolicy,
           false,
           System.getProperty("java.io.tmpdir"),
           false,
           60,
           30,
           false,
           60,
           null);
   manager.addCache(cache);
   store = cache.getStore();
 }
示例#11
0
 /** teardown */
 @Override
 @After
 public void tearDown() throws Exception {
   try {
     if (cache != null) {
       cache.removeAll();
       cache = null;
     }
     if (manager != null) {
       manager.shutdown();
       manager = null;
     }
   } catch (OutOfMemoryError e) {
     // OutOfMemoryError Happens at different places on Apache LRU for some reason
     LOG.info(e.getMessage());
   } catch (Throwable t) {
     // OutOfMemoryError Happens at different places on Apache LRU for some reason
     LOG.info(t.getMessage());
   }
 }
示例#12
0
  @Test
  public void testCache() {
    Cache<String, String> cache = new LRUCache<String, String>(3);
    cache.put("1", "1");
    cache.put("2", "2");
    assertFalse(cache.isFull());
    cache.put("3", "3");
    assertTrue(cache.isFull());

    assertNotNull(cache.get("1"));
    assertNotNull(cache.get("2"));
    cache.put("4", "4");
    assertNull(cache.get("3"));
    assertNotNull(cache.get("1"));
    assertNotNull(cache.get("2"));
    cache.put("3", "3");
    assertNull(cache.get("4"));
  }
示例#13
0
  @Test
  public void testCache2() {
    Cache<String, String> cache = new LFUCache<>(3);
    cache.put("1", "1");
    cache.put("2", "2");
    assertFalse(cache.isFull());
    cache.put("3", "3");
    assertTrue(cache.isFull());

    assertNotNull(cache.get("3"));
    assertNotNull(cache.get("3"));
    assertNotNull(cache.get("3")); // boost usage of a 3
    assertNotNull(cache.get("1"));
    assertNotNull(cache.get("2"));
    cache.put("4", "4"); // since this is LFU cache, 1 AND 2 will be removed, but not 3
    assertNotNull(cache.get("3"));
    assertNotNull(cache.get("4"));
    assertEquals(2, cache.size());
  }
示例#14
0
  @Test
  public void testCache() {
    Cache<String, String> cache = new LFUCache<>(3);
    cache.put("1", "1");
    cache.put("2", "2");
    assertFalse(cache.isFull());
    cache.put("3", "3");
    assertTrue(cache.isFull());

    assertNotNull(cache.get("1"));
    assertNotNull(cache.get("2"));
    cache.put("4", "4"); // new element, cache is full, prune is invoked
    assertNull(cache.get("3"));
    assertNotNull(cache.get("1"));
    assertNotNull(cache.get("2"));
    cache.put("3", "3");
    assertNull(cache.get("4"));
    assertNotNull(cache.get("3"));
  }
示例#15
0
 /**
  * Creates a store from the given configuration and cache within it.
  *
  * @param filePath
  * @param cacheName
  * @throws CacheException
  */
 protected void createMemoryStore(String filePath, String cacheName) throws CacheException {
   manager.shutdown();
   manager = CacheManager.create(filePath);
   cache = manager.getCache(cacheName);
   store = cache.getStore();
 }
示例#16
0
  @Test
  public void testBoosting() {
    Cache<String, String> cache = new LFUCache<>(3);
    cache.put("1", "1");
    cache.put("2", "2");
    cache.put("3", "3");

    cache.get("3");
    cache.get("3");
    cache.get("3");
    cache.get("3");
    cache.get("2");
    cache.get("2");
    cache.get("2");
    cache.get("1");
    cache.get("1");

    assertEquals(3, cache.size());

    cache.put("4", "4");

    assertNull(cache.get("1")); // 1 is less frequent and it is out of cache
    assertNotNull(cache.get("4")); // 4 is new and it is inside

    cache.get("3");
    cache.get("2");

    // bad sequence
    cache.put("5", "5");
    cache.get("5"); // situation: 2(1), 3(2), 5(1)   value(accessCount)
    cache.put("4", "4");
    cache.get("4"); // situation: 3(1), 4(1)
    cache.put("5", "5");
    cache.get("5"); // situation: 3(1), 4(1), 5(1)
    cache.put("4", "4");
    cache.get("4"); // situation: 4(1)

    assertEquals(3, cache.size());

    assertNull(cache.get("1"));
    assertNull(cache.get("2"));
    assertNotNull(cache.get("3"));
    assertNotNull(cache.get("4"));
    assertNotNull(cache.get("5"));
  }
示例#17
0
  @Test
  public void testCache2() {
    Cache<String, String> cache = new LRUCache<String, String>(3);
    cache.put("1", "1");
    cache.put("2", "2");
    assertFalse(cache.isFull());
    cache.put("3", "3");
    assertTrue(cache.isFull());

    assertNotNull(cache.get("3"));
    assertNotNull(cache.get("3"));
    assertNotNull(
        cache.get(
            "3")); // boost usage of a 3, but this doesn't change a thing, since this is a LRU cache
                   // and not a LFU cache
    assertNotNull(cache.get("1"));
    assertNotNull(cache.get("2"));
    cache.put("4", "4");
    assertNull(cache.get("3"));
    assertNotNull(cache.get("1"));
    assertNotNull(cache.get("2"));
    assertNotNull(cache.get("4"));
    cache.put("3", "3");
    assertNull(cache.get("1"));
  }