Skip to content

Commit

Permalink
Updating unit tests
Browse files Browse the repository at this point in the history
* Adds unit tests for custom keys
* Adds unit tests for large keys
  • Loading branch information
ripleyb committed Sep 6, 2018
1 parent 25b6318 commit 5e6ecbf
Showing 1 changed file with 133 additions and 11 deletions.
144 changes: 133 additions & 11 deletions src/test/java/com/scaleoutsoftware/soss/cache/ScaleoutCacheTest.java
Expand Up @@ -37,6 +37,7 @@
import javax.cache.processor.MutableEntry;
import javax.cache.spi.CachingProvider;

import java.io.Serializable;
import java.util.*;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -76,6 +77,66 @@ void get() {
Assertions.assertNull(_cache.get("k1"));
}

@org.junit.jupiter.api.Test
void getWithLargeKey() {
String key = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" +
"BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB" +
"CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC" +
"DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD";
_cache.put(key, "v1");
String ret = _cache.get(key);
Assertions.assertEquals("v1", ret);

_cache.remove("k1");
Assertions.assertNull(_cache.get("k1"));
}

@org.junit.jupiter.api.Test
void getWithLargeCustomClassKey() {
CachingProvider provider = new ScaleoutCachingProvider();
CacheManager manager = provider.getCacheManager();
Cache<CustomKeyType, String> cache = manager.getCache("customKeyTest", CustomKeyType.class, String.class);
String keyValue = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" +
"BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB" +
"CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC" +
"DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD";
CustomKeyType key = new CustomKeyType(keyValue);
cache.put(key, "v1");
String ret = cache.get(key);
Assertions.assertEquals("v1", ret);

cache.remove(key);
Assertions.assertNull(cache.get(key));
}

@org.junit.jupiter.api.Test
void testExpiredWithLargeCustomClassKey() {
long timeout = 1;
long currentTimeMillis = System.currentTimeMillis() + (1000 * timeout);
int roundToNearestSecond = (int)((currentTimeMillis + 500) / 1000);
CacheEntryListenerConfiguration<CustomKeyType, String> configuration = getCacheEntryListenerConfigurationCustomKey(roundToNearestSecond);
CachingProvider provider = new ScaleoutCachingProvider();
CacheManager manager = provider.getCacheManager();
Cache<CustomKeyType, String> cache = manager.createCache(_name+"expiredCustomKey", new MutableConfiguration<CustomKeyType,String>()
.setTypes(CustomKeyType.class, String.class)
.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, timeout)))
.addCacheEntryListenerConfiguration(configuration));
String keyValue = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" +
"BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB" +
"CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC" +
"DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD";
CustomKeyType key = new CustomKeyType(keyValue);
cache.put(key, "bar");

try {
Thread.sleep(3000);
} catch (InterruptedException e) {
fail();
}
cache.deregisterCacheEntryListener(configuration);
}


@org.junit.jupiter.api.Test
void getAllremoveAll() {
_cache.clear();
Expand Down Expand Up @@ -251,38 +312,43 @@ void testExpired() {
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<String, String> cache = manager.getCache(_name + "_expired", String.class, String.class);

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

cache.registerCacheEntryListener(getCacheEntryListenerConfiguration(roundToNearestSecond));

CacheEntryListenerConfiguration<String, String> configuration = getCacheEntryListenerConfiguration(roundToNearestSecond);
cache.registerCacheEntryListener(configuration);
cache.put("foo", "bar");

try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
cache.deregisterCacheEntryListener(configuration);
}

@org.junit.jupiter.api.Test
void testExpiredThroughConfig() {
long timeout = 1;
long currentTimeMillis = System.currentTimeMillis() + (1000 * timeout);
int roundToNearestSecond = (int)((currentTimeMillis + 500) / 1000);
CacheEntryListenerConfiguration<String, String> configuration = getCacheEntryListenerConfiguration(roundToNearestSecond);
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(roundToNearestSecond)));
Cache<String,String> cache = manager.createCache(_name+"_expiredThroughConfig", new MutableConfiguration<String,String>()
.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, timeout)))
.addCacheEntryListenerConfiguration(configuration));

