Skip to content

Commit

Permalink
Stricter unit testing
Browse files Browse the repository at this point in the history
Adds unit tests for ensuring correct expiry policy was used
Expiration unit tests now correctly compare times expiry times
  • Loading branch information
ripleyb committed Sep 5, 2018
1 parent c486c35 commit adc0320
Showing 1 changed file with 48 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
import javax.cache.configuration.MutableCacheEntryListenerConfiguration;
import javax.cache.configuration.MutableConfiguration;
import javax.cache.event.*;
import javax.cache.expiry.AccessedExpiryPolicy;
import javax.cache.expiry.CreatedExpiryPolicy;
import javax.cache.expiry.Duration;
import javax.cache.expiry.TouchedExpiryPolicy;
import javax.cache.processor.EntryProcessor;
import javax.cache.processor.EntryProcessorException;
import javax.cache.processor.EntryProcessorResult;
Expand Down Expand Up @@ -242,17 +244,21 @@ void getName() {

@org.junit.jupiter.api.Test
void testExpired() {
long timeout = 1;
CachingProvider provider = new ScaleoutCachingProvider();
Properties props = provider.getDefaultProperties();
props.setProperty("object_expiration_timeout_secs", "1");

props.setProperty("object_expiration_timeout_secs", Long.toString(timeout));
props.setProperty("object_expiration_timeout_policy", "absolute");
CacheManager manager = provider.getCacheManager(provider.getDefaultURI(), provider.getDefaultClassLoader(), props);
Cache<String, String> cache = manager.getCache(_name, String.class, String.class);
cache.put("foo", "bar");
String isnullstring = null;

cache.registerCacheEntryListener(getCacheEntryListenerConfiguration());
long currentTimeMillis = System.currentTimeMillis() + (1000 * timeout);
int roundToNearestSecond = (int)((currentTimeMillis + 500) / 1000);

cache.registerCacheEntryListener(getCacheEntryListenerConfiguration(roundToNearestSecond));

cache.put("foo", "bar");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
Expand All @@ -262,11 +268,14 @@ void testExpired() {

@org.junit.jupiter.api.Test
void testExpiredThroughConfig() {
long timeout = 1;
long currentTimeMillis = System.currentTimeMillis() + (1000 * timeout);
int roundToNearestSecond = (int)((currentTimeMillis + 500) / 1000);
CachingProvider provider = new ScaleoutCachingProvider();
CacheManager manager = provider.getCacheManager();
Cache<String,String> cache = manager.createCache(_name, new MutableConfiguration<String,String>()
.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, 1)))
.addCacheEntryListenerConfiguration(getCacheEntryListenerConfiguration()));
.addCacheEntryListenerConfiguration(getCacheEntryListenerConfiguration(roundToNearestSecond)));
cache.put("foo", "bar");

try {
Expand All @@ -276,11 +285,39 @@ void testExpiredThroughConfig() {
}
}

@org.junit.jupiter.api.Test
void testCorrectExpiredPolicyThroughConfig() {
long timeout = 1;
// round to nearest second * 2 because we will touch the object
long currentTimeMillis = System.currentTimeMillis() + ((1000 * timeout) * 2);
int roundToNearestSecond = (int)((currentTimeMillis + 500) / 1000);
CachingProvider provider = new ScaleoutCachingProvider();
CacheManager manager = provider.getCacheManager();
Cache<String,String> cache = manager.createCache(_name, new MutableConfiguration<String,String>()
.setExpiryPolicyFactory(TouchedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, 1)))
.addCacheEntryListenerConfiguration(getCacheEntryListenerConfiguration(roundToNearestSecond)));
cache.put("foo", "bar");
try {
Thread.sleep(750);
} catch (InterruptedException e) {
e.printStackTrace();
fail();
}
cache.get("foo");

try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
fail();
}
}

@org.junit.jupiter.api.Test
void testRemoveCacheEntryListenerConfiguration() {
CachingProvider provider = new ScaleoutCachingProvider();
CacheManager manager = provider.getCacheManager();
CacheEntryListenerConfiguration<String, String> configuration = getCacheEntryListenerConfiguration();
CacheEntryListenerConfiguration<String, String> configuration = getCacheEntryListenerConfiguration(1);
_cache = manager.createCache(_name, new MutableConfiguration<String,String>()
.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, 1)))
.addCacheEntryListenerConfiguration(configuration));
Expand Down Expand Up @@ -363,7 +400,7 @@ public String process(MutableEntry<String, String> mutableEntry, Object... objec
}
}

private CacheEntryListenerConfiguration<String, String> getCacheEntryListenerConfiguration() {
private CacheEntryListenerConfiguration<String, String> getCacheEntryListenerConfiguration(final long expected) {
return new CacheEntryListenerConfiguration<String, String>() {
@Override
public Factory<CacheEntryListener<? super String, ? super String>> getCacheEntryListenerFactory() {
Expand All @@ -373,8 +410,11 @@ private CacheEntryListenerConfiguration<String, String> getCacheEntryListenerCon
return new CacheEntryExpiredListener<String, String>() {
@Override
public void onExpired(Iterable<CacheEntryEvent<? extends String, ? extends String>> iterable) throws CacheEntryListenerException {
long currentTimeMillis = System.currentTimeMillis();
int roundToNearestSecond = (int)((currentTimeMillis + 500) / 1000);
System.out.println("Actual: " + roundToNearestSecond + " expected: " + expected);
for(CacheEntryEvent<? extends String, ? extends String> kvp : iterable) {
System.out.println("key: " + kvp.getKey() + " value: " + kvp.getValue());
assertEquals(roundToNearestSecond, expected);
}
}
};
Expand Down

0 comments on commit adc0320

Please sign in to comment.