⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 entitycachetester.java

📁 uPortal是开放源码的Portal门户产品
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
 *  - Size of cache must equal number of adds. * Sleeps for 1/2 of (sweep interval + max age), then touches [touched] number  * of entities. Now adds [secondAdded] number of entities and sleeps for * 1/2 of sweep interval. Sweep should remove all firstAdded entities except those  * that were touched. *  - Size of cache must = secondAdded + touched - firstAdded.  */public void testIEntityCacheSweep() throws Exception{    print("***** ENTERING EntityCacheTester.testIEntityCacheSweep() *****");    String msg = null;    int idx = 0;    IBasicEntity ent = null;    int firstAdded = 100;    int secondAdded = 25;    int touched = firstAdded / 2;    int maxSize = (firstAdded + 1);    int maxIdleSecs = 50;    int sweepIntervalSecs = maxIdleSecs / 2;    IEntityCache c = getEntityCache(maxSize, maxIdleSecs*1000, sweepIntervalSecs*1000);    msg = "Adding " + firstAdded + " entities to the cache.";    print(msg);    for(idx=0; idx<firstAdded; idx++)        { c.add(testEntities[idx]); }    assertEquals(msg, c.size(), firstAdded);    int sleepSecs = ((maxIdleSecs + sweepIntervalSecs) / 2);    print("Now sleeping for " + sleepSecs  + " secs.");    Thread.sleep(sleepSecs*1000);    print("Now touching " + touched + " entries.");    for(idx=0; idx<touched; idx++)        { c.get(testEntityKeys[idx]); }    msg = "Adding " + secondAdded + " entities to the cache.";    print(msg);    for(idx=firstAdded; idx<firstAdded+secondAdded; idx++)        { c.add(testEntities[idx]); }    print("Now sleeping for " + (maxIdleSecs/2) + " secs.");    Thread.sleep(maxIdleSecs*500);    msg = "Sweep should have purged " + (firstAdded - touched) + " entries.";    print(msg);    assertEquals(msg, (touched + secondAdded), c.size());    print("***** LEAVING EntityCacheTester.testIEntityCacheSweep() *****");}/** * Gets an INVALIDATING instance of IEntityCache and adds, retrieves and removes  * entities directly from the cache.   *  *  - After adds, cache size must equal number of adds. *  - Adds must be correctly retrieved. *  - Removes must not be found. *  - After removes, cache size must be 0. */public void testInvalidatingCacheAddsAndDeletes() throws Exception{    print("***** ENTERING EntityCacheTester.testInvalidatingCacheAddsAndDeletes() *****");    String msg = null;    int idx = 0;    IBasicEntity ent = null;    int numEntitiesToBeTested = 100;    IEntityCache c = getInvalidatingEntityCache();    ReferenceInvalidatingEntityCache rec = (ReferenceInvalidatingEntityCache) c;    msg = "Adding " + numEntitiesToBeTested + " entities to the cache.";    print(msg);    for(idx=0; idx<numEntitiesToBeTested; idx++)        { c.add(testEntities[idx]); }    assertEquals(msg, rec.size(), numEntitiesToBeTested);    msg = "Retrieving entities from the cache.";    print(msg);    for(idx=0; idx<numEntitiesToBeTested; idx++)    {        ent = c.get( testEntityKeys[idx] );        assertEquals(msg, ent, testEntities[idx]);    }    msg = "Removing entities from the cache.";    print(msg);    for(idx=0; idx<numEntitiesToBeTested; idx++)    {        c.remove( testEntityKeys[idx] );        ent = c.get( testEntityKeys[idx] );        assertNull(msg, ent);    }    // We should have removed all entries.    assertEquals(msg, rec.size(), 0);    print("***** LEAVING EntityCacheTester.testInvalidatingCacheAddsAndDeletes() *****");}/** * Get 2 INVALIDATING IEntityCaches and add the same entities to each. *  - Size of each cache must = number entities added. * Update some entities from the first and remove other entities from the second, * then sleep for the sweep interval. *  - First cache must have number entities added - number of deletes from second. *  - Second cache must have number entities added - number of deletes - number updates. */public void testInvalidatingCacheInvalidation() throws Exception{    print("***** ENTERING EntityCacheTester.testInvalidatingInvalidation() *****");    String msg = null;    int idx = 0;    IBasicEntity ent = null;    int numEntitiesToBeAdded = 10;    int numEntitiesToBeUpdated = 5;    int numEntitiesToBeDeleted = 5;    IEntityCache cacheA = getInvalidatingEntityCache();    ReferenceInvalidatingEntityCache recA = (ReferenceInvalidatingEntityCache) cacheA;    IEntityCache cacheB = getInvalidatingEntityCache();    ReferenceInvalidatingEntityCache recB = (ReferenceInvalidatingEntityCache) cacheB;    msg = "Adding " + numEntitiesToBeAdded + " entities to both caches.";    print(msg);    for(idx=0; idx<numEntitiesToBeAdded; idx++)    {        cacheA.add(testEntities[idx]);        cacheB.add(testEntities[idx]);    }    assertEquals(msg, recA.size(), numEntitiesToBeAdded);    assertEquals(msg, recB.size(), numEntitiesToBeAdded);    Thread.sleep(100);    msg = "Updating " + numEntitiesToBeUpdated + " in first cache.";    print(msg);    for(idx=0; idx<numEntitiesToBeUpdated; idx++)        { cacheA.update( testEntities[idx] ); }    msg = "Removing " + numEntitiesToBeDeleted + " from second cache.";    print(msg);    for(idx=numEntitiesToBeUpdated; idx<numEntitiesToBeAdded; idx++)        { cacheB.remove( testEntityKeys[idx] ); }    print("Will now sleep for " + (cacheSweepIntervalSecs + 5) + " seconds.");    Thread.sleep( (cacheSweepIntervalSecs + 5) * 1000);    // Check the caches.    msg = "Checking first cache for invalidations";    print(msg);    assertEquals(msg, (numEntitiesToBeAdded - numEntitiesToBeDeleted), recA.size());    for(idx=numEntitiesToBeUpdated; idx<numEntitiesToBeAdded; idx++)        { assertNull(msg, cacheA.get( testEntityKeys[idx] )); }    msg = "Check second cache for invalidations";    assertEquals(msg, (numEntitiesToBeAdded - numEntitiesToBeDeleted - numEntitiesToBeUpdated), recB.size());    print("***** LEAVING EntityCacheTester.testInvalidatingCacheInvalidation() *****");}/** * Adds and deletes invalidations directly via RDBMCachedEntityInvalidationStore. *  - Must be able to retrieve number of invalidations added from the store. *  - After deletions, must retrieve 0 invalidations from the store. */public void testStoreAddsAndDeletes() throws Exception{    print("***** ENTERING EntityCacheTester.testStoreAddsAndDeletes() *****");    String msg = null;    int idx = 0;    CachedEntityInvalidation[] invalidations = null;    int numAdds = 5;    msg = "Adding " + numAdds + " invalidations to the store.";    print(msg);    for(idx=0; idx<numAdds; idx++)        { getStore().add(testEntities[idx], 0); }    msg = "Retrieving invalidations from the store.";    print(msg);    invalidations = getStore().find(MINIMAL_ENTITY_CLASS, null);    assertEquals(msg, invalidations.length, numAdds);    msg = "Deleting invalidations from the store.";    print(msg);    getStore().deleteBefore(new Date());    msg = "Retrieving invalidations from the store.";    print(msg);    invalidations = getStore().find(MINIMAL_ENTITY_CLASS, null);    assertEquals(msg, invalidations.length, 0);    print("***** LEAVING EntityCacheTester.testStoreAddsAndDeletes() *****");}/** * Adds invalidations directly to the store in 2 batches.   * - Must be able to correctly findAfter().  */public void testStoreBeforeAndAfter() throws Exception{    print("***** ENTERING EntityCacheTester.testStoreBeforeAndAfter() *****");    String msg = null;    int idx = 0;    CachedEntityInvalidation[] invalidations = null;    int numBeforeAdds = 3;    int numAfterAdds = 2;    int cacheID = 0;    msg = "Adding " + numBeforeAdds + " invalidations to the store.";    print(msg);    for(idx=0; idx<numBeforeAdds; idx++)        { getStore().add(testEntities[idx], cacheID); }    msg = "Retrieving invalidations from the store.";    print(msg);    invalidations = getStore().find(MINIMAL_ENTITY_CLASS, null);    assertEquals(msg, invalidations.length, numBeforeAdds);    Date now = new Date();    Thread.sleep(10);    msg = "Adding " + numAfterAdds + " invalidations to the store.";    print(msg);    for(idx=numBeforeAdds; idx<(numAfterAdds + numBeforeAdds); idx++)        { getStore().add(testEntities[idx], cacheID); }    msg = "Retrieving invalidations from the store.";    print(msg);    invalidations = getStore().find(MINIMAL_ENTITY_CLASS, null);    assertEquals(msg, invalidations.length, numBeforeAdds + numAfterAdds);    msg = "Retrieving invalidations inserted AFTER first batch from the store.";    print(msg);    invalidations = getStore().findAfter(now, MINIMAL_ENTITY_CLASS, null, null);    assertEquals(msg, invalidations.length, numAfterAdds);    msg = "Deleting first batch of invalidations from the store.";    print(msg);    getStore().deleteBefore(now);    msg = "Retrieving invalidations from the store.";    print(msg);    invalidations = getStore().find(MINIMAL_ENTITY_CLASS, null);    assertEquals(msg, invalidations.length, numAfterAdds);    print("***** LEAVING EntityCacheTester.testStoreBeforeAndAfter() *****");}/** * Add some invalidations to the store and then update some of them.   * - Test findAfter() to retrieve only the updated ones. */public void testStoreUpdates() throws Exception{    print("***** ENTERING EntityCacheTester.testStoreUpdates() *****");    String msg = null;    int idx = 0;    CachedEntityInvalidation[] invalidations = null;    int numAdds = 5;    int numUpdates = 2;    msg = "Adding " + numAdds + " invalidations to the store.";    print(msg);    for(idx=0; idx<numAdds; idx++)        { getStore().add(testEntities[idx], 0); }    msg = "Retrieving invalidations from the store.";    print(msg);    invalidations = getStore().find(MINIMAL_ENTITY_CLASS, null);    assertEquals(msg, invalidations.length, numAdds);    Date now = new Date();    Thread.sleep(10);    msg = "Updating " + numUpdates + " invalidations in the store.";    print(msg);    for(idx=0; idx<numUpdates; idx++)        { getStore().add(testEntities[idx], 0); }    msg = "Retrieving only updated invalidations from the store.";    print(msg);    for(idx=0; idx<numUpdates; idx++)    {        String key = testEntities[idx].getEntityIdentifier().getKey();        invalidations = getStore().findAfter(now, MINIMAL_ENTITY_CLASS, key, null);        assertEquals(msg, 1, invalidations.length);    }    print("***** LEAVING EntityCacheTester.testStoreUpdates() *****");}/* *  */public void testFudgeFactor() throws Exception {    print("***** ENTERING EntityCacheTester.testFudgeFactor() *****");    String msg = null;    int idx = 0;    int numEntitiesToBeAdded = 10;    int numEntitiesToBeUpdated = 5;    int numEntitiesToBeDeleted = 2;    IEntityCache cacheA = getInvalidatingEntityCache();    ReferenceInvalidatingEntityCache recA = (ReferenceInvalidatingEntityCache) cacheA;    IEntityCache cacheB = getInvalidatingEntityCache();    ReferenceInvalidatingEntityCache recB = (ReferenceInvalidatingEntityCache) cacheB;    msg = "Adding " + numEntitiesToBeAdded + " entities to FIRST cache.";    print(msg);    for(idx=0; idx<numEntitiesToBeAdded; idx++)        { cacheA.add(testEntities[idx]); }    assertEquals(msg, recA.size(), numEntitiesToBeAdded);    Thread.sleep(100);    msg = "Updating " + numEntitiesToBeUpdated + " in first cache.";    print(msg);    for(idx=0; idx<numEntitiesToBeUpdated; idx++)        { cacheA.update( testEntities[idx] ); }    msg = "Adding " + numEntitiesToBeAdded + " entities to SECOND cache.";    print(msg);    for(idx=0; idx<numEntitiesToBeAdded; idx++)        { cacheB.add(testEntities[idx]); }    assertEquals(msg, recB.size(), numEntitiesToBeAdded);        msg = "Removing " + numEntitiesToBeDeleted + " from second cache.";     print(msg);     for(idx=numEntitiesToBeUpdated; idx<numEntitiesToBeUpdated + numEntitiesToBeDeleted; idx++)         { cacheB.remove( testEntityKeys[idx] ); }       print("Will now sleep for " + (cacheSweepIntervalSecs + 5) + " seconds.");    Thread.sleep( (cacheSweepIntervalSecs + 5) * 1000);    // Check the caches.    msg = "Checking second cache for invalidations";    print(msg);    assertEquals(msg, (numEntitiesToBeAdded - numEntitiesToBeUpdated - numEntitiesToBeDeleted), recB.size());        msg = "Checking first cache for invalidations";    print(msg);    for(idx=numEntitiesToBeUpdated; idx<numEntitiesToBeUpdated + numEntitiesToBeDeleted; idx++)        { assertNull(msg, cacheA.get( testEntityKeys[idx] )); }    print("***** LEAVING EntityCacheTester.testFudgeFactor() *****");}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -