Search in sources :

Example 1 with MyCacheEventListener

use of com.adaptris.core.cache.MyCacheEventListener in project interlok by adaptris.

the class AddValueToCacheTest method testDoService_NoExpiryMetadata.

@Test
public void testDoService_NoExpiryMetadata() throws Exception {
    AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage("Hello World");
    ExpiringMapCache cache = createCacheInstanceForTests().withExpiration(new TimeInterval(1L, TimeUnit.SECONDS));
    MyCacheEventListener listener = new MyCacheEventListener();
    cache.getEventListener().addEventListener(listener);
    AddValueToCache service = new AddValueToCache().withExpiry("%message{expiry}").withValueTranslator(new StringPayloadCacheTranslator()).withKey("%message{%uniqueId}").withConnection(new CacheConnection().withCacheInstance(cache));
    try {
        start(service);
        // msg.addMetadata("expiry", "a"); // empty expiry should not expire.
        service.doService(msg);
        Object value = cache.get(msg.getUniqueId());
        assertEquals("Hello World", value);
        await().atMost(Duration.ofSeconds(5)).with().pollInterval(Duration.ofMillis(100)).until(listener::expiredCount, greaterThanOrEqualTo(1));
        assertEquals(0, cache.size());
    } finally {
        stop(service);
    }
}
Also used : TimeInterval(com.adaptris.util.TimeInterval) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) ExpiringMapCache(com.adaptris.core.cache.ExpiringMapCache) StringPayloadCacheTranslator(com.adaptris.core.services.cache.translators.StringPayloadCacheTranslator) MyCacheEventListener(com.adaptris.core.cache.MyCacheEventListener) Test(org.junit.Test)

Example 2 with MyCacheEventListener

use of com.adaptris.core.cache.MyCacheEventListener in project interlok by adaptris.

the class AddValueToCacheTest method testDoService_AbsoluteExpiry_Millis.

@Test
public void testDoService_AbsoluteExpiry_Millis() throws Exception {
    AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage("Hello World");
    ExpiringMapCache cache = createCacheInstanceForTests();
    MyCacheEventListener listener = new MyCacheEventListener();
    cache.getEventListener().addEventListener(listener);
    AddValueToCache service = new AddValueToCache().withExpiry("%message{expiry}").withValueTranslator(new StringPayloadCacheTranslator()).withKey("%message{%uniqueId}").withConnection(new CacheConnection().withCacheInstance(cache));
    try {
        start(service);
        // Expire in 2 seconds or so.
        long expiry = System.currentTimeMillis() + ThreadLocalRandom.current().nextInt(1000) + 1000;
        msg.addMetadata("expiry", String.valueOf(expiry));
        System.err.println(getName() + ":" + msg.getMetadataValue("expiry"));
        service.doService(msg);
        Object value = cache.get(msg.getUniqueId());
        assertEquals("Hello World", value);
        // Default expiration is > 5 seconds.
        await().atMost(Duration.ofSeconds(5)).with().pollInterval(Duration.ofMillis(100)).until(listener::expiredCount, greaterThanOrEqualTo(1));
        assertEquals(0, cache.size());
    } finally {
        stop(service);
    }
}
Also used : AdaptrisMessage(com.adaptris.core.AdaptrisMessage) ExpiringMapCache(com.adaptris.core.cache.ExpiringMapCache) StringPayloadCacheTranslator(com.adaptris.core.services.cache.translators.StringPayloadCacheTranslator) MyCacheEventListener(com.adaptris.core.cache.MyCacheEventListener) Test(org.junit.Test)

Example 3 with MyCacheEventListener

use of com.adaptris.core.cache.MyCacheEventListener in project interlok by adaptris.

the class AddValueToCacheTest method testDoService_Expiry_Unsupported.

@Test
public void testDoService_Expiry_Unsupported() throws Exception {
    AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage("Hello World");
    ExpiringMapCache cache = createCacheInstanceForTests().withExpiration(new TimeInterval(1L, TimeUnit.SECONDS));
    MyCacheEventListener listener = new MyCacheEventListener();
    cache.getEventListener().addEventListener(listener);
    AddValueToCache service = new AddValueToCache().withExpiry("%message{expiry}").withValueTranslator(new StringPayloadCacheTranslator()).withKey("%message{%uniqueId}").withConnection(new CacheConnection().withCacheInstance(cache));
    try {
        start(service);
        // unparseable expiry should not expire.
        msg.addMetadata("expiry", "a");
        service.doService(msg);
        Object value = cache.get(msg.getUniqueId());
        assertEquals("Hello World", value);
        await().atMost(Duration.ofSeconds(5)).with().pollInterval(Duration.ofMillis(100)).until(listener::expiredCount, greaterThanOrEqualTo(1));
        assertEquals(0, cache.size());
    } finally {
        stop(service);
    }
}
Also used : TimeInterval(com.adaptris.util.TimeInterval) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) ExpiringMapCache(com.adaptris.core.cache.ExpiringMapCache) StringPayloadCacheTranslator(com.adaptris.core.services.cache.translators.StringPayloadCacheTranslator) MyCacheEventListener(com.adaptris.core.cache.MyCacheEventListener) Test(org.junit.Test)

Example 4 with MyCacheEventListener

use of com.adaptris.core.cache.MyCacheEventListener in project interlok by adaptris.

the class AddValueToCacheTest method testDoService_AbsoluteExpiry_Date.

@Test
public void testDoService_AbsoluteExpiry_Date() throws Exception {
    AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage("Hello World");
    ExpiringMapCache cache = createCacheInstanceForTests();
    MyCacheEventListener listener = new MyCacheEventListener();
    cache.getEventListener().addEventListener(listener);
    AddValueToCache service = new AddValueToCache().withExpiry("%message{expiry}").withValueTranslator(new StringPayloadCacheTranslator()).withKey("%message{%uniqueId}").withConnection(new CacheConnection().withCacheInstance(cache));
    try {
        start(service);
        // Expire in 2 seconds or so.
        Date expiry = new Date(System.currentTimeMillis() + ThreadLocalRandom.current().nextInt(1000) + 1000);
        msg.addMetadata("expiry", DateFormatUtil.format(expiry));
        service.doService(msg);
        Object value = cache.get(msg.getUniqueId());
        assertEquals("Hello World", value);
        // Default expiration is > 5 seconds.
        await().atMost(Duration.ofSeconds(5)).with().pollInterval(Duration.ofMillis(100)).until(listener::expiredCount, greaterThanOrEqualTo(1));
        assertEquals(0, cache.size());
    } finally {
        stop(service);
    }
}
Also used : AdaptrisMessage(com.adaptris.core.AdaptrisMessage) ExpiringMapCache(com.adaptris.core.cache.ExpiringMapCache) StringPayloadCacheTranslator(com.adaptris.core.services.cache.translators.StringPayloadCacheTranslator) MyCacheEventListener(com.adaptris.core.cache.MyCacheEventListener) Date(java.util.Date) Test(org.junit.Test)

Example 5 with MyCacheEventListener

use of com.adaptris.core.cache.MyCacheEventListener in project interlok by adaptris.

the class AddValueToCacheTest method testDoService_RelativeExpiry.

@Test
public void testDoService_RelativeExpiry() throws Exception {
    AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage("Hello World");
    ExpiringMapCache cache = createCacheInstanceForTests();
    MyCacheEventListener listener = new MyCacheEventListener();
    cache.getEventListener().addEventListener(listener);
    AddValueToCache service = new AddValueToCache().withExpiry("%message{expiry}").withValueTranslator(new StringPayloadCacheTranslator()).withKey("%message{%uniqueId}").withConnection(new CacheConnection().withCacheInstance(cache));
    try {
        start(service);
        // relative expiry, 500ms
        msg.addMetadata("expiry", "500");
        service.doService(msg);
        Object value = cache.get(msg.getUniqueId());
        assertEquals("Hello World", value);
        // Default expiration is > 5 seconds.
        await().atMost(Duration.ofSeconds(5)).with().pollInterval(Duration.ofMillis(100)).until(listener::expiredCount, greaterThanOrEqualTo(1));
        assertEquals(0, cache.size());
    } finally {
        stop(service);
    }
}
Also used : AdaptrisMessage(com.adaptris.core.AdaptrisMessage) ExpiringMapCache(com.adaptris.core.cache.ExpiringMapCache) StringPayloadCacheTranslator(com.adaptris.core.services.cache.translators.StringPayloadCacheTranslator) MyCacheEventListener(com.adaptris.core.cache.MyCacheEventListener) Test(org.junit.Test)

Aggregations

AdaptrisMessage (com.adaptris.core.AdaptrisMessage)5 ExpiringMapCache (com.adaptris.core.cache.ExpiringMapCache)5 MyCacheEventListener (com.adaptris.core.cache.MyCacheEventListener)5 StringPayloadCacheTranslator (com.adaptris.core.services.cache.translators.StringPayloadCacheTranslator)5 Test (org.junit.Test)5 TimeInterval (com.adaptris.util.TimeInterval)2 Date (java.util.Date)1