Search in sources :

Example 31 with Cache

use of org.cache2k.Cache in project cache2k by cache2k.

the class JmxSupportTest method multipleWarnings.

/**
 * Construct three caches with multiple issues and check the health string of the manager JMX.
 */
@Test
public void multipleWarnings() throws Exception {
    final String _MANAGER_NAME = getClass().getName() + ".multipleWarnings";
    final String _CACHE_NAME_BAD_HASHING = "cacheWithBadHashing";
    final String _CACHE_NAME_KEY_MUTATION = "cacheWithKeyMutation";
    final String _CACHE_NAME_MULTIPLE_ISSUES = "cacheWithMultipleIssues";
    final String _SUPPRESS1 = "org.cache2k.Cache/" + _MANAGER_NAME + ":" + _CACHE_NAME_KEY_MUTATION;
    final String _SUPPRESS2 = "org.cache2k.Cache/" + _MANAGER_NAME + ":" + _CACHE_NAME_MULTIPLE_ISSUES;
    Log.registerSuppression(_SUPPRESS1, new Log.SuppressionCounter());
    Log.registerSuppression(_SUPPRESS2, new Log.SuppressionCounter());
    CacheManager m = CacheManager.getInstance(_MANAGER_NAME);
    Cache _cacheWithBadHashing = Cache2kBuilder.of(Object.class, Object.class).manager(m).name(_CACHE_NAME_BAD_HASHING).eternal(true).build();
    Cache _cacheWithKeyMutation = Cache2kBuilder.of(Object.class, Object.class).manager(m).name(_CACHE_NAME_KEY_MUTATION).eternal(true).entryCapacity(50).build();
    Cache _cacheWithMultipleIssues = Cache2kBuilder.of(Object.class, Object.class).manager(m).name(_CACHE_NAME_MULTIPLE_ISSUES).entryCapacity(50).eternal(true).build();
    for (int i = 0; i < 9; i++) {
        _cacheWithBadHashing.put(new KeyForMutation(), 1);
    }
    for (int i = 0; i < 100; i++) {
        _cacheWithMultipleIssues.put(new KeyForMutation(), 1);
    }
    for (int i = 0; i < 100; i++) {
        KeyForMutation v = new KeyForMutation();
        _cacheWithKeyMutation.put(v, 1);
        _cacheWithMultipleIssues.put(v, 1);
        v.value = 1;
    }
    String _health = (String) server.getAttribute(getCacheManagerObjectName(_MANAGER_NAME), "HealthStatus");
    assertEquals("FAILURE: [cacheWithKeyMutation] hash quality is 0 (threshold: 5); " + "FAILURE: [cacheWithMultipleIssues] hash quality is 0 (threshold: 5); " + "WARNING: [cacheWithBadHashing] hash quality is 7 (threshold: 20); " + "WARNING: [cacheWithKeyMutation] key mutation detected; " + "WARNING: [cacheWithMultipleIssues] key mutation detected", _health);
    Log.deregisterSuppression(_SUPPRESS1);
    Log.deregisterSuppression(_SUPPRESS2);
    m.close();
}
Also used : Log(org.cache2k.core.util.Log) CacheManager(org.cache2k.CacheManager) Cache(org.cache2k.Cache) Test(org.junit.Test)

Example 32 with Cache

use of org.cache2k.Cache in project cache2k by cache2k.

the class JmxSupportTest method testDisabled.

@Test
public void testDisabled() throws Exception {
    String _name = getClass().getName() + ".testInitialProperties";
    Cache c = new Cache2kBuilder<Long, List<Collection<Long>>>() {
    }.name(_name).disableStatistics(true).eternal(true).build();
    objectName = constructCacheObjectName(_name);
    try {
        retrieve("Alert");
        fail("exception expected");
    } catch (InstanceNotFoundException ex) {
    }
    c.close();
}
Also used : InstanceNotFoundException(javax.management.InstanceNotFoundException) Collection(java.util.Collection) Cache2kBuilder(org.cache2k.Cache2kBuilder) Cache(org.cache2k.Cache) Test(org.junit.Test)

Example 33 with Cache

use of org.cache2k.Cache in project cache2k by cache2k.

the class JmxSupportTest method testInitialProperties.

