From 5e6ecbf02be32fc1cebda1922dfdf08e6c9e08fb Mon Sep 17 00:00:00 2001 From: Brandon Date: Thu, 6 Sep 2018 13:37:25 -0700 Subject: [PATCH] Updating unit tests * Adds unit tests for custom keys * Adds unit tests for large keys --- .../soss/cache/ScaleoutCacheTest.java | 144 ++++++++++++++++-- 1 file changed, 133 insertions(+), 11 deletions(-) diff --git a/src/test/java/com/scaleoutsoftware/soss/cache/ScaleoutCacheTest.java b/src/test/java/com/scaleoutsoftware/soss/cache/ScaleoutCacheTest.java index 5b7f653..61286a4 100644 --- a/src/test/java/com/scaleoutsoftware/soss/cache/ScaleoutCacheTest.java +++ b/src/test/java/com/scaleoutsoftware/soss/cache/ScaleoutCacheTest.java @@ -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; @@ -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 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 configuration = getCacheEntryListenerConfigurationCustomKey(roundToNearestSecond); + CachingProvider provider = new ScaleoutCachingProvider(); + CacheManager manager = provider.getCacheManager(); + Cache cache = manager.createCache(_name+"expiredCustomKey", new MutableConfiguration() + .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(); @@ -251,19 +312,21 @@ 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 cache = manager.getCache(_name, String.class, String.class); + Cache 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 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 @@ -271,11 +334,13 @@ void testExpiredThroughConfig() { long timeout = 1; long currentTimeMillis = System.currentTimeMillis() + (1000 * timeout); int roundToNearestSecond = (int)((currentTimeMillis + 500) / 1000); + CacheEntryListenerConfiguration configuration = getCacheEntryListenerConfiguration(roundToNearestSecond); CachingProvider provider = new ScaleoutCachingProvider(); CacheManager manager = provider.getCacheManager(); - Cache cache = manager.createCache(_name, new MutableConfiguration() - .setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, 1))) - .addCacheEntryListenerConfiguration(getCacheEntryListenerConfiguration(roundToNearestSecond))); + Cache cache = manager.createCache(_name+"_expiredThroughConfig", new MutableConfiguration() + .setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, timeout))) + .addCacheEntryListenerConfiguration(configuration)); + cache.put("foo", "bar"); try { @@ -283,6 +348,7 @@ void testExpiredThroughConfig() { } catch (InterruptedException e) { e.printStackTrace(); } + cache.deregisterCacheEntryListener(configuration); } @org.junit.jupiter.api.Test @@ -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 configuration = getCacheEntryListenerConfiguration(roundToNearestSecond); CachingProvider provider = new ScaleoutCachingProvider(); CacheManager manager = provider.getCacheManager(); - Cache cache = manager.createCache(_name, new MutableConfiguration() - .setExpiryPolicyFactory(TouchedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, 1))) - .addCacheEntryListenerConfiguration(getCacheEntryListenerConfiguration(roundToNearestSecond))); + Cache cache = manager.createCache(_name+"_expiredThroughConfigSliding", new MutableConfiguration() + .setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, 1))) + .addCacheEntryListenerConfiguration(configuration)); cache.put("foo", "bar"); try { Thread.sleep(750); @@ -311,6 +378,7 @@ void testCorrectExpiredPolicyThroughConfig() { e.printStackTrace(); fail(); } + cache.deregisterCacheEntryListener(configuration); } @org.junit.jupiter.api.Test @@ -368,13 +436,12 @@ void testInvoke() { String ret = _cache.invoke("k1", new EntryProcessor() { @Override public String process(MutableEntry 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() { @@ -415,6 +482,7 @@ public void onExpired(Iterable kvp : iterable) { assertEquals(roundToNearestSecond, expected); + assertEquals(kvp.getValue(), "bar"); } } }; @@ -438,4 +506,58 @@ public boolean isSynchronous() { } }; } + + private CacheEntryListenerConfiguration getCacheEntryListenerConfigurationCustomKey(final long expected) { + return new CacheEntryListenerConfiguration() { + @Override + public Factory> getCacheEntryListenerFactory() { + return new Factory>() { + @Override + public CacheEntryListener create() { + return new CacheEntryExpiredListener() { + @Override + public void onExpired(Iterable> iterable) throws CacheEntryListenerException { + long currentTimeMillis = System.currentTimeMillis(); + int roundToNearestSecond = (int)((currentTimeMillis + 500) / 1000); + System.out.println("Actual: " + roundToNearestSecond + " expected: " + expected); + for(CacheEntryEvent kvp : iterable) { + assertEquals(roundToNearestSecond, expected); + assertEquals(kvp.getValue(), "bar"); + } + } + }; + } + }; + } + + @Override + public boolean isOldValueRequired() { + return false; + } + + @Override + public Factory> 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 + '\'' + + '}'; + } + } } \ No newline at end of file