cache.put("foo", "bar");

try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
cache.deregisterCacheEntryListener(configuration);
}

@org.junit.jupiter.api.Test
Expand All @@ -291,11 +357,12 @@ void testCorrectExpiredPolicyThroughConfig() {
// round to nearest second * 2 because we will touch the object
long currentTimeMillis = System.currentTimeMillis() + ((1000 * timeout) * 2);
int roundToNearestSecond = (int)((currentTimeMillis + 500) / 1000);
CacheEntryListenerConfiguration<String, String> configuration = getCacheEntryListenerConfiguration(roundToNearestSecond);
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<String,String> cache = manager.createCache(_name+"_expiredThroughConfigSliding", new MutableConfiguration<String,String>()
.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, 1)))
.addCacheEntryListenerConfiguration(configuration));
cache.put("foo", "bar");
try {
Thread.sleep(750);
Expand All @@ -311,6 +378,7 @@ void testCorrectExpiredPolicyThroughConfig() {
e.printStackTrace();
fail();
}
cache.deregisterCacheEntryListener(configuration);
}

@org.junit.jupiter.api.Test
Expand Down Expand Up @@ -368,13 +436,12 @@ void testInvoke() {
String ret = _cache.invoke("k1", new EntryProcessor<String, String, String>() {
@Override
public String process(MutableEntry<String, String> mutableEntry, Object... objects) throws EntryProcessorException {
System.out.println(mutableEntry.getValue());
return "foo";
}
});


assertEquals(0, ret.compareTo("foo"));
assertEquals("foo", ret);
}
@org.junit.jupiter.api.Test
void testInvokeAll() {
Expand Down Expand Up @@ -415,6 +482,7 @@ public void onExpired(Iterable<CacheEntryEvent<? extends String, ? extends Strin
System.out.println("Actual: " + roundToNearestSecond + " expected: " + expected);
for(CacheEntryEvent<? extends String, ? extends String> kvp : iterable) {
assertEquals(roundToNearestSecond, expected);
assertEquals(kvp.getValue(), "bar");
}
}
};
Expand All @@ -438,4 +506,58 @@ public boolean isSynchronous() {
}
};
}

private CacheEntryListenerConfiguration<CustomKeyType, String> getCacheEntryListenerConfigurationCustomKey(final long expected) {
return new CacheEntryListenerConfiguration<CustomKeyType, String>() {
@Override
public Factory<CacheEntryListener<? super CustomKeyType, ? super String>> getCacheEntryListenerFactory() {
return new Factory<CacheEntryListener<? super CustomKeyType, ? super String>>() {
@Override
public CacheEntryListener<? super CustomKeyType, ? super String> create() {
return new CacheEntryExpiredListener<CustomKeyType, String>() {
@Override
public void onExpired(Iterable<CacheEntryEvent<? extends CustomKeyType, ? 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 CustomKeyType, ? extends String> kvp : iterable) {
assertEquals(roundToNearestSecond, expected);
assertEquals(kvp.getValue(), "bar");
}
}
};
}
};
}

@Override
public boolean isOldValueRequired() {
return false;
}

@Override
public Factory<CacheEntryEventFilter<? super CustomKeyType, ? super String>> getCacheEntryEventFilterFactory() {
return null;
}

@Override
public boolean isSynchronous() {
return false;
}
};
}

private static class CustomKeyType implements Serializable {
String _keyValue;
public CustomKeyType(String value) {
_keyValue = value;
}

@Override
public String toString() {
return "CustomKeyType{" +
"_keyValue='" + _keyValue + '\'' +
'}';
}
}
}

0 comments on commit 5e6ecbf

Please sign in to comment.