@Test
public void testInitialProperties() throws Exception {
    Date _beforeCreateion = new Date();
    String _name = getClass().getName() + ".testInitialProperties";
    Cache c = new Cache2kBuilder<Long, List<Collection<Long>>>() {
    }.name(_name).eternal(true).build();
    objectName = constructCacheObjectName(_name);
    checkAttribute("KeyType", "Long");
    checkAttribute("ValueType", "java.util.List<java.util.Collection<Long>>");
    checkAttribute("Size", 0L);
    checkAttribute("EntryCapacity", 1234L);
    checkAttribute("InsertCount", 0L);
    checkAttribute("MissCount", 0L);
    checkAttribute("RefreshCount", 0L);
    checkAttribute("RefreshFailedCount", 0L);
    checkAttribute("RefreshedHitCount", 0L);
    checkAttribute("ExpiredCount", 0L);
    checkAttribute("EvictedCount", 0L);
    checkAttribute("PutCount", 0L);
    checkAttribute("RemoveCount", 0L);
    checkAttribute("ClearedEntriesCount", 0L);
    checkAttribute("ClearCount", 0L);
    checkAttribute("KeyMutationCount", 0L);
    checkAttribute("LoadExceptionCount", 0L);
    checkAttribute("SuppressedLoadExceptionCount", 0L);
    checkAttribute("HitRate", 0.0);
    checkAttribute("HashQuality", 100);
    checkAttribute("MillisPerLoad", 0.0);
    checkAttribute("TotalLoadMillis", 0L);
    checkAttribute("Implementation", "HeapCache");
    checkAttribute("ClearedTime", null);
    checkAttribute("Alert", 0);
    assertTrue("reasonable CreatedTime", ((Date) retrieve("CreatedTime")).compareTo(_beforeCreateion) >= 0);
    assertTrue("reasonable InfoCreatedTime", ((Date) retrieve("InfoCreatedTime")).compareTo(_beforeCreateion) >= 0);
    assertTrue("reasonable InfoCreatedDeltaMillis", ((Integer) retrieve("InfoCreatedDeltaMillis")) >= 0);
    assertTrue("reasonable EvictionStatistics", retrieve("EvictionStatistics").toString().contains("impl="));
    assertTrue("reasonable IntegrityDescriptor", retrieve("IntegrityDescriptor").toString().startsWith("0."));
    c.close();
}
Also used : List(java.util.List) Date(java.util.Date) Cache(org.cache2k.Cache) Test(org.junit.Test)

Example 34 with Cache

use of org.cache2k.Cache in project cache2k by cache2k.

the class BasicCacheOperationsTest method peekAndRemove_Exception.

@Test
public void peekAndRemove_Exception() {
    ((Cache) cache).put(KEY, new ExceptionWrapper(OUCH));
    try {
        cache.peekAndRemove(KEY);
        fail("exception expected");
    } catch (CacheLoaderException ex) {
    }
}
Also used : CacheLoaderException(org.cache2k.integration.CacheLoaderException) ExceptionWrapper(org.cache2k.core.ExceptionWrapper) InternalCache(org.cache2k.core.InternalCache) Cache(org.cache2k.Cache)

Example 35 with Cache

use of org.cache2k.Cache in project cache2k by cache2k.

the class BasicCacheOperationsTest method getEntryState_Exception.

@Test
public void getEntryState_Exception() {
    if (!(cache instanceof InternalCache)) {
        return;
    }
    ((Cache) cache).put(KEY, new ExceptionWrapper(OUCH));
    InternalCache c = (InternalCache) cache;
    String s = c.getEntryState(KEY);
    assertTrue(s.contains("exception="));
}
Also used : InternalCache(org.cache2k.core.InternalCache) ExceptionWrapper(org.cache2k.core.ExceptionWrapper) InternalCache(org.cache2k.core.InternalCache) Cache(org.cache2k.Cache)

Aggregations

Cache (org.cache2k.Cache)60 Test (org.junit.Test)49 CacheManager (org.cache2k.CacheManager)11 Cache2kBuilder (org.cache2k.Cache2kBuilder)9 CacheEntry (org.cache2k.CacheEntry)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 ExceptionWrapper (org.cache2k.core.ExceptionWrapper)8 InternalCache (org.cache2k.core.InternalCache)8 CacheRule (org.cache2k.test.util.CacheRule)6 Condition (org.cache2k.test.util.Condition)6 IntCacheRule (org.cache2k.test.util.IntCacheRule)5 CountDownLatch (java.util.concurrent.CountDownLatch)3 MBeanInfo (javax.management.MBeanInfo)3 Log (org.cache2k.core.util.Log)3 CacheEntryCreatedListener (org.cache2k.event.CacheEntryCreatedListener)3 CacheEntryUpdatedListener (org.cache2k.event.CacheEntryUpdatedListener)3 ArrayList (java.util.ArrayList)2 CacheLoaderException (org.cache2k.integration.CacheLoaderException)2 Collection (java.util.Collection)1 Date (java.util.Date